2

I need to add in order metadata the amount of the discount for each product individually. For example, There are ten products in the order. The user has applied a coupon discount to the products. The total amount of the discount was calculated (if there are products on_sale, they were not taken into discount calculate).

But in the end, we got a total discount. And I need to split this discount for all the products that received it and even write this value into order item metadata.

Why don't I even have a sample code? I just don't know where to start. The only thing I think is to take the total discount, divide it by the number of products that can have a discount (these are all products in the order except on_sale), And add the resulting number to the product meta. But there is a problem, I'm not sure if this is the right solution. Could you please share any advice on how to solve this?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Victor Sokoliuk
  • 435
  • 4
  • 17

1 Answers1

4

You can start with woocommerce_checkout_create_order_line_item action hook where you will be able to set custom order item meta data. This hook has 4 arguments:

  • $item the order item (object),
  • $cart_item_key the cart item key (string),
  • $values the cart item (array),
  • $order the order (object).

So you can get from the cart item $values the discounted line item totals using:

$line_discount     = $value['line_subtotal'] - $value['line_total'];
$line_discount_tax = $value['line_subtotal_tax'] - $value['line_tax'];

Then for example all together to save the line discount total as custom order item meta data:

// Save Line item discount as custom order item meta data
add_action('woocommerce_checkout_create_order_line_item', 'action_checkout_create_order_line_item', 10, 4 );
function action_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    $line_discount     = $value['line_subtotal'] - $value['line_total'];
    $line_discount_tax = $value['line_subtotal_tax'] - $value['line_tax'];
    
    $item->update_meta_data( '_line_discount', $line_discount + $line_discount_tax );
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Then you can get it from an order item object $item using the WC_Data method get_meta() like:

 $line_discount = $item->get_meta('_line_discount');

Related: Get Order items and WC_Order_Item_Product in WooCommerce 3

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hello, this solution works like a charm. But I just can't understand. If I apply a coupon the amount of the discount is displayed for all items individual without any trouble. For example: 1.25 0.9 3.25 But we also have an individual custom discount. : And when this discount is applied, nothing is displayed. 0 0 0 It is strange because the sum of an individual discount is calculated not from what discount is used, but from the difference between the subtotal amount and the total. As I get right: $line_discount = $value['line_subtotal'] - $value['line_total']; – Victor Sokoliuk Mar 15 '21 at 17:27
  • Oh, sorry I could not compliment the comment link: https://stackoverflow.com/questions/65097517/apply-a-welcome-discount-to-non-on-sale-items-in-woocommerce A welcome discount is calculated based on this code. And for some reason, it is not taken into account. Here is a link for example: https://prnt.sc/10mj1mp – Victor Sokoliuk Mar 15 '21 at 17:53
  • @VictorSokoliuk The link provided in your comment is using **a negative fee**, which is a trick that WooCommerce does not recommend using, and that **is not really a discount**. Fees are not applied at item level and even when they are negative are still Fees, but not discounts… That's why you get 0 0 0 0 – LoicTheAztec Mar 15 '21 at 18:26
  • So, as I get it right, at this level, I will not get to these values in any way? And the total/subtotal amount of the cart will not give me anything in this case. And there is only one way out to change the welcome bonus? – Victor Sokoliuk Mar 15 '21 at 18:29
  • @VictorSokoliuk 1) *"I will not get to these values in any way?"*: You could try to split the negative fee amount based on cart item line total amount and add it to your custom field. 2) *"cart item total/subtotal amount"*: doesn't take fees into account. 3) *"And there is only one way out to change the welcome bonus?"*: I don't catch (?). – LoicTheAztec Mar 15 '21 at 18:35
  • 1) It's really, thanks for the idea, I'll try to split the negative fee. 3) Sorry. I mean, if I can not resolve this issue, then I will have to abandon the welcome bonus. – Victor Sokoliuk Mar 15 '21 at 18:46
  • @VictorSokoliuk For the Welcome bonus, instead you could auto-apply/remove a coupon "welcome bonus" of 5% based on your conditions. I have made some answer(s) related if I remember. But you need to allow this coupon to be applied with others. This will solve the problem. – LoicTheAztec Mar 15 '21 at 18:57
  • Hi! Here is my solution: https://prnt.sc/10qvmbn It's not very elegant, but it works. Thank you for helping. – Victor Sokoliuk Mar 20 '21 at 14:07