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

时间:2020-07-25 14:19:36

标签: php wordpress woocommerce product cart

如果用户在我的网站上购买产品,我可以给他们选择在结帐时将折扣产品添加到购物车中的选项。但是,用户可以从购物车中删除原始产品,而打折的产品仍保留在购物车中。

是否有可能这样,如果打折的产品是购物车中唯一的产品,那么购物车应该是空的?我还没有找到一种方法。

我尝试过的事情

目前,我使用在Github上的the following code中的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;
}

但是我认为这不是一个好方法。基本上,它不允许用户结帐购物车是否仅包含特定类别的商品。

1 个答案:

答案 0 :(得分:3)

您可以使用以下代码检查将特定折扣产品添加到购物车中,即该购物车不为空并且不是单独存在于购物车中,然后将其从购物车中删除:

// 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
    }
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。