php – 为本地提取Woocommerce订单的特定电子邮件通知添加自定义文本

我尝试了以下代码,当local_pickup是选择的送货方式时,该代码向所有应该收到customer_processing_order和customer_completed_order的客户显示消息.

我注意到我没有在任何订单元数据中存储任何_shipping_method项目,但仅限于:order_item_type:shipping> method_id> local_pickup:3

我怎样才能找回它?

我尝试了这个代码没有成功:

// 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>';
   }
}

解决方法:

这可以通过订单运送物品迭代完成,这样:

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文件中,或者放在任何插件文件中.

经过测试和工作.

上一篇:php – WooCommerce – 免费送货时隐藏其他送货方式


下一篇:php – 将自定义字段添加到运输选项卡中的WooComerce产品设置页面