slug = $slug; $this->condition = 'category_product'; /* this adds the condition in set of rules dropdown */ add_filter("pi_".$this->slug."_condition", array($this, 'addRule')); /* this gives value field to store condition value either select or text box */ add_action( 'wp_ajax_pi_'.$this->slug.'_value_field_'.$this->condition, array( $this, 'ajaxCall' ) ); /* This gives our field with saved value */ add_filter('pi_'.$this->slug.'_saved_values_'.$this->condition, array($this, 'savedDropdown'), 10, 3); /* This perform condition check */ add_filter('pi_'.$this->slug.'_condition_check_'.$this->condition,array($this,'conditionCheck'),10,5); /* This gives out logic dropdown */ add_action('pi_'.$this->slug.'_logic_'.$this->condition, array($this, 'logicDropdown')); /* This give saved logic dropdown */ add_filter('pi_'.$this->slug.'_saved_logic_'.$this->condition, array($this, 'savedLogic'),10,3); } function addRule($rules){ $rules[$this->condition] = array( 'name'=>__('Cart has product of category','advanced-free-flat-shipping-woocommerce'), 'group'=>'product_related', 'condition'=>$this->condition ); return $rules; } function logicDropdown(){ $html = ""; $html .= 'var pi_logic_'.$this->condition.'= "'; $html .= 'Know more about this";'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $html; } function savedLogic($html_in, $saved_logic, $count){ $html = ""; $html .= ''; $html .= 'Know more about this'; return $html; } function ajaxCall(){ if(!current_user_can( 'manage_options' )) { return; die; } $count = sanitize_text_field(filter_input(INPUT_POST,'count')); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo Pi_efrs_selection_rule_main::createSelect($this->allCategories(),$count,$this->condition, "multiple",null,'static'); die; } function savedDropdown($html, $values, $count){ $html = Pi_efrs_selection_rule_main::createSelect($this->allCategories(), $count,$this->condition, "multiple", $values,'static'); return $html; } function allCategories(){ $taxonomy = 'product_cat'; $post_status = 'publish'; $orderby = 'name'; $hierarchical = 1; // 1 for yes, 0 for no $empty = 0; $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'taxonomy' => 'product_cat', 'orderby' => 'name', 'hierarchical' => 1, 'hide_empty' => 0, 'posts_per_page' => 1000, ); global $sitepress; if($sitepress && is_object($sitepress)){ remove_filter('terms_clauses', array($sitepress, 'terms_clauses')); } $get_all_categories = get_categories( $args ); if($sitepress && is_object($sitepress)){ add_filter('terms_clauses', array($sitepress, 'terms_clauses'), 10, 3); } $return_category = array(); foreach($get_all_categories as $category){ if ( $category->parent > 0 ) { $parent_category = get_term_by( 'id', $category->parent, 'product_cat' ); $return_category[$category->term_id] = $parent_category->name.' -> '.$category->name.' (#'.$category->term_id.')'; }else{ $return_category[$category->term_id] = $category->name.' (#'.$category->term_id.')'; } } return $return_category; } function conditionCheck($result, $package, $logic, $values, $package_support){ $or_result = false; $user_categories = $this->getCategoriesFromOrder($package, $package_support); $rule_categories = pisol_wpml_affsw_object($values, 'product_cat'); $intersect = array_intersect($rule_categories, $user_categories); if($logic == 'equal_to'){ if(count($intersect) > 0){ $or_result = true; }else{ $or_result = false; } }else{ if(count($intersect) == 0){ $or_result = true; }else{ $or_result = false; } } return $or_result; } function getCategoriesFromOrder($package, $package_support){ $package_support = 'package'; if($package_support == 'cart'){ $products = function_exists('WC') && is_object(WC()->cart) ? WC()->cart->get_cart() : []; }else{ $products = PISOL\EFRS\Package::get_products($package); } $user_products_categories = array(); foreach($products as $product){ if($product['variation_id'] != 0){ $product_obj = wc_get_product($product['product_id']); }else{ $product_obj = $product['data']; } $product_categories = $product_obj->get_category_ids(); foreach($product_categories as $product_category){ $user_products_categories[] = pisol_wpml_affsw_object($product_category, 'product_cat'); } } return $user_products_categories; } } new Pi_efrs_selection_rule_category_product(PI_EFRS_SELECTION_RULE_SLUG);