【问题标题】:Restricting cart items to be from the same product category in WooCommerce在 WooCommerce 中将购物车商品限制为来自同一产品类别
【发布时间】:2017-07-13 00:49:23
【问题描述】:

当购物车中添加了带有特殊产品类别“cat_x”的商品并显示一些不同的自定义通知时,我正在使用下面的代码从购物车中删除其他 WooCommerce 产品类别商品。代码来自this thread,运行良好:

add_action( 'woocommerce_check_cart_items', 'checking_cart_items' );
function checking_cart_items() {
    $special = false;
    $catx = 'cat_x';
    $number_of_items = sizeof( WC()->cart->get_cart() );

    if ( $number_of_items > 0 ) {

        // Loop through all cart products
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $item = $values['data'];
            $item_id = $item->id;

            // detecting if 'cat_x' item is in cart
            if ( has_term( $catx, 'product_cat', $item_id ) ) {
                if (!$special)
                    $special = true;
            }
        }

        // Re-loop through all cart products
        foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
            $item = $values['data'];
            $item_id = $item->id;

            if ( $special ) // there is a 'cat_x' item in cart
            { 
                if ( $number_of_items == 1 ) { // only one 'cat_x' item in cart
                    if ( empty( $notice ) )
                        $notice = '1';
                }
                if ( $number_of_items >= 2 ) { // 'cat_x' item + other categories items in cart
            // removing other categories items from cart
                    if ( !has_term( $catx, 'product_cat', $item_id ) ) {
                        WC()->cart->remove_cart_item( $cart_item_key ); // removing item from cart
                        if ( empty( $notice ) || $notice == '1' )
                            $notice = '2';
                    }
                }
            } else { // Only other categories items
                if ( empty( $notice ) )
                    $notice = '3';
            }
        }

        // Firing notices
        if ( $notice == '1' ) { // message for an 'cat_x' item only (alone)
            wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla one category X item in the cart</p>' ), 'success' );
        } elseif ( $notice == '2' ) { // message for an 'cat_x' item and other ones => removed other ones 
            wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla ther is already category X in the cart => Other category items has been removed</p>' ), 'error' );
        } elseif ( $notice == '3' ) { // message for other categories items (if needed)
            wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla NOT category X in the cart</p>' ), 'success' );
        }
    }
} 

条件函数 has_term() 也适用于类别数组,我尝试在该代码中设置类别数组而不是一个类别。 但它不起作用

但是,我的需求发生了变化:我不想让客户有可能从不同类别的购物车中选择商品。因此,购物车必须始终包含来自同一产品类别的商品。

有什么帮助吗?

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce categories cart


    【解决方案1】:

    在 woocommerce_add_to_cart_validation 过滤器挂钩中创建一个自定义函数将以更简单的方式完成这项工作,无需设置类别数组。

    因此您的代码将更快更紧凑。此外,您可以显示自定义通知以警告客户。

    如果购物车中有不同类别的商品,此代码将避免添加到购物车:

    add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation_callback', 10, 3 );
    function add_to_cart_validation_callback( $passed, $product_id, $quantity) {
        // HERE set your alert text message
        $message = __( 'MY ALERT MESSAGE.', 'woocommerce' );
        
        if( ! WC()->cart->is_empty() ) {
            // Get the product category terms for the current product
            $terms_slugs = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs'));
            
            // Loop through cart items
            foreach (WC()->cart->get_cart() as $cart_item ){
                if( ! has_term( $terms_slugs, 'product_cat', $cart_item['product_id'] )) {
                    $passed = false;
                    wc_add_notice( $message, 'error' );
                    break;
                }
            }
        }
        return $passed;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    此代码已经过测试并且可以正常工作


    将购物车商品限制为仅来自不同的产品类别:

    在函数中替换条件:

    if( ! has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) { 
    

    通过

    if( has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) {
    

    【讨论】:

      【解决方案2】:

      我刚刚尝试过,我以这种方式修复的数组出现语法错误:

      $terms_slugs = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'slugs'));
      

      product_cat 分心后只是一个逗号,但也许有人会需要它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-04
        • 2019-01-27
        • 2021-07-22
        • 2019-09-18
        • 2018-02-10
        • 2017-12-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多