[英]Show Shipping Method data on the Order edit pages in WooCommerce
在本網站上的 WooCommerce 中,我有 2 種本地取貨運輸方式:
shipping_method_0_local_pickup31
shipping_method_0_local_pickup32
不幸的是,此運輸方式未顯示在管理訂單編輯頁面上
是否可以使用: add_action('woocommerce_admin_order_data_after_shipping_address','cwn_add_pickup_to_order_item_meta', 1, 2);
function cwn_add_pickup_to_order_item_meta($shipping_method) {
echo "<h4>Pickup Time</h4>";
echo '<p><strong>' . __('AM pickup') . ':</strong><br> ' . get_post_meta($shipping_method->id, '_shipping_method_0_local_pickup31', true) . '</p>';
echo '<p><strong>' . __('PM pickup') . ':</strong><br> ' . get_post_meta($shipping_method->id, '_shipping_method_0_local_pickup32', true) . '</p>';
}
有兩種類似的方法可以在 WC_Order 對象中獲取運輸方法數據。
1) 使用WC_Abstract_Order
get_shipping_methods()
方法。 2) 使用WC_Abstract_Order
get_items( 'shipping' )
方法。
這將輸出一組WC_Order_Item_Shipping
對象。 因此,您將需要一個 foreach 循環來使用WC_Order_Item_Shipping
方法來獲取數據,這樣(其中$order
是WC_Order
對象的實例) :
foreach($order->get_items( 'shipping' ) as $shipping_method ){
$method_id = $shipping_method->get_method_id().'<br>';
$shipping_method_name = $shipping_method->get_name().'<br>';
$shipping_method_title = $shipping_method->get_method_title().'<br>';
$shipping_method_total = $shipping_method->get_total().'<br>';
$shipping_method_total_tax = $shipping_method->get_total_tax().'<br>';
}
//Or:
foreach($order->get_shipping_methods() as $shipping_method ){
$method_id = $shipping_method->get_method_id().'<br>';
$shipping_method_name = $shipping_method->get_name().'<br>';
$shipping_method_title = $shipping_method->get_method_title().'<br>';
$shipping_method_total = $shipping_method->get_total().'<br>';
$shipping_method_total_tax = $shipping_method->get_total_tax().'<br>';
}
在您的代碼中,
$shipping_method
掛鈎函數參數是錯誤的,因為它應該是$order
(WC_Order object
的實例。
所以現在你可以在你的鈎子函數中使用它,例如:
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'cwn_add_pickup_to_order_item_meta', 10, 1 );
function cwn_add_pickup_to_order_item_meta( $order ){
echo '<div>';
foreach($order->get_shipping_methods() as $shipping_method ){
echo '<p><strong>Shipping Method ID:</strong> '. $shipping_method->get_method_id().'<br>
<strong>Shipping Method name:</strong> '. $shipping_method->get_name().'<br>
<strong>Shipping Method title:</strong> '. $shipping_method->get_method_title().'<br>
<strong>Shipping Method Total:</strong> '. $shipping_method->get_total().'<br>
<strong>Shipping Method Total tax:</strong> '. $shipping_method->get_total_tax().'</p><br>';
}
echo '</div>';
}
代碼位於活動子主題(或主題)的 function.php 文件中或任何插件文件中
經過測試並適用於 WooCommerce 3+
我正在尋找一種在完成購買時執行命令的方法,並且選擇的運輸方式與“本地提貨”不同。
你的最后一個代碼給了我我需要的光。 我的代碼有效。
按照我的代碼來幫助需要它的人:
function showpopup($order_id){
$order = wc_get_order($order_id);
$order->get_shipping_methods();
if( ! $order->has_shipping_method('local_pickup') ) {
echo '
<script>
function myFunction() {
alert("Retirada no local NÃO SELECIONADA!");
}
myFunction()
</script>
';
}
}
add_action( 'woocommerce_thankyou', 'showpopup', 10, 1 );
謝謝你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.