在 WooCommerce 订单页面上显示所有可用的运输方式和费用

上一个/相关问题:在 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>';

        }

    }

}


阿晨1998
浏览 211回答 2
2回答

富国沪深

以下略有不同的代码将显示标签和所有可用运输方式的成本(在一个数组 | 一个 foreach 循环中):add_action( 'woocommerce_checkout_create_order', 'action_wc_checkout_create_order' );function action_wc_checkout_create_order( $order ) {&nbsp; &nbsp; $shipping_data = array(); // Initializing&nbsp; &nbsp; // Get shipping packages keys from cart&nbsp; &nbsp; $packages_keys = (array) array_keys(WC()->cart->get_shipping_packages());&nbsp; &nbsp; // Loop through shipping packages keys (when cart is split into many shipping packages)&nbsp; &nbsp; foreach( $packages_keys as $key ){&nbsp; &nbsp; &nbsp; &nbsp; // Get available shipping rates from WC_Session&nbsp; &nbsp; &nbsp; &nbsp; $shipping_rates = WC()->session->get('shipping_for_package_'.$key)['rates'];&nbsp; &nbsp; &nbsp; &nbsp; // Loop through shipping rates&nbsp; &nbsp; &nbsp; &nbsp; foreach( $shipping_rates as $rate_key => $rate ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set all related shipping rate data in the array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $shipping_data[] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => $rate_key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'method_id'&nbsp; &nbsp;=> $rate->method_id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'instance_id' => (int) $rate->instance_id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label'&nbsp; &nbsp; &nbsp; &nbsp;=> $rate->label,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'cost'&nbsp; &nbsp; &nbsp; &nbsp; => (float) $rate->cost,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'taxes'&nbsp; &nbsp; &nbsp; &nbsp;=> (array) $rate->taxes,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'package_key' => (int) $key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Save shipping data as order custom field&nbsp; &nbsp; if( ! empty($shipping_data) ) {&nbsp; &nbsp; &nbsp; &nbsp; $order->update_meta_data( '_shipping_data', $shipping_data );&nbsp; &nbsp; }}add_action( 'woocommerce_admin_order_data_after_shipping_address', 'available_shipping_rates_after_shipping_address' );function available_shipping_rates_after_shipping_address( $order ) {&nbsp; &nbsp; // Get shipping rates custom meta data&nbsp; &nbsp; $shipping_data = $order->get_meta( '_shipping_data' );&nbsp; &nbsp; if ( ! empty($shipping_data) ) {&nbsp; &nbsp; &nbsp; &nbsp; echo '<p><strong>Shipping Methods: </strong><br>';&nbsp; &nbsp; &nbsp; &nbsp; // Loop through shipping rates data&nbsp; &nbsp; &nbsp; &nbsp; foreach( $shipping_data as $rate ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Calculate cost with taxes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $rate_cost = $rate['cost'] + array_sum($rate['taxes']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Output&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $rate['label'] . ( $rate_cost > 0 ? ': '. wc_price($rate_cost) : '' ) . '<br>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo '</p>';&nbsp; &nbsp; }}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。

白板的微信

如果有人碰巧有和我一样的问题,我是这样做的:function action_woocommerce_admin_order_data_after_shipping_address( $order ) {&nbsp; &nbsp; // Get meta&nbsp; &nbsp; $rate_labels = $order->get_meta( '_available_shipping_methods' );&nbsp; &nbsp; $rate_costs = $order->get_meta( '_available_shipping_method_cost' );&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $methods = array ( $rate_labels, $rate_costs );&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // True&nbsp; &nbsp; if ( $rate_labels ) {&nbsp; &nbsp; &nbsp; &nbsp; // Loop&nbsp; &nbsp; &nbsp; &nbsp; echo '<p><strong>Shipping Methods: </strong>';&nbsp; &nbsp; &nbsp; &nbsp; foreach(array_combine($rate_labels, $rate_costs) as $rate_label => $rate_cost) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo '<p>' . $rate_label . ' - $' . $rate_cost . '</p>';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP