Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - WooCommerce discount: buy one get one 50% off

I wish to set up a specific discount on a particular variable products, if customer buys one product they get the another(same) on 50% discount(Buy one get another for 50% off). I've tried many discount plugins buy the closest that I have found are:

  • Pricing Deals for WooCommerce

  • WooCommerce All Discounts Lite

  • WooCommerce Extended Coupon Features

By using these plugins I was able to setup discount on subtotal or discount on a each product but not exactly what I am looking for(Buy 1 get 1 off). There are other pro plugins I don't want to go for it.

Is it possible to achieve without purchasing a plugin?

Thanks

Found something similar https://www.fldtrace.com/buy-3-get-1-free-coupon-woocommerce

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update (related to your comments)

This version will work globally on all product variations in the cart for this defined variable product:

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

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested on Woocommerce 3+ and works.


Original answer:

There is many ways to do add a custom discount of 50% on the 2nd item for a specific variable product ID. Below I am using add_fee() method with a negative value (so it adds a discount).
Optionally it will display a custom notice:

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

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested on Woocommerce 3+ and works.

Related: WooCommerce discount: buy one get one 50% off with a notice


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...