简体   繁体   English

如何根据送货方式将自定义元数据添加到 WooCommerce 订单

[英]How to add Custom Meta Data to a WooCommerce Order based on the Shipping Method

I'm looking for a solution to add Custom Meta to each order in WooCommerce based on the shipping method chosen at the checkout.我正在寻找一种解决方案,以根据结帐时选择的送货方式将自定义元添加到 WooCommerce 中的每个订单。 As far as I can see there have been some updates since WooCommerce 3.0 so I'm struggling to find a definitive answer to this.据我所知,自 WooCommerce 3.0 以来已经有一些更新,所以我正在努力寻找一个明确的答案。

Here's what I have so far.这是我到目前为止所拥有的。

According to this thread the WC3+ / CRUD method would be:根据这个线程,WC3+/CRUD 方法将是:

    add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data ) {
    $order->update_meta_data( '_custom_meta_key', 'value' );
}

And according to this thread , you can use the shipping method conditionally using:根据此线程,您可以有条件地使用运输方法:

// Conditional function based on the Order shipping method 
if( $order->has_shipping_method('flat_rate') ) { 

The problem I am having is combining these functions.我遇到的问题是结合这些功能。 Here's what I have tried but it doesn't seem to work:这是我尝试过但似乎不起作用的方法:

add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function before_checkout_create_order( $order, $data ) {
    if( $order->has_shipping_method('Special Delivery') ) { 
    $order->update_meta_data( 'royal_mail_shipping_code', 'SD1' );
    }
    if( $order->has_shipping_method('Royal Mail Tracked 48') ) { 
    $order->update_meta_data( 'royal_mail_shipping_code', 'TPS' );
    }
}

Any help getting the above code working would be hugely appreciated!任何帮助使上述代码工作的帮助将不胜感激!

Try this:尝试这个:

add_action('woocommerce_checkout_update_order_meta',function( $order_id, $posted ) {

if( $order->has_shipping_method('Special Delivery') ) {
$order = wc_get_order( $order_id );
$order->update_meta_data( 'royal_mail_shipping_code', 'SD1' );
$order->save();
}
if( $order->has_shipping_method('Royal Mail Tracked 48') ) {
$order = wc_get_order( $order_id );
$order->update_meta_data( 'royal_mail_shipping_code', 'TPS' );
$order->save();
}
} , 10, 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 WooCommerce 中自动添加自定义订单发货项目元数据 - Automatically add custom order shipping item meta data in WooCommerce 如何在woocommerce中向订单项添加自定义元数据 - How to add custom meta data to the Order Items in woocommerce 如何在 WooCommerce 3 中添加自定义工作送货方式 - How to add a custom working Shipping Method in WooCommerce 3 WooCommerce根据付款方式和送货方式添加自定义电子邮件内容 - WooCommerce add custom email content based on payment method and shipping method 在 WooCommerce 中添加订单号作为自定义订单商品元数据 - Add order number as custom order item meta data in WooCommerce 在Woocommerce 3中添加自定义结帐字段作为订单自定义元数据 - Add custom checkout field as Order custom-meta data in Woocommerce 3 根据运输方式更改 Woocommerce 订单状态 - Change Woocommerce Order Status based on Shipping Method WooCommerce:获取为 package 选择的运输方式并将其存储为订单项目元数据 - WooCommerce: Get chosen shipping method for package and store it as order item meta data 将自定义购物车商品值添加到WooCommerce订单商品元数据 - Add a custom cart item value to WooCommerce order item meta data Woocommerce 预订添加自定义订单商品元数据 - Woocommerce Booking Add custom order item meta data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM