首页 > 解决方案 > 如果还添加了特定产品,我如何从 WooCommerce 中的购物车中删除其他产品?

问题描述

我的 WooCommerce 网站正在销售 2 款限量版产品,为癌症研究筹集资金。一种是男鞋,另一种是女鞋。

因为这些产品将作为预售提供(它们要到 9 月才能发货)我想确保任何包含其中 1 只鞋子的订单不能在购物车中包含其他产品(因为这会导致部分执行的命令,这会引起大家的困惑)。

这是我想要的行为:

仅供参考,这两个产品的 WooCommerce 产品 ID 是:348455 和 348443。它们都属于名为“EMI”的产品类别,产品类别 ID 为 348。

这是我迄今为止编写的代码,基于我发现的 2017 年类似的 Stackoverflow 问题。我下面的代码的问题是,如果我先将普通产品添加到我的购物车,然后是其中一只限量版鞋,它将从我的购物车中删除限量版鞋,而不是普通产品。

如果我先添加限量版鞋,然后添加普通产品,它就可以正常工作。有人可以帮我找出问题所在吗?

add_action( 'woocommerce_add_to_cart', 'check_product_added_to_cart', 10, 6 );
function check_product_added_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {

    // Set HERE your targeted product IDs
    $target_product_id_one = 348455;
    $target_product_id_two = 348443;

    // Initialising some variables
    $has_item = false;
    $is_product_id = false;

    foreach( WC()->cart->get_cart() as $key => $item ){
        // Check if there are other products in the cart with the 2 limited edition shoes
        if( $item['product_id'] !== $target_product_id_one && $item['product_id'] !== $target_product_id_two ){
            $has_item = true;
            $key_to_remove = $key;
        }

        // Check if we add to cart the targeted product ID
        if( $product_id == $target_product_id_one || $product_id == $target_product_id_two ){
            $is_product_id = true;
        }
    }

    if( $has_item && $is_product_id ){
        WC()->cart->remove_cart_item($key_to_remove);

        // Optionaly displaying a notice for the removed item:
    wc_add_notice( __( 'Due to the presale, EMI products cannot be purchased with other products in the same order.', 'theme_domain' ), 'notice' );
    }
}

标签: phpwordpressfunctionwoocommerce

解决方案


我找到了一些适合你的代码。当第二件(或后续)物品添加到您的购物车时,它会检查产品是否在购物车中,如果特殊鞋是添加到购物车的第一件物品,则删除其他所有物品,如果是普通产品,则移除特殊鞋第一件事添加到购物车。

// Remove conditionally cart items based on a specific product (item)
add_action( 'woocommerce_before_calculate_totals', 'remove_cart_items_conditionally_a', 10, 1 );
function remove_cart_items_conditionally_a( $cart ) {
    // HERE define your specific product ID
    $specific_product_id = 348455; 

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart_items  = $cart->get_cart(); // Cart items array
    $items_count = count($cart_items); // Different cart items count

    // Continue if cart has at least 2 different cart items
    if ( $items_count < 2 )
        return;

    $last_item    = end($cart_items); // Last cart item data array
    $is_last_item = false; // Initializing

    // Check if the specific product is the last added item
    if ( in_array($specific_product_id, array( $last_item['product_id'], $last_item['variation_id'] ) ) ) {
        $is_last_item = true;
    }

    // Loop through cart items
    foreach ( $cart_items as $cart_item_key => $cart_item ) {
        // Remove all others cart items when specific product ID is --the last-- added to cart
        if ( ! in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) && $is_last_item ) {
            $cart->remove_cart_item( $cart_item_key );
            // Optionaly displaying a notice for the removed item:
            wc_add_notice( __( 'Due to the presale, AMI products cannot be purchased with other products in the same order.', 'theme_domain' ), 'notice' );
        }
        // Remove the specific item when its is not the last added to cart
        elseif ( in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) && ! $is_last_item ) {
            $cart->remove_cart_item( $cart_item_key );
            // Optionaly displaying a notice for the removed item:
            wc_add_notice( __( 'Due to the presale, AMI products cannot be purchased with other products in the same order.', 'theme_domain' ), 'notice' );
        }
    }
}

推荐阅读