WooCommerce折扣:买一送一50%的折扣

时间:2017-09-17 13:10:18

标签: php wordpress woocommerce cart discount

我希望为特定的可变产品设置特定的折扣,如果客户购买一种产品,他们可以获得另一种(相同)50%的折扣(买一送五折50%)。我尝试了很多折扣插件,购买我发现的最接近的插件:

  • WooCommerce的定价优惠

  • WooCommerce所有折扣精简版

  • WooCommerce扩展优惠券功能

通过使用这些插件,我可以设置每个产品的小计或折扣折扣,但不是我想要的(买1送1)。还有其他一些我不想去的专业插件。

是否可以在不购买插件的情况下实现?

谢谢

找到类似的东西 https://www.fldtrace.com/buy-3-get-1-free-coupon-woocommerce

1 个答案:

答案 0 :(得分:2)

更新 (与您的评论相关)

此版本将针对此定义的变量产品在购物车中的所有产品变体进行全局工作:

add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1 );
function add_custom_discount_2nd_at_50( $wc_cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $discount = 0;
    $items_prices = array();

    // Set HERE your targeted variable product ID
    $targeted_product_id = 40;

    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $targeted_product_id ){
            $qty = intval( $cart_item['quantity'] );
            for( $i = 0; $i < $qty; $i++ )
                $items_prices[] = floatval( $cart_item['data']->get_price());
        }
    }
    $count_items_prices = count($items_prices);
    if( $count_items_prices > 1 ) foreach( $items_prices as $key => $price )
        if( $key % 2 == 1 ) $discount -= number_format($price / 2, 2 );

    if( $discount != 0 ){
        // Displaying a custom notice (optional)
        wc_clear_notices();
        wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');

        // The discount
        $wc_cart->add_fee( 'Discount 2nd at 50%', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

此代码在Woocommerce 3+上进行测试并正常运行。

原始回答:

对于特定的变量产品ID,有很多方法可以在第二项上添加50%的自定义折扣。下面我使用add_fee()方法,使用负值(因此它会增加折扣)
可选择显示自定义通知:

add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1 );
function add_custom_discount_2nd_at_50( $wc_cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $discount = 0;

    // Set HERE your targeted variable product ID
    $targeted_product_id = 40;

    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $targeted_product_id ){
            $price = $cart_item['data']->get_price();
            $quantity = intval( $cart_item['quantity'] );
            for( $i = 1, $j = 0; $i <= $quantity; $i++ ){
                if( $i % 2 == 0 &&  $quantity > 1 ) $j++;
            }
            if( $quantity > 1 ) number_format($discount -= $price * $j / 2, 2 );
        }
    }
    if( $discount != 0 ){
        // Displaying a custom notice (optional)
        wc_clear_notices();
        wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');

        $wc_cart->add_fee( 'Discount 2nd at 50%', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

此代码在Woocommerce 3+上进行测试并正常运行。