A
A
Artem Ivanov2021-12-08 12:19:19
WordPress
Artem Ivanov, 2021-12-08 12:19:19

How to make a discount in woocmomerce?

Hello! Please tell me how to make a discount when placing an order, for example, for a table and 4 chairs 10%.
On the Internet, I found a function for a discount only from the number of goods in the basket, but here you need to unlock the number of categories in the basket
, this is what I found:

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 >= 3 && $woo_count_item <= 50) {
  
    $discount = $cart->subtotal * 0.05; 
    $cart->add_fee('Скидка в 5% за заказ включающий в себя от 3 товаров ', -$discount);
    
  } elseif($woo_count_item > 100000) {
  
    $discount = $cart->subtotal * 0.05; 
    $cart->add_fee('Скидка в 5% за заказ включающий в себя от 3 товаров  ', -$discount);
  
  }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2021-12-08
@artklu

You have a basket object,
respectively, get your goods from it
$cart->get_cart()
Go through them in a cycle, look at the categories and, if the conditions match, add a discount as in your example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question