Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅当存在特定类别的产品时,才将产品添加到购物篮中_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 仅当存在特定类别的产品时,才将产品添加到购物篮中

Php 仅当存在特定类别的产品时,才将产品添加到购物篮中,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我试图通过ACF字段将产品添加到我的购物篮中,并且有一个工作设置。然而,我试图限制产品仅在属于特定类别的产品在篮子中时出现 下面的代码在woocommerce.php中。如何对特定产品类别进行限制 function add_free_product_to_cart($item_key, $product_id, $quantity){ $product_ids = []; $current_date = strtotime(date('Y-m-d H:i:s')); if(have_row

我试图通过ACF字段将产品添加到我的购物篮中,并且有一个工作设置。然而,我试图限制产品仅在属于特定类别的产品在篮子中时出现

下面的代码在woocommerce.php中。如何对特定产品类别进行限制

function add_free_product_to_cart($item_key, $product_id, $quantity){

$product_ids = [];

$current_date = strtotime(date('Y-m-d H:i:s'));

if(have_rows('theme_free_products', 'options')){
    while(have_rows('theme_free_products', 'options')){
        the_row();
        $start_date = get_sub_field('start_date');
        $end_date = get_sub_field('end_date');
        if(strtotime($start_date) <= $current_date && strtotime($end_date) > $current_date){
            $product_id = get_sub_field('product', false);
            $product_ids[] = $product_id;
        }
    }
}


if(!empty($product_ids)){
    foreach($product_ids as $product_id){
        $found = false;
        if(sizeof(WC()->cart->get_cart()) > 0){
            foreach(WC()->cart->get_cart() as $cart_item_key => $values){
                $_product = $values['data'];

                if($_product->get_id() == $product_id){
                    $found = true;
                }
            }

        }

        if(!$found){
            WC()->cart->add_to_cart($product_id);
        }
    }
}

$warrantyProduct = get_field("warranty_product", "option",false);
if($product_id != $warrantyProduct && !get_field('product_has_no_warranty_product', $product_id)){
    $terms = get_the_terms($product_id, 'product_cat');

    foreach($terms as $term){
        $categoryPossibleToAddWarranty = get_field("category_possible_to_add_warranty", "product_cat_" . $term->term_id);
        if($categoryPossibleToAddWarranty){

            WC()->cart->add_to_cart($warrantyProduct, $quantity, 0,[], ['connect_to_id' => $item_key]);

            //$cart_item_data

            break;
        }
    }
}

}
add_action('woocommerce_add_to_cart', 'add_free_product_to_cart', 5, 3);
函数将免费产品添加到购物车($item\u key、$product\u id、$quantity){
$product_ids=[];
$current_date=strottime(日期('Y-m-d H:i:s');
如果(有行('theme\u free\u products'、'options')){
while(有行('theme\u free\u products'、'options')){
_行();
$start_date=获取子字段(“start_date”);
$end_date=获取子字段(“end_date”);
if(标准时间($开始日期)$当前日期){
$product\U id=获取子字段('product',false);
$product\U id[]=$product\U id;
}
}
}
如果(!空($product_ID)){
foreach($product\U id作为$product\U id){
$found=false;
如果(sizeof(WC()->cart->get_cart())>0){
foreach(WC()->cart->get_cart()作为$cart\u item\u key=>$value){
$_product=$values['data'];
如果($\u product->get\u id()==$product\u id){
$found=true;
}
}
}
如果(!$found){
WC()->购物车->将购物车添加到购物车($product\U id);
}
}
}
$保修产品=获取字段(“保修产品”、“选项”,错误);
if($product\U id!=$PRODUCTIONPRODUCT&&!get\U字段($product\U没有保修\U product',$product\U id)){
$terms=获取术语($product_id,'product_cat');
foreach($terms作为$term){
$categoryPossibleToAddWarranty=获取\字段(“类别\可能\添加\保修”、“产品\类别\保修”)。$term->term\ id);
如果($categoryPossibleToadd保修){
WC()->cart->add_to_cart($warrantyProduct,$quantity,0,[],['connect_to_id'=>$item_key]);
//$cart\u项目\u数据
打破
}
}
}
}
添加行动(“商业添加到购物车”,“免费产品添加到购物车”,5,3);
$category=get\u term\u by('name','warranty','category')帮助?$category=get\u term\u by('name','warranty','category')帮助?