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

Cart item discount based on specific product variation in Woocommerce

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

由于它们有很多不同的图案,客户可能想要 10 个湿的,但需要两个不同的包装,每包 5 个,以获得两种不同的图案。不幸的是,他会多花2欧元,我想避免这种情况。

我怎样才能检测到这种行为,更准确地说,我应该在哪里挂钩?

我想把它放在购物车预览中,如果我检测到这些包裹可能会自动添加一个减价券,但我不确定这是不是最有效的方法。

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

您的名为 "Reusable wet" 的产品似乎是一个可变产品,根据某些产品属性具有多种变体。

所以在您的情况下,可以通过两种不同的方式应用折扣

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) 更改相关商品价格 (最佳方式):

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

代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作


2) 优惠券方式 (逻辑上很多原因不是很方便):

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

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

完成后,您将在以下代码中设置优惠券名称(小写):

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 );
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作


相关: