在 WooCommerce 订单页面上显示所有可用的运输方式和成本 [英] Display all available shipping methods and costs on WooCommerce Order pages
问题描述
上一个/相关问题:在 Woocommerce 的管理员编辑订单页面上显示每个特定订单的所有可用运输方式
Previous / Related Question: Display ALL available shipping methods for each specific order on admin edit order pages in Woocommerce
目前在我基于 WooCommerce 的网站中,我想在订单编辑页面上显示可用的运输方式和价格.
Currently in my WooCommerce based site, I am wanting to display the available shipping methods and prices on the order edit page.
它没有按照我的意愿显示数据.例如,到目前为止我的代码的输出结果是:
It does not display the data as I want. For example, the output of my code so far results in:
方法一
方法二
方法 3
Method 1
Method 2
Method 3
价格 1
价格 2
价格 3
Price 1
Price 2
Price 3
或者,我希望它显示如下:
When alternatively, I would like for it to display like this:
方法 1 - $Price 1
方法 2 - $Price 2
方法 3 - $Price 3
Method 1 - $Price 1
Method 2 - $Price 2
Method 3 - $Price 3
我明白为什么它会以这种方式显示,但我很好奇如何同时迭代循环并格式化它们,而不是一个接一个.
I understand why it is displaying this way, but I was curious how I could iterate the loops at the same time and format them, rather than one after the other.
这是我目前的代码:
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>';
}
}
}
推荐答案
如果有人碰巧遇到和我一样的问题,我是这样做的:
In case anyone happens to have the same question as I did, here is how I did it:
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>';
}
}
}
这篇关于在 WooCommerce 订单页面上显示所有可用的运输方式和成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!