0

I want to access the value of FEE on the Order Object, its not actually fee, its a discount, but we use negative value on woocommerce_cart_calculate_fees hook.

I tried so many way to access it but it did not work, at first i use get_total_discount() but it give me blank value, and i check the order object it is because it is added on the "fee_lines".

I already tried this: Get the order fee item details in Woocommerce 3 but its not the same and not works for me it did not enter on the loop actually here's my code:

add_action( 'woocommerce_new_order', 'insert_distributor_discount',  1, 1  );
function insert_distributor_discount($order_id){
    global $wpdb;
    
    $order  = wc_get_order( $order_id );

    //Get Discount Amount
    
    // Iterating through order fee items ONLY
    foreach( $order->get_items('fee') as $item_id => $item_fee ){

        // The fee name
        $fee_name = $item_fee->get_name();

        // The fee total amount
        $fee_total = $item_fee->get_total();

        // The fee total tax amount
        $fee_total_tax = $item_fee->get_total_tax();
        
        error_log(print_r('$fee_name '.$fee_name, TRUE)); 
        error_log(print_r('$fee_total '.$fee_total, TRUE)); 
        error_log(print_r('$fee_total_tax '.$fee_total_tax, TRUE)); 
    }
    
    error_log(print_r('$fee_name '.$fee_name, TRUE)); 
    error_log(print_r('$fee_total '.$fee_total, TRUE)); 
    error_log(print_r('$fee_total_tax '.$fee_total_tax, TRUE));

    //INSERT QUERY HERE
    
}

Is there a way i can access it?


Here's the sample order object, and what i want to get is the value of Distributor Discount under fee_lines :

{"id":0,"parent_id":0,"status":"","currency":"","version":"","prices_include_tax":false,"date_created":null,"date_modified":null,"discount_total":0,"discount_tax":0,"shipping_total":0,"shipping_tax":0,"cart_tax":0,"total":0,"total_tax":0,"customer_id":0,"order_key":"","billing":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":"","email":"","phone":""},"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"payment_method":"","payment_method_title":"","transaction_id":"","customer_ip_address":"","customer_user_agent":"","created_via":"","customer_note":"","date_completed":null,"date_paid":null,"cart_hash":"","number":"0","meta_data":[{"key":"is_vat_exempt","value":"no"},{"key":"stockist_id","value":"102"}],"line_items":{"new:line_items0":{"legacy_values":{"key":"9461cce28ebe3e76fb4b931c35a169b0","product_id":481,"variation_id":0,"variation":[],"quantity":1,"data_hash":"b5c1d5ca8bae6d4896cf1807cdf763f0","line_tax_data":{"subtotal":[],"total":[]},"line_subtotal":1000,"line_subtotal_tax":0,"line_total":1000,"line_tax":0,"data":{}},"legacy_cart_item_key":"9461cce28ebe3e76fb4b931c35a169b0"}},"tax_lines":[],"shipping_lines":{"new:shipping_lines0":{"legacy_package_key":0}},"fee_lines":{"new:fee_lines0":{"legacy_fee":{"id":"distributor-discount","name":"Distributor Discount","tax_class":"","taxable":true,"amount":"-500","total":-500,"tax_data":[],"tax":0},"legacy_fee_key":"distributor-discount"}},"coupon_lines":[]}

I tried also using this json to loop through it and get that value and i cant make it work.

Here's what i tried

$order_arr = json_decode($order, true);

foreach ($order_arr['fee_lines'][0]['legacy_fee'] as $key => $value) {
    error_log(print_r($key . ' - '. $value, TRUE)); 
}
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
Ako Si
  • 101
  • 2
  • 8
  • 1
    Duplicate: [Get the order fee item details in Woocommerce 3](https://stackoverflow.com/questions/47669922/get-the-order-fee-item-details-in-woocommerce-3) – 7uc1f3r Dec 16 '20 at 15:00
  • it is not. I tried that answer and its not working. im trying to access "fee_lines" not "fee" that on that answer. – Ako Si Dec 17 '20 at 00:43
  • i included the code ony my question i used the same with that answer. It actually not enter on loop. – Ako Si Dec 17 '20 at 01:03
  • 1
    Your question was: **_Access a negative fee used as discount from Woocommerce order object_**, and that is certainly a duplicate as the question/answer referred to. That it doesn't work with the hook (`woocommerce_new_order`) you are using is another matter. **Therefore, I have adapted your question accordingly and voted to reopen it** – 7uc1f3r Dec 17 '20 at 08:16

0 Answers0