Get order fee item details in Woocommerce 3
P粉438918323
P粉438918323 2024-02-21 10:23:55
0
1
395

I try to get the name of the fees attached to a Woocommerce order and I get an array but I don't know how to get the name.

I tried using the function get_name () but it doesn't work.

$the_order->get_items( array( 'line_item', 'fee', 'shipping' ) );

Original data output:

[137] => WC_Order_Item_Fee Object
        (
            [extra_data:protected] => Array
                (
                    [tax_class] => 
                    [tax_status] => taxable
                    [amount] => 
                    [total] => 
                    [total_tax] => 
                    [taxes] => Array
                        (
                            [total] => Array
                                (
                                )

                        )

                )

            [data:protected] => Array
                (
                    [order_id] => 7795
                    [name] => Frais de réservation
                    [tax_class] => 0
                    [tax_status] => taxable
                    [amount] => 
                    [total] => 35
                    [total_tax] => 0
                    [taxes] => Array
                        (
                            [total] => Array
                                (
                                )

                        )

                )

P粉438918323
P粉438918323

reply all(1)
P粉436052364

To access and use the properties of the order fee item, you need to use the WC_Order_Item_Fee method First use a foreach loop Like this:

// (optional if not defined) An instance of the WC_Order object
$the_order = wc_get_order( $order_id );

// Iterating through order fee items ONLY
foreach( $the_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();
}

Tested and valid

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!