在 Woocommerce 的管理员编辑订单页面上显示每个特定订单的所有可用运输方式 [英] Display ALL available shipping methods for each specific order on admin edit order pages in Woocommerce
问题描述
在我的基于 WooCommerce 的网站的结帐页面上,用户将拥有一个可供选择的送货方式列表,具体取决于他们购买的商品
On the checkout page of my WooCommerce based site, users will have a list of shipping methods to choose from depending on what they are purchasing
这是我想要捕获的列表:
类似的东西
- 超过特定价格的订单免费送货"
- 某些商品的货运",等等.
我的目标是显示每个特定订单的所有可用方法,并将其显示在管理视图的编辑订单/订单详细信息"页面上.
My goal, is to display ALL available methods for each specific order, and display it on the "Edit Order / Order Details" page in the Admin view.
一个小功能,可帮助我们快速确定人们更常选择的选项,具体取决于他们可用的选项.
A small feature that would help us be able to quickly identify what option people are choosing more often, depending on the choices that they have available.
这是我目前所拥有的:
add_action( 'woocommerce_checkout_update_order_meta', 'save_available_shipping_methods' );
function save_available_shipping_methods( $order_id ) {
$shippingmethods = WC()->cart->get_shipping_methods();
update_post_meta( $order_id, '_shipping_methods', $shippingmethods );
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'get_available_shipping_methods', 10, 1 );
function get_available_shipping_methods($order){
$order = wc_get_order( $order_id );
if ( $order ) {
echo '<p><strong>'.__('Available Shipping Methods: ').'</strong> ' . get_post_meta($order->get_shipping_methods(), '_shipping_field_value', true ) . '</p>';
}
}
我也会附上一张图片,也许会让事情更容易理解.
I'll attach a picture as well to maybe make things a little easier to follow.
我想把它放在这里.当前挂钩将字段置于送货地址下方
推荐答案
- 在
woocommerce_checkout_update_order_meta
钩子中,可用的运输包裹通过update_post_meta
存储 - 在代码中添加了带有解释的注释
- In the
woocommerce_checkout_update_order_meta
hook, the available shipping packages are stored viaupdate_post_meta
- Comments with explanation added in the code
function action_woocommerce_checkout_update_order_meta( $order_id ) {
// Get shipping packages
$packages = WC()->shipping()->get_packages();
// Set array
$rate_labels = array();
// Loop trough packages
foreach ( $packages as $key => $package ) {
// Loop through package rates
foreach( $package['rates'] as $rate_id => $rate ) {
// Push to array
$rate_labels[] = $rate->get_label();
}
}
// NOT empty
if ( ! empty ( $rate_labels ) ) {
// Update post meta
update_post_meta( $order_id, '_available_shipping_methods', $rate_labels );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'action_woocommerce_checkout_update_order_meta', 10, 1 );
// Display on the order edit page (backend)
function action_woocommerce_admin_order_data_after_shipping_address( $order ) {
// Get meta
$rate_labels = $order->get_meta( '_available_shipping_methods' );
// True
if ( $rate_labels ) {
// Loop trough rate labels
foreach( $rate_labels as $rate_label ) {
// Output
echo '<p>' . $rate_label . '</p>';
}
}
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );
这篇关于在 Woocommerce 的管理员编辑订单页面上显示每个特定订单的所有可用运输方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!