$order->get_items('fee') doesn't seem to work with the woocommerce_new_order hook

557 Views Asked by At

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)); 
}
0

There are 0 best solutions below