亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 WooCommerce 訂單頁面上顯示所有可用的運輸方式和費用

在 WooCommerce 訂單頁面上顯示所有可用的運輸方式和費用

PHP
阿晨1998 2023-07-21 18:32:42
上一個/相關問題:在 Woocommerce 的管理編輯訂單頁面上顯示每個特定訂單的所有可用送貨方式目前在我基于 WooCommerce 的網站中,我想在訂單編輯頁面上顯示可用的運輸方式和價格。它沒有按照我想要的方式顯示數據。例如,到目前為止我的代碼的輸出結果是:方法1方法2方法3價格1價格2價格3或者,我希望它顯示如下:方法 1 - $Price 1方法 2 - $Price 2方法 3 - $Price 3我理解為什么它以這種方式顯示,但我很好奇如何同時迭代循環并格式化它們,而不是一個接一個地循環。到目前為止,這是我的代碼:add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );function action_woocommerce_admin_order_data_after_shipping_address( $order ){? ? // Get meta? ? $rate_labels = $order->get_meta( '_available_shipping_methods' );? ? $rate_costs = $order->get_meta( '_available_shipping_method_cost' );? ??? ? $methods = array ( $rate_labels, $rate_costs );? ??? ? // True? ? if ( $rate_labels ) {? ? ? ? // Loop? ? ? ? echo '<p><strong>Shipping Methods: </strong>';? ? ? ? foreach( $rate_labels as $rate_label ) {? ? ? ? ? ? // Output? ? ? ? ? ? echo '<p>' . $rate_label . '</p>';? ? ? ? }? ? ? ? foreach( $rate_costs as $rate_cost ) {? ? ? ? ? ? // Output? ? ? ? ? ? echo '<p> $' . $rate_cost . '</p>';? ? ? ? }? ? }}
查看完整描述

2 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

以下略有不同的代碼將顯示標簽和所有可用運輸方式的成本(在一個數組 | 一個 foreach 循環中):


add_action( 'woocommerce_checkout_create_order', 'action_wc_checkout_create_order' );

function action_wc_checkout_create_order( $order ) {

    $shipping_data = array(); // Initializing


    // Get shipping packages keys from cart

    $packages_keys = (array) array_keys(WC()->cart->get_shipping_packages());


    // Loop through shipping packages keys (when cart is split into many shipping packages)

    foreach( $packages_keys as $key ){

        // Get available shipping rates from WC_Session

        $shipping_rates = WC()->session->get('shipping_for_package_'.$key)['rates'];


        // Loop through shipping rates

        foreach( $shipping_rates as $rate_key => $rate ){

            // Set all related shipping rate data in the array

            $shipping_data[] = array(

                'id'          => $rate_key,

                'method_id'   => $rate->method_id,

                'instance_id' => (int) $rate->instance_id,

                'label'       => $rate->label,

                'cost'        => (float) $rate->cost,

                'taxes'       => (array) $rate->taxes,

                'package_key' => (int) $key,

            );

        }

    }


    // Save shipping data as order custom field

    if( ! empty($shipping_data) ) {

        $order->update_meta_data( '_shipping_data', $shipping_data );

    }

}


add_action( 'woocommerce_admin_order_data_after_shipping_address', 'available_shipping_rates_after_shipping_address' );

function available_shipping_rates_after_shipping_address( $order ) {

    // Get shipping rates custom meta data

    $shipping_data = $order->get_meta( '_shipping_data' );


    if ( ! empty($shipping_data) ) {

        echo '<p><strong>Shipping Methods: </strong><br>';


        // Loop through shipping rates data

        foreach( $shipping_data as $rate ) {

            // Calculate cost with taxes

            $rate_cost = $rate['cost'] + array_sum($rate['taxes']);


            // Output

            echo $rate['label'] . ( $rate_cost > 0 ? ': '. wc_price($rate_cost) : '' ) . '<br>';

        }


        echo '</p>';

    }

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。


查看完整回答
反對 回復 2023-07-21
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

如果有人碰巧有和我一樣的問題,我是這樣做的:


function action_woocommerce_admin_order_data_after_shipping_address( $order ) {

    // Get meta

    $rate_labels = $order->get_meta( '_available_shipping_methods' );

    $rate_costs = $order->get_meta( '_available_shipping_method_cost' );

    

    $methods = array ( $rate_labels, $rate_costs );

    

    // True

    if ( $rate_labels ) {

        // Loop

        echo '<p><strong>Shipping Methods: </strong>';

        foreach(array_combine($rate_labels, $rate_costs) as $rate_label => $rate_cost) {

             echo '<p>' . $rate_label . ' - $' . $rate_cost . '</p>';

        }

    }

    

}


查看完整回答
反對 回復 2023-07-21
  • 2 回答
  • 0 關注
  • 167 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號