【问题标题】:Auto add product to cart except for some WooCommerce product categories除某些 WooCommerce 产品类别外,自动将产品添加到购物车
【发布时间】:2020-10-15 23:36:57
【问题描述】:

我正在使用 Auto add a product for cart item from specific product categories in WooCommerce 答案代码自动将免费产品添加到购物车。如果产品属于特定类别,则代码效果很好,但如果产品不在特定类别中,我需要添加产品。

如果免费产品不在此编辑的特定类别中,我可以添加它:

if( **!** has_term( $required_categories, 'product_cat', $item['product_id'] ) ) {
    $matched_category = true;
}

但这不会在删除父产品时删除免费产品。

任何帮助将不胜感激!

【问题讨论】:

  • “父产品”是什么意思?这里哪里有父子关系?您的意思是说,一旦该特定类别中的任何 个产品被添加到购物车中,您就希望再次删除这个免费产品?
  • 当购物车中的唯一产品属于排除类别时,我希望删除免费产品。因此,在链接问题的示例代码中,我将使用 $excluded_categories 而不是 $required_categories。
  • 我想如果你保留上面显示的行,而不是在最后否定条件,if ( isset($saved_item_key) && $matched_category )(!在 $matched_category 删除之前)和elseif ( ! isset($saved_item_key) && ! $matched_category )( ! 在添加 $matched_category 之前。)你能试试看你得到什么结果吗?

标签: php wordpress woocommerce cart taxonomy-terms


【解决方案1】:

更新:以下是“自动将产品添加到购物车中所需的更改,特定定义的产品类别除外(如果购物车中包含混合类别,则不会删除自动添加的产品)

add_action( 'woocommerce_before_calculate_totals', 'auto_add_item_except_for_product_category', 10, 1 );
function auto_add_item_except_for_product_category( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Settings
    $except_terms  = array('t-shirts'); // Required product category(ies)
    $auto_added_id = 70; // Specific product to be added automatically

    $except_found  = false;
    $others_found  = false;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // Check for product category
        if( has_term( $except_terms, 'product_cat', $cart_item['product_id'] ) ) {
            $except_found = true;
        } else {
            $others_found = true;
        } 

        // Check if specific product is already auto added
        if( $cart_item['data']->get_id() == $auto_added_id ) {
            $auto_added_item_key = $cart_item_key; // keep cart item key
        }
    }

    // If auto added product is in cart with at least an item from a the defined product category only
    if ( isset($auto_added_item_key) && $except_found && ! $others_found ) {
        $cart->remove_cart_item( $auto_added_item_key ); // Remove specific product
    }
    // If there is at least an item from others product categories and the specific product is not in cart
    elseif ( ! isset($auto_added_item_key) && ! $except_found ) {
        $cart->add_to_cart( $auto_added_id ); // Add specific product
    }
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

基于:Auto add a product for cart item from specific product categories in WooCommerce

【讨论】:

  • 如果存在未排除类别中的产品,则添加已排除类别中的产品不应删除免费产品。
  • 另外,当我从未排除的类别中删除产品时,不会删除免费产品。只要存在未排除的类别中的产品,免费产品就可以与排除的产品一起存在。
  • 根据您引用的链接,我只是想避免在 $required_categories 变量中添加多个类别。因此,我试图仅列出一个被排除的类别,而不是列出 12 个必需的类别。如果我列出 12 个必需的类别,它就可以完美运行。很抱歉造成混乱。
  • 很遗憾没有。在任何一种情况下,免费产品都会被删除,如果您先添加排除的产品,那么当您添加其他类别的产品时,不会添加免费赠品。
【解决方案2】:

也许能够钩入 Woo 的从购物车中移除物品的钩子:

function remove_free_item() {
    if ( is_admin() ) {
        return;
    }
    
    $product_id = 'ID_OF_FREE_ITEM';
    $product_cart_id = WC()->cart->generate_cart_id( $product_id );
    $cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id );

    if ( $cart_item_key ) {
        WC()->cart->remove_cart_item( $cart_item_key );
    }
}


add_action( 'woocommerce_cart_item_removed', 'after_remove_product_from_cart', 10, 2 );
function after_remove_product_from_cart($removed_cart_item_key, $cart) {
    // removed item
    $line_item = $cart->removed_cart_contents[ $removed_cart_item_key ];

    // removed item product id
    $product_id = $line_item[ 'product_id' ];

    // might need to wrap this in some check depending on your case
    remove_free_item();
}

【讨论】:

  • 我很快就会尝试这段代码。感谢您的意见。
猜你喜欢
  • 1970-01-01
  • 2014-11-21
  • 2021-08-22
  • 2015-03-31
  • 2014-02-06
  • 1970-01-01
  • 2012-10-29
  • 1970-01-01
  • 2013-03-10
相关资源
最近更新 更多