繁体   English   中英

自动将产品添加到购物车,部分 WooCommerce 产品类别除外

[英]Auto add product to cart except for some WooCommerce product categories

我正在使用自动为 WooCommerce 答案代码中特定产品类别的购物车项目添加产品,以自动将免费产品添加到购物车。 如果产品属于特定类别,则代码效果很好,但如果产品不在特定类别中,我需要添加产品。

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

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

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

任何帮助,将不胜感激!

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

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 文件。 测试和工作。

基于: 自动为 WooCommerce 中特定产品类别的购物车项目添加产品

可能能够从购物车钩子中钩入 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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM