简体   繁体   English

WooCommerce:如果特定产品是购物车中的唯一商品,请清除购物车

[英]WooCommerce: If specific product is the only item in cart, clear cart

If users purchase a product on my site, I give them the option of adding a discounted product to their cart at checkout.如果用户在我的网站上购买产品,我会为他们提供在结账时将打折产品添加到购物车的选项。 However, users are able to remove the original product from cart and the discounted product still remains within the cart.但是,用户可以从购物车中删除原始产品,并且折扣产品仍保留在购物车中。

Is it possible to have it such that if the discounted product is the only one in the cart, then the cart should be empty?是否有可能这样,如果打折的产品是购物车中唯一的一个,那么购物车应该是空的? I haven't found a way to do this.我还没有找到办法做到这一点。

What I have tried我试过的

Currently, I use the following code from bekarice found on Github:目前,我使用Github上的 bekarice 的以下代码

/**
 * Renders a notice and prevents checkout if the cart
 * only contains products in a specific category
 */
function sv_wc_prevent_checkout_for_category() {
    
    // set the slug of the category for which we disallow checkout
    $category = 'clothing';
    
    // get the product category
    $product_cat = get_term_by( 'slug', $category, 'product_cat' );
    
    // sanity check to prevent fatals if the term doesn't exist
    if ( is_wp_error( $product_cat ) ) {
        return;
    }
    
    $category_name = '<a href="' . get_term_link( $category, 'product_cat' ) . '">' . $product_cat->name . '</a>';
    
    // check if this category is the only thing in the cart
    if ( sv_wc_is_category_alone_in_cart( $category ) ) {
        
        // render a notice to explain why checkout is blocked
        wc_add_notice( sprintf( 'Hi there! Looks like your cart only contains products from the %1$s category &ndash; you must purchase a product from another category to check out.', $category_name ), 'error' );
    }
}
add_action( 'woocommerce_check_cart_items', 'sv_wc_prevent_checkout_for_category' );


/**
 * Checks if a cart contains exclusively products in a given category
 * 
 * @param string $category the slug of the product category
 * @return bool - true if the cart only contains the given category
 */
function sv_wc_is_category_alone_in_cart( $category ) {
        
    // check each cart item for our category
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        
        // if a product is not in our category, bail out since we know the category is not alone
        if ( ! has_term( $category, 'product_cat', $cart_item['data']->id ) ) {
            return false;
        }
    }
        
    // if we're here, all items in the cart are in our category
    return true;
}

But I don't think it as good a method.但我不认为这是一个很好的方法。 Basically, it doesn't allow users to checkout if cart only contains items from a certain category.基本上,如果购物车仅包含某个类别的商品,它不允许用户结帐。

You can use the following code that will check on adding to cart your specific discounted product that cart is not empty and that it's not alone in cart, removing it from cart:您可以使用以下代码检查是否将您的特定折扣产品添加到购物车中,即购物车不是空的,并且它不是单独在购物车中,将其从购物车中删除:

// Add to cart validation for the discounted product
add_filter( 'woocommerce_add_to_cart_validation', 'check_specific_discounted_product', 10, 3 );
function check_specific_discounted_product( $passed, $product_id, $quantity ) {
    // Settings
    $discounted_product_id = 53;

    if( WC()->cart->is_empty() && $discounted_product_id == $product_id ) {
        wc_add_notice( __("This product can't be purchased alone."), 'notice' );
        return false;
    }

    return $passed;
}

// Removing the discounted product if it's alone in cart
add_action( 'woocommerce_before_calculate_totals', 'conditionally_remove_a_discounted_product' );
function conditionally_remove_a_discounted_product( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Settings
    $discounted_product_id = 53;

    // Initializing variables
    $discounted_item_key = false;

    // Loop through cart items (first loop)
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
        // When free productis is cart
        if ( in_array( $discounted_product_id, array($cart_item['product_id'], $cart_item['variation_id']) ) ) {
            $discounted_item_key = $cart_item_key;
        }
        // if any other product is in cart: EXIT
        else {
           return;
        }
    }
    // When the discounted product is alone in cart, remove it
    if( $discounted_item_key ) {
        // display notice on removal (optional)
        wc_clear_notices();
        wc_add_notice( __("The discounted product can't be purchased alone and has been removed."), 'notice' );

        $cart->remove_cart_item( $discounted_item_key ); // Remove
    }
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的functions.php 文件。 Tested and works.测试和工作。

暂无
暂无

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

相关问题 基于 Woocommerce 中特定产品类别购物车项目数量的购物车费用 - Cart fee based on specific product category cart item quantity in Woocommerce 在 WooCommerce 购物车中获取购物车项目的产品 ID - Get in WooCommerce cart the product ID of a cart item 为 WooCommerce 中特定产品类别的购物车项目自动添加产品 - Auto add a product for cart item from specific product categories in WooCommerce 设置购物车商品仅针对Woocommerce中的特定商品生成的销售价格 - Set cart item product generated sale price only for specific products in Woocommerce 仅保留购物车中特定产品类别中最后添加的项目 WooCommerce - Keep in cart only last added item from specific product category in WooCommerce 仅在特定页面上从 WooCommerce 购物车中删除特定产品 - Remove specific product from WooCommerce cart only on a specific page 在 Woocommerce 购物车结账和订单中禁用特定产品的项目名称链接 - Disable item name link for specific product in Woocommerce cart checkout and orders 更改Woocommerce中特定产品类别的购物车项目价格 - Change cart item prices for specific product categories in Woocommerce 根据Woocommerce中的特定产品属性值更改购物车商品价格 - Change cart item prices based on specific product attribute value in Woocommerce 显示特定产品类别的 WooCommerce 购物车项目简短描述 - Display WooCommerce cart item short description for a specific product categories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM