Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.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_Cart_Custom Taxonomy - Fatal编程技术网

Php 仅从一个自定义分类法限制购物车中的电子商务产品

Php 仅从一个自定义分类法限制购物车中的电子商务产品,php,wordpress,woocommerce,cart,custom-taxonomy,Php,Wordpress,Woocommerce,Cart,Custom Taxonomy,我试图限制我的Woocommerce商店的客户一次只能从一个“供应商”订购产品。我用一种称为“供应商”的自定义分类法定义“供应商”。我尝试的代码只是出于某种原因限制了一切 function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) { // If passed if ( $passed ) {

我试图限制我的Woocommerce商店的客户一次只能从一个“供应商”订购产品。我用一种称为“供应商”的自定义分类法定义“供应商”。我尝试的代码只是出于某种原因限制了一切

function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// If passed
if ( $passed ) {
    
    // If cart is NOT empty when a product is added     
    if ( !WC()->cart->is_empty() ) {
        
        // Set vars
        $current_product_tag_ids = array();
        $in_cart_product_tag_ids = array();
        
        // Get current product categories via product_id
        $current_product_tag_ids = wc_get_product_term_ids( $product_id, 'supplier' );

        // Loop through cart items checking for product categories
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            // Get product categories from product in cart via cart item product id
            $in_cart_product_tag_ids = array_merge( $in_cart_product_tag_ids, wc_get_product_term_ids( $cart_item['product_id'], 'product_cat' ) );
        }
        
        // Removes duplicate values
        $in_cart_product_tag_ids = array_unique( $in_cart_product_tag_ids, SORT_NUMERIC );
        
        // Compare
        $compare = array_diff( $current_product_tag_ids, $in_cart_product_tag_ids );
        
        // Result is NOT empty
        if ( !empty ( $compare ) ) {
            wc_add_notice( 'This product is with a different supplier. Please only order from 1 supplier at a time.', 'error' );
            $passed = false;
        }
    }
}

return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
我并没有试图限制每个供应商只能订购一个产品,我只是试图限制他们,让他们每次只能订购一个供应商的产品。例如,一旦他们将供应商“供应商1”的产品添加到篮子中,他们将无法添加除“供应商1”之外的任何其他供应商的产品


我上一篇文章试图使用类别而不是自定义分类法,但我需要它们分开,这样我们就不局限于类别。此帖子可在此处找到:

以下内容仅允许从一个“供应商”向购物车添加物品:

现在,为了确保客户无法在购物车中与不同的供应商结账,您可以添加以下内容:

// To be sure (avoiding checkout)
add_action( 'woocommerce_check_cart_items', 'only_one_supplier_by_order_check' );
function only_one_supplier_by_order_check() {
    $taxonomy   = 'supplier';
    $term_names = []; // Initializing

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $terms = wp_get_post_terms( $cart_item['product_id'], $taxonomy );
        if( ! empty($terms) ) {
            $term  = reset($terms);
            
            $term_names[$term->term_id] = $term->name;  
        }
    }

    // Allow only one supplier in cart (avoid checkout for more than one
    if( count( $term_names ) > 1 ) {

        // Displaying a custom notice
        wc_add_notice( __('Only items from one supplier at the time are allowed in cart'), 'error' );
    }
}

代码进入活动子主题(或活动主题)的functions.php文件。经过测试,工作正常。

工作正常。谢谢你,伙计!
// To be sure (avoiding checkout)
add_action( 'woocommerce_check_cart_items', 'only_one_supplier_by_order_check' );
function only_one_supplier_by_order_check() {
    $taxonomy   = 'supplier';
    $term_names = []; // Initializing

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $terms = wp_get_post_terms( $cart_item['product_id'], $taxonomy );
        if( ! empty($terms) ) {
            $term  = reset($terms);
            
            $term_names[$term->term_id] = $term->name;  
        }
    }

    // Allow only one supplier in cart (avoid checkout for more than one
    if( count( $term_names ) > 1 ) {

        // Displaying a custom notice
        wc_add_notice( __('Only items from one supplier at the time are allowed in cart'), 'error' );
    }
}