我嘗試取得 Woocommerce 訂單中附加的費用名稱,我得到一個數組,但我不知道如何取得該名稱。
我嘗試使用函數 get_name ()
但它不起作用。
$the_order->get_items( array( 'line_item', 'fee', 'shipping' ) );
原始資料輸出:
[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粉4360523642024-02-22 00:28:10
要存取和使用訂單費用項目的屬性,您需要使用 WC_Order_Item_Fee 方法首先使用 foreach 循環這樣:
// (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(); }
經過測試並有效