基于Woocommerce中特定产品变化的购物车商品折扣

基于Woocommerce中特定产品变化的购物车商品折扣

问题描述:

I’ve, on my DIY sell store, a product named ‘Reusable wet’ which come in different patterns and different packages.

  • 5 Reusable wet cost €10
  • 10 Reusable wet cost €18

Since they are a lot of different patterns, the customer could want 10 wet, but take two different packages of 5, to get two different patterns. Unfortunately, it would cost him 2 more euros, and I want to avoid this situation.

How could I detect this behaviour, and more precisely, where should I hook that?

I thought of putting it in the cart preview, maybe automatically add a reduction coupon if I detect these packages, but I’m not sure if it’s the most efficient way of doing it.

Any suggestions would help me.

在我的DIY商店里,我有一个名为“可重复使用的湿”的产品,它有不同的图案和不同的颜色 包装。 p>

  • 5可重复使用的湿成本€10 li>
  • 10可重复使用的湿成本€18 li> ul> \ n

    由于它们有很多不同的模式,客户可能需要10个湿,但需要两个不同的5个包,以获得两种不同的模式。 不幸的是,这将花费他2欧元,我想避免这种情况。 p>

    我怎么能检测到这种行为,更确切地说,我应该在哪里挂钩? p>

    我想把它放在购物车预览中,如果我检测到这些包,可能会自动添加减少优惠券,但我不确定这是否是最有效的方式。 p> \ n

    任何建议都会对我有所帮助。 p> div>

It seems that your product named "Reusable wet" is a variable product with multiple variations based on some product attributes.

So in your case, the discount can be applied in 2 different ways.

But first you will need to define the related product attribute SLUG that is involved in "Reusable wet" quantity pack and the related term NAME value for the "pack of 5" in the code.

If this seetings are not done in the right way, the code will not work.

1) Changing related item prices (the best way):

Here we set a discounted unit price of 9 (18 / 2 = 9) when there is 2 related items or more in cart.

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

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

    // HERE your product attribute slug for "Reusable wet" (without "pa_")
    $product_attr = 'reusable-wet';
    $product_attr = 'color';

    // HERE set the corresponding (value) term NAME for "5 Reusable wet"
    $term_slug = '5 reusable wet';
    $term_slug = 'Black';

    // HERE set the discounted price for each cart item with "5 Reusable wet" when they are 2 or more
    $discounted_price = 9; // (18 / 2 = 9)

    $five_rw_count = 0; // Initializing variable

    // 1st Loop: count cart items with "5 Reusable wet"
    foreach ( $cart->get_cart() as $cart_item ){
        if( $cart_item['data']->get_attribute( $product_attr ) == $term_slug ){
            $five_rw_count += $cart_item['quantity'];
        }
    }

    // Continue if there is at least 2 units of "5 Reusable wet"
    if( $five_rw_count < 2 ) return; // Exit

    // 2nd Loop: set the discounted price for cart items with "5 Reusable wet"
    foreach ( $cart->get_cart() as $cart_item ){
        if( $cart_item['data']->get_attribute( $product_attr ) == $term_slug ){
            $cart_item['data']->set_price($discounted_price); // Set the discounted price
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works


2) The coupon way (not very convenient for many logical reasons):

Here you will have to set the coupon code. This coupon settings should be like:

enter image description here

Where you will set all related product variations in the "products" field.

Once done you will set the coupon name (in lowercase) in the following code:

add_action( 'woocommerce_before_calculate_totals', 'conditionally_auto_add_coupon', 30, 1 );
function conditionally_auto_add_coupon( $cart ) {
    if ( is_admin() && !defined('DOING_AJAX') ) return; // Exit

    // HERE your product attribute slug for "Reusable wet" (without "pa_")
    $product_attr = 'reusable-wet';

    // HERE set the corresponding (value) term NAME for "5 Reusable wet"
    $term_slug = '5 reusable wet';

    // HERE set the coupon code (in lowercase)
    $coupon_code = 'multiplefive';

    $five_rw_count = 0; // Initializing variable

    // 1st Loop: count cart items with "5 Reusable wet"
    foreach ( $cart->get_cart() as $cart_item ){
        if( $cart_item['data']->get_attribute( $product_attr ) == $term_slug ){
            $five_rw_count++; // Increasing count
        }
    }

    // Apply the coupon if there is at least 2 units of "5 Reusable wet"
    if ( ! $cart->has_discount( $coupon_code ) && $five_rw_count >= 2 ) {
        $cart->add_discount( $coupon_code );  
    } 
    // Remove the coupon if there is at less than 2 units of "5 Reusable wet"
    elseif  ( $cart->has_discount( $coupon_code ) && $five_rw_count < 2 ) {
        $cart->remove_coupon( $coupon_code );
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works


Related: Auto apply or remove a coupon in Woocommerce cart for a specific product id