slug = $slug;
$this->condition = 'product';
add_filter("pi_".$this->slug."_condition", array($this, 'addRule'));
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_action( 'wp_ajax_pi_'.$this->slug.'_options_'.$this->condition, array( $this, 'search_product' ) );
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'=>__('Cart has Product','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(array(), $count,$this->condition, "multiple", null,'dynamic');
die;
}
function savedProducts($values){
$saved_products = array();
if(is_array($values)){
foreach($values as $value){
$product = wc_get_product($value);
if(is_wp_error($product) || !is_object($product)) continue;
$saved_products[$value] = $product->get_title().' (#'.$value.')';
}
}
return $saved_products;
}
function savedDropdown($html, $values, $count){
$html = Pi_efrs_selection_rule_main::createSelect($this->savedProducts($values), $count, $this->condition, "multiple", $values,'dynamic');
return $html;
}
public function search_product( $x = '', $post_types = array( 'product' ) ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
ob_start();
if(!isset($_GET['keyword'])) die;
$keyword = isset($_GET['keyword']) ? sanitize_text_field(wp_unslash($_GET['keyword'])) : "";
if ( empty( $keyword ) ) {
die();
}
$arg = array(
'post_status' => 'publish',
'post_type' => $post_types,
'posts_per_page' => 50,
's' => $keyword
);
$the_query = new WP_Query( $arg );
$found_products = array();
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$prd = wc_get_product( get_the_ID() );
if ( $prd->has_child() && $prd->is_type( 'variable' ) ) {
/** This is for the variable product */
/*
$product_children = $prd->get_children();
if ( count( $product_children ) ) {
foreach ( $product_children as $product_child ) {
if ( woocommerce_version_check() ) {
$product = array(
'id' => $product_child,
'text' => get_the_title( $product_child )
);
} else {
$child_wc = wc_get_product( $product_child );
$get_atts = $child_wc->get_variation_attributes();
$attr_name = array_values( $get_atts )[0];
$product = array(
'id' => $product_child,
'text' => get_the_title() . ' - ' . $attr_name
);
}
$found_products[] = $product;
}
}
*/
} else {
$product_id = get_the_ID();
$product_title = get_the_title().' (#'.$product_id.')';
$the_product = new WC_Product( $product_id );
if ( ! $the_product->is_in_stock() ) {
$product_title .= ' (Out of stock)';
}
$product = array( 'id' => $product_id, 'text' => $product_title );
$found_products[] = $product;
}
}
}
wp_send_json( $found_products );
die;
}
function conditionCheck($result, $package, $logic, $values, $package_support){
$or_result = false;
$user_products = $this->getProductsFromOrder($package, $package_support);
if(is_array($values)){
$rule_products = pisol_wpml_affsw_object($values,'product');
}else{
$rule_products = array();
}
$intersect = array_intersect($rule_products, $user_products);
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 getProductsFromOrder($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 = array();
foreach($products as $product){
$product_obj = $product['data'];
$user_products[] = pisol_wpml_affsw_object($product_obj->get_id(),'product');
}
return $user_products;
}
}
new Pi_efrs_selection_rule_product(PI_EFRS_SELECTION_RULE_SLUG);
/**
*
* @param string $version
*
* @return bool
*/
if ( ! function_exists( 'woocommerce_version_check' ) ) {
function woocommerce_version_check( $version = '3.0' ) {
global $woocommerce;
if ( version_compare( $woocommerce->version, $version, ">=" ) ) {
return true;
}
return false;
}
}