Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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/2/joomla/2.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 在将产品添加到购物车(Woocommerce)之前检查产品类别/id_Php_Wordpress_Function_Woocommerce_Product - Fatal编程技术网

Php 在将产品添加到购物车(Woocommerce)之前检查产品类别/id

Php 在将产品添加到购物车(Woocommerce)之前检查产品类别/id,php,wordpress,function,woocommerce,product,Php,Wordpress,Function,Woocommerce,Product,我正在寻找一个必须做到这一点的函数,例如: 我有10个类别,其中3个是特别的,它们的名字是:“阿尔法”,“布拉沃”和“查理” 在函数中,我必须有一个类别ID为“Alpha”、“Bravo”和“Charlie”的数组 当用户单击“添加到购物车”时,函数必须检查购物车是否为空,如果购物车为空,函数必须重置名为$restricted\u item\u cart的全局变量 在此之后,函数必须检查我要添加到购物车的商品的产品类别是否包含在我放置类别“Alpha”、“Bravo”和“Charlie”的数组中

我正在寻找一个必须做到这一点的函数,例如:

我有10个类别,其中3个是特别的,它们的名字是:“阿尔法”,“布拉沃”和“查理”

在函数中,我必须有一个类别ID为“Alpha”、“Bravo”和“Charlie”的数组

  • 当用户单击“添加到购物车”时,函数必须检查购物车是否为空,如果购物车为空,函数必须重置名为$restricted\u item\u cart的全局变量
  • 在此之后,函数必须检查我要添加到购物车的商品的产品类别是否包含在我放置类别“Alpha”、“Bravo”和“Charlie”的数组中
  • 如果项目类别包含在数组中,则函数必须将产品id添加到全局变量$restricted\u item\u cart中
  • 你能帮我吗

    新编辑(新代码,我不是专业的php程序员,你能告诉我这是否正确吗?)


    $restricted_category=数组(11,22);该行用于什么?此数组必须包含所有考虑(受限)的产品类别此函数必须处理所有这些代码:是的,但您是否需要将项目附加到该数组中..或者这只是您的方法?是的,我需要将“受限”类别的所有类别ID附加到该数组中,这是因为我需要在一个时间内用一个产品来修改全局变量$restricted\u cart\u items。因此,我需要一个具有所有类别ID的数组来检查当前产品类别和受限类别
    function my_add_woo_cat_classes($classes) {
    
    global $restricted_cart_items ;
        if ( is_null( $cart )){
                $restricted_cart_items=array();
        }
    
    
    
    global $post;
    $terms = get_the_terms( $post->ID, 'product_cat' );
        foreach ($terms as $term) {
                $product_cat_id = $term->term_id;
                break;
        }
    
    $restricted_category= array(11,22);
    
    if (in_array( $product_cat_id, $restricted_category)){
        $current_product= $product->id;
        $restricted_cart_items=array($current_product);
    }
    
    return $restricted_cart_items;
    }