Tips / Recommendations

Percentage Discount on Whole Cart Without Coupon in WooCommerce

Fixed Percentage Discount on Whole Cart in WooCommerce
So, let’s start with the simplest discount – fixed. In it, the discount will be applied to the entire basket as a result, without unnecessary actions on the part of the user.

Discount script:

function woo_discount_total(WC_Cart $cart) {

	if(is_admin() && !defined('DOING_AJAX')) {
	
		return;
		
	}

	$discount = $cart->subtotal * 0.05; // 0.05 - это 5%
	
	$cart->add_fee('Фиксированная скидка в 5% ', -$discount);

}

add_action('woocommerce_cart_calculate_fees' , 'woo_discount_total');

You paste it at the bottom of your functions.php file, which is located in your theme folder.

Fixed (applied when there are specific items in the cart) percentage discount on the entire cart in WooCommerce
The second option for a fixed discount is a discount that is applied depending on which products (or bundles of products) are in the cart.

A script for applying a discount when there is a specific item in the cart:

function woo_discount_total(WC_Cart $cart) {

	if(is_admin() && !defined('DOING_AJAX')) {
	
		return;
		
	}

	$array_product_id = [];

	foreach($cart->get_cart() as $cart_item) {

		$array_product_id[] = $cart_item['product_id'];
	
	}

	if(in_array(6, $array_product_id)) { // Если в корзине есть товар с ID = 6

		$discount = $cart->subtotal * 0.05; // 0.05 - это 5%

		$cart->add_fee('Фиксированная скидка в 5% за выбор акционного товара ', -$discount);

	}

}

add_action('woocommerce_cart_calculate_fees' , 'woo_discount_total');

A script for applying a discount when there is one of the items in the cart:


function woo_discount_total(WC_Cart $cart) {

	if(is_admin() && !defined('DOING_AJAX')) {
	
		return;
		
	}

	$array_product_id = [];

	foreach($cart->get_cart() as $cart_item) {

		$array_product_id[] = $cart_item['product_id'];
	
	}

	if(in_array(6, $array_product_id) || in_array(7, $array_product_id)) { // Если в корзине есть товар с ID = 6 или ID = 7

		$discount = $cart->subtotal * 0.05; // 0.05 - это 5%

		$cart->add_fee('Фиксированная скидка в 5% за выбор акционного товара ', -$discount);

	}

}

add_action('woocommerce_cart_calculate_fees' , 'woo_discount_total');

Script for applying discounts for specific items in the cart:


function woo_discount_total(WC_Cart $cart) {

	if(is_admin() && !defined('DOING_AJAX')) {
	
		return;
		
	}

	$array_product_id = [];

	foreach($cart->get_cart() as $cart_item) {

		$array_product_id[] = $cart_item['product_id'];
	
	}

	if(in_array(6, $array_product_id) && in_array(7, $array_product_id)) { // Если в корзине есть товары с ID = 6 и ID = 7

		$discount = $cart->subtotal * 0.05; // 0.05 - это 5%

		$cart->add_fee('Фиксированная скидка в 5% за выбор акционных товаров ', -$discount);

	}

}

add_action('woocommerce_cart_calculate_fees' , 'woo_discount_total');

The one you need, you paste at the bottom of the functions.php file, which is located in your theme folder. If you are using variable products, then “product_id” in the code should be replaced with “variation_id”.

Fixed (applied when there are products from specific categories in the cart) percentage discount on the entire cart in WooCommerce
Here, as you already understood, unlike the previous option, for applying a discount, it is not the product itself that is checked, but the category in which it is located.

A script for applying a discount when there is an item in the cart from a specific category


function woo_discount_total(WC_Cart $cart) {

	if(is_admin() && !defined('DOING_AJAX')) {
	
		return;
		
	}

	foreach($cart->get_cart() as $cart_item) {

		if(has_term(16, 'product_cat', $cart_item['product_id'])) { // Если в корзине есть товар из категории с ID = 16

			$discount = $cart->subtotal * 0.05; // 0.05 - это 5%

			$cart->add_fee('Фиксированная скидка в 5% за выбор товара из акционной категории ', -$discount);

			break;

		}
	
	}

}

add_action('woocommerce_cart_calculate_fees' , 'woo_discount_total');

A script for applying a discount if there is an item in the basket from one category:


function woo_discount_total(WC_Cart $cart) {

	if(is_admin() && !defined('DOING_AJAX')) {
	
		return;
		
	}

	foreach($cart->get_cart() as $cart_item) {

		if(has_term([16, 23], 'product_cat', $cart_item['product_id'])) { // Если в корзине есть товар из категории с ID = 16 или ID = 23

			$discount = $cart->subtotal * 0.05; // 0.05 - это 5%

			$cart->add_fee('Фиксированная скидка в 5% за выбор товара из акционной категории ', -$discount);

			break;

		}
	
	}

}

add_action('woocommerce_cart_calculate_fees' , 'woo_discount_total');

A script for applying a discount when there is an item in the cart from specific categories:


function woo_discount_total(WC_Cart $cart) {

	if(is_admin() && !defined('DOING_AJAX')) {
	
		return;
		
	}

	foreach($cart->get_cart() as $cart_item) {

		if(has_term(16, 'product_cat', $cart_item['product_id']) && has_term(23, 'product_cat', $cart_item['product_id'])) { // Если в корзине есть товар из категории с ID = 16 и ID = 23

			$discount = $cart->subtotal * 0.05; // 0.05 - это 5%

			$cart->add_fee('Фиксированная скидка в 5% за выбор товара из акционных категорий ', -$discount);

			break;

		}
	
	}

}

add_action('woocommerce_cart_calculate_fees' , 'woo_discount_total');

A script for applying a discount when there are goods in the basket from specific categories:


function woo_discount_total(WC_Cart $cart) {

	if(is_admin() && !defined('DOING_AJAX')) {
	
		return;
		
	}

	$one_category = false; // Товар из первой категории
	$two_category = false; // Товар из второй категории

	foreach($cart->get_cart() as $cart_item) {

		if(has_term(16, 'product_cat', $cart_item['product_id'])) { // Если в корзине есть товар из категории с ID = 16

			$one_category = true;

		}

		if(has_term(23, 'product_cat', $cart_item['product_id'])) { // Если в корзине есть товар из категории с ID = 23

			$two_category = true;

		}
	
	}

	if($one_category && $two_category) { // Если в корзине есть товары из категорий с ID = 16 и ID = 23

		$discount = $cart->subtotal * 0.05; // 0.05 - это 5%

		$cart->add_fee('Фиксированная скидка в 5% за выбор товаров из акционных категорий ', -$discount);

	}

}

add_action('woocommerce_cart_calculate_fees' , 'woo_discount_total');

The one you need, you insert all the same at the bottom of the functions.php file, which is located in your theme folder.

Fixed (depending on the selected shipping method) percentage discount on the entire cart in WooCommerce
In addition to the fact that we can tie the discount to a specific product or, for example, its category, it is just as easy to organize a discount for the selected delivery method.

A script for applying a discount when choosing a specific delivery method:


function woo_discount_total(WC_Cart $cart) {

	if(is_admin() && !defined('DOING_AJAX')) {
	
		return;
		
	}

	global $woocommerce;

	$delivery_name = '';
	$delivery_selected = [];
	$available_methods = $woocommerce->shipping->get_packages();

	if(isset($woocommerce->session)) {

		$delivery_selected = $woocommerce->session->get('chosen_shipping_methods');

	}

	foreach($available_methods as $method) {
		
		foreach($delivery_selected as $delivery) {
			
			if(isset($method['rates'][$delivery])) {

				$delivery_name = $method['rates'][$delivery]->label;

				break;

			}
		
		}

	}

	if($delivery_name == 'Самовывоз') {

		$discount = $cart->subtotal * 0.05; // 0.05 - это 5%

		$cart->add_fee('Фиксированная скидка в 5% за самовывоз ', -$discount);

	}

}

add_action('woocommerce_cart_calculate_fees' , 'woo_discount_total');

A script for applying a discount when choosing one of the specified delivery methods:


function woo_discount_total(WC_Cart $cart) {

	if(is_admin() && !defined('DOING_AJAX')) {
	
		return;
		
	}

	global $woocommerce;

	$delivery_name = '';
	$delivery_selected = [];
	$available_methods = $woocommerce->shipping->get_packages();

	if(isset($woocommerce->session)) {

		$delivery_selected = $woocommerce->session->get('chosen_shipping_methods');

	}

	foreach($available_methods as $method) {
		
		foreach($delivery_selected as $delivery) {
			
			if(isset($method['rates'][$delivery])) {

				$delivery_name = $method['rates'][$delivery]->label;

				break;

			}
		
		}

	}

	if($delivery_name == 'Самовывоз' || $delivery_name == 'СДЭК') {

		$discount = $cart->subtotal * 0.05; // 0.05 - это 5%

		$cart->add_fee('Фиксированная скидка в 5% за выбранный способ доставки ', -$discount);

	}

}

add_action('woocommerce_cart_calculate_fees' , 'woo_discount_total');

The one you need, you paste at the bottom of the functions.php file, which is located in your theme folder.

Dynamic (variable depending on the total) percentage discount for the whole basket in WooCommerce
Unlike the aforementioned options with a fixed discount, the task is a little more complicated here. We will tie our discount to a price range. And, for example, when a person has ordered goods in the amount from 1000 to 10,000, we will give him one discount, and higher – another.

The implementation will look like this:


function woo_discount_total(WC_Cart $cart) {

	if(is_admin() && !defined('DOING_AJAX')) {
	
		return;
		
	}

	$woo_current_price = $cart->subtotal; // Текущая итоговая сумма
	
	if($woo_current_price >= 1000 && $woo_current_price <= 10000) {
	
		$discount = $cart->subtotal * 0.03; // 0.03 - это 3%
		$cart->add_fee('Скидка в 3% за заказ на сумму от 1 000 до 10 000 рублей ', -$discount);
		
	} elseif($woo_current_price > 10000) {
	
		$discount = $cart->subtotal * 0.05; // 0.05 - это 5%
		$cart->add_fee('Скидка в 5% за заказ на сумму более 10 000 рублей ', -$discount);
	
	}

}

add_action('woocommerce_cart_calculate_fees' , 'woo_discount_total');

You also insert it at the very bottom of the functions.php file, which is located in your theme folder.

Dynamic (changing depending on the total number of products) percentage discount on the entire cart in WooCommerce
Here, as you understood from the title, we will not calculate the discount on the amount, as we did in the second option, but on the number of items in the basket.

Implementation:


function woo_discount_total(WC_Cart $cart) {

	if(is_admin() && !defined('DOING_AJAX')) {
	
		return;
		
	}

	$woo_count_item = $cart->get_cart_contents_count(); // Количество товаров в корзине
	
	if($woo_count_item >= 10 && $woo_count_item <= 50) {
	
		$discount = $cart->subtotal * 0.03; // 0.03 - это 3%
		$cart->add_fee('Скидка в 3% за заказ включающий в себя от 10 до 50 товаров ', -$discount);
		
	} elseif($woo_count_item > 50) {
	
		$discount = $cart->subtotal * 0.05; // 0.05 - это 5%
		$cart->add_fee('Скидка в 5% за заказ включающий в себя более 50 товаров ', -$discount);
	
	}

}

add_action('woocommerce_cart_calculate_fees' , 'woo_discount_total');