slug = $slug; $this->condition = 'stock_status'; /* this adds the condition in set of rules dropdown */ add_filter("pi_".$this->slug."_condition", array($this, 'addRule')); /* this gives value field blank of populated */ add_action( 'wp_ajax_pi_'.$this->slug.'_value_field_'.$this->condition, array( $this, 'ajaxCall' ) ); add_filter('pi_'.$this->slug.'_saved_values_'.$this->condition, array($this, 'savedDropdown'), 10, 3); add_filter('pi_'.$this->slug.'_condition_check_'.$this->condition,array($this,'conditionCheck'),10,5); add_action('pi_'.$this->slug.'_logic_'.$this->condition, array($this, 'logicDropdown')); add_filter('pi_'.$this->slug.'_saved_logic_'.$this->condition, array($this, 'savedLogic'),10,3); } function addRule($rules){ $rules[$this->condition] = array( 'name'=>__('Stock status of product in cart','advanced-free-flat-shipping-woocommerce'), 'group'=>'cart_related', 'condition'=>$this->condition ); return $rules; } function logicDropdown(){ $html = ""; $html .= 'var pi_logic_'.$this->condition.'= "";'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $html; } function savedLogic($html_in, $saved_logic, $count){ $html = ""; $html .= ''; return $html; } function ajaxCall(){ $cap = Pi_Efrs_Menu::getCapability(); if(!current_user_can( $cap )) { return; die; } $count = sanitize_text_field(filter_input(INPUT_POST,'count')); echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo Pi_efrs_selection_rule_main::createNumberField($count,$this->condition, null,'any'); echo ''; die; } function savedDropdown($html, $values, $count){ $html = ''; $html .= Pi_efrs_selection_rule_main::createNumberField($count, $this->condition, $values,'any'); $html .= ''; return $html; } function backOrderProductPresent($package, $package_support){ $back_order_present = false; if(function_exists('WC') && is_object(WC()->cart)){ $package_support = 'package'; if($package_support == 'cart'){ $products = WC()->cart->get_cart(); }else{ $products = PISOL\EFRS\Package::get_products($package); } foreach( $products as $product ){ $quantity = $product['quantity']; if( $product['data']->is_on_backorder( $quantity ) ){ $back_order_present = true; } } } return $back_order_present; } function conditionCheck($result, $package, $logic, $values, $package_support){ $or_result = false; $back_order_present = $this->backOrderProductPresent($package, $package_support); switch ($logic){ case 'all-in-stock': if($back_order_present){ $or_result = false; }else{ $or_result = true; } break; case 'back-order-present': if($back_order_present){ $or_result = true; }else{ $or_result = false; } break; } return $or_result; } } new Pi_efrs_selection_rule_stock_status(PI_EFRS_SELECTION_RULE_SLUG);