【问题标题】:Limit WooCommerce cart based on child product categories根据子产品类别限制 WooCommerce 购物车
【发布时间】:2021-04-07 19:18:36
【问题描述】:

我希望使用 WooCommerce 的添加到购物车验证功能来限制特定类别的操作。

我有两个父类别:Cat A 和 Cat B。

对于 Cat A,它应该是不受限制的。因此,它可以随时添加到购物车。

对于 Cat B,我有不同的子类别。我希望对其进行限制,以便任何时候购物车中只能存在 Cat B 的一个子类别。如果有人尝试将第二个 Cat B 子类别产品添加到购物车中,而购物车中已经存在冲突的子猫,我希望显示一条错误消息。

子类别将不断变化,因此不能通过子猫 ID 进行查询 - 必须通过父类别来完成。也可以选择将所有 Cat B 子类别设为父类别,但我仍然需要将 Cat A 排除在限制之外。

基于Allow only one product per product category in cart 答案代码,这是我目前所拥有的,试图使购物车循环仅在添加的产品不是来自 Cat A 时才运行:

add_filter( 'woocommerce_add_to_cart_validation', 'custom_checking_product_added_to_cart', 10, 3 );
function custom_checking_product_added_to_cart( $passed, $product_id, $quantity) {

    // Getting the product categories slugs in an array for the current product
    $product_cats_object = get_the_terms( $product_id, 'product_cat' );
    foreach($product_cats_object as $obj_prod_cat)
        $product_cats[] = $obj_prod_cat->slug;

    if ( ! $product_cats['cat-a-slug']) {
    
    // Iterating through each cart item
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){

        // When the product category of the current product does not match with a cart item
        if( ! has_term( $product_cats, 'product_cat', $cart_item['product_id'] ))
        {
            // Don't add 
            $passed = false;
            
            // Displaying a message
            wc_add_notice( 'Only one product from a category is allowed in cart', 'error' );

            // We stop the loop
            break;
        }
    }
    }
    return $passed;
}

这满足了我对 Cat B 产品的要求,但也限制了我不想要的 Cat A。

虽然有类似的问题对此有可靠的答案,但我还没有找到一个可以解决我的问题的问题。我似乎无法让它忽略 Cat A 以使其不受限制,或者正确读取子类别。

【问题讨论】:

  • 很接近,但没有。我将在每个 Cat B 子项中添加多个需要添加的项目。我已经用我现在拥有的代码更新了我的问题。我认为最干净的方法是仅当添加的产品不在 Cat A 中时才尝试让它在购物车中循环,但我似乎无法让它工作。
  • 向我们展示你到目前为止的尝试!
  • 刚刚添加,复制粘贴稍等!感谢您的帮助 =)
  • Cat B 会被修复吗?

标签: php wordpress woocommerce


【解决方案1】:

如果你的代码按照 B 类的逻辑,你可以添加这个控件总是允许将属于 A 类的产品添加到购物车

// if the product belongs to category A allows the addition of the product to the cart
if ( has_term( 'cat-a-slug', 'product_cat', $product_id ) ) {
    return $passed;
}

所以完整的功能是:

add_filter( 'woocommerce_add_to_cart_validation', 'custom_checking_product_added_to_cart', 10, 3 );
function custom_checking_product_added_to_cart( $passed, $product_id, $quantity) {

    // if the product belongs to category A allows the addition of the product to the cart
    if ( has_term( 'cat-a-slug', 'product_cat', $product_id ) ) {
        return $passed;
    }

    // Getting the product categories slugs in an array for the current product
    $product_cats_object = get_the_terms( $product_id, 'product_cat' );
    foreach ( $product_cats_object as $obj_prod_cat ) {
        $product_cats[] = $obj_prod_cat->slug;
    }

    // if the product belongs to category B
    if ( in_array( 'cat-b-slug', $product_cats ) ) {
        // Iterating through each cart item
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

            // When the product category of the current product does not match with a cart item
            if ( ! has_term( $product_cats, 'product_cat', $cart_item['product_id'] ) ) {
                // Don't add 
                $passed = false;
                
                // Displaying a message
                wc_add_notice( 'Only one product from a category is allowed in cart', 'error' );

                // We stop the loop
                break;
            }
        }
    }

    return $passed;
}

我没有测试过代码,但它应该可以工作。将其添加到您的活动主题的 functions.php 中。

【讨论】:

  • 这很接近,但如果 Cat A 已经在购物车中,它不允许我添加 Cat B 的产品,或者添加来自同一 Cat B 子类别的多个项目...
猜你喜欢
  • 2020-01-04
  • 2014-04-29
  • 2017-07-13
  • 1970-01-01
  • 2015-12-09
  • 2017-03-26
  • 2018-10-04
  • 1970-01-01
  • 2022-11-18
相关资源
最近更新 更多