WooCommerce Shopping Cart Quantity Base Discount

In WooCommerce, how do I set a discount on a cart based on the total number of items in my cart?

For instance:

  • 1 to 4 items - no discount
  • 5 to 10 items - 5%
  • From 11 to 15 items - 10%
  • From 16 to 20 items - 15%
  • From 21 to 25 items - 20%
  • From 26 to 30 items - 25%

I searched the internet but did not find any solutions or plugins available.

Thanks.

+5
source share
1 answer

You can use the negative cart fee to get a discount. Then you add your conditions and calculations to a regular function connected to woocommerce_cart_calculate_fees , this way:

 ## Tested and works on WooCommerce 2.6.x and 3.0+ add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 ); function wc_cart_quantity_discount( $cart_object ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; ## -------------- DEFINIG VARIABLES ------------- ## $discount = 0; $cart_item_count = $cart_object->get_cart_contents_count(); $cart_total_excl_tax = $cart_object->subtotal_ex_tax; ## ----------- CONDITIONAL PERCENTAGE ----------- ## if( $cart_item_count <= 4 ) $percent = 0; elseif( $cart_item_count >= 5 && $cart_item_count <= 10 ) $percent = 5; elseif( $cart_item_count > 10 && $cart_item_count <= 15 ) $percent = 10; elseif( $cart_item_count > 15 && $cart_item_count <= 20 ) $percent = 15; elseif( $cart_item_count > 20 && $cart_item_count <= 25 ) $percent = 20; elseif( $cart_item_count > 25 ) $percent = 25; ## ------------------ CALCULATION ---------------- ## $discount -= ($cart_total_excl_tax / 100) * $percent; ## ---- APPLYING CALCULATED DISCOUNT TAXABLE ---- ## if( $percent > 0 ) $cart_object->add_fee( __( "Quantity discount $percent%", "woocommerce" ), $discount, true); } 

The code goes in the function.php file of your active child theme (or theme), as well as in any plug-in file.

Tested and works on WooCommerce 2.6.x and 3.0 +

+3
source

Source: https://habr.com/ru/post/1269387/


All Articles