本文介绍了向特定的电子邮件通知中添加自定义文本,以获取本地取货Woocommerce订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我尝试了以下代码,向所有选择local_pickupcustomer_completed_order的客户显示一条消息,而local_pickup是选择的送货方式.

I tried the following code that is displaying a message to all those customers who should receive a customer_processing_order and customer_completed_order when local_pickup is the chosen shipping method.

我注意到我没有在任何订单元中存储任何_shipping_method项,而仅存储以下内容:order_item_type:shipping> method_id> local_pickup:3

I noticed I do not store any _shipping_method item within any order meta, but only a thing like: order_item_type: shipping > method_id > local_pickup:3

我如何找回它?

我尝试了此代码,但没有成功:

I tried this of code without success:

// testo per Ritiro in Sede

add_action( 'woocommerce_email_order_details', 'my_completed_order_email_instructions', 10, 4 );
function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {

if( 'customer_processing_order' != $email->id ) return;

if ( method_exists( $order, 'get_id' ) ) {
    $order_id = $order->get_id();
} else {
    $order_id = $order->id;
}

$shipping_method_arr = get_post_meta($order_id, '_shipping_method', false);
$method_id = explode( ':', $shipping_method_arr[0][0] );
$method_id = $method_id[0];  // We get the slug type method


if ( 'local_pickup' == $method_id ){
    echo '<p><strong>Ritiro in sede</strong></p>';
   }
}

推荐答案

可以通过以下方式遍历订单运送物品:

This can be done iterating through order shipping items, this way:

add_action( 'woocommerce_email_order_details', 'my_completed_order_email_instructions', 10, 4 );
function my_completed_order_email_instructions( $order, $sent_to_admin, $plain_text, $email ) {
    // Only for processing and completed email notifications to customer
    if( ! ( 'customer_processing_order' == $email->id || 'customer_completed_order' == $email->id ) ) return;

    foreach( $order->get_items('shipping') as $shipping_item ){
        $shipping_rate_id = $shipping_item->get_method_id();
        $method_array = explode(':', $shipping_rate_id );
        $shipping_method_id = reset($method_array);
        // Display a custom text for local pickup shipping method only
        if( 'local_pickup' == $shipping_method_id ){
            echo '<p><strong>Ritiro in sede</strong></p>';
            break;
        }
    }
}

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

经过测试,可以正常工作.

Tested and works.

这篇关于向特定的电子邮件通知中添加自定义文本,以获取本地取货Woocommerce订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 03:15