wordpress 添加基于运输方式的自定义文本到某些WooCommerce电子邮件通知

yh2wf1be  于 2023-01-16  发布在  WordPress
关注(0)|答案(1)|浏览(155)

我试图让WooCommerce发送不同的电子邮件给客户,这取决于他们是否选择了“收集”或任何交付方式。
如果客户选择收款,则“处理中”电子邮件必须告诉他们准备就绪后将收到通知,“已完成”电子邮件必须告诉他们前来收款。
如果客户选择交付,则“处理中”电子邮件必须告诉他们在交付途中将收到通知,“已完成”电子邮件必须告诉他们在交付途中。
目前,我有以下代码,没有任何东西出现在任何电子邮件。
我不确定我的错误是什么,我也不确定我是否使用了正确的变量名来引用我的运输方法。

add_action ('woocommerce_email_customer_details', 'custom_email_customer_details', 15, 4);
function custom_email_customer_details( $order, $sent_to_admin, $plain_text, $email ){

  
    if ( $order->has_shipping_method('Pickup from PnP Honey Junction') && $new_status == 'processing' ){

        echo "<h2>Shipping notice</h2>
        <p>You will be notified when to collect your order</p>";
        
    }
        
        elseif($order->has_shipping_method('Pickup from PnP Honey Junction') && $new_status == 'completed'){
            
        echo "<h2>Shipping notice</h2>
        <p>Your order is ready for pickup</p>";
            
        }
    
elseif ( ! $order->has_shipping_method('Pickup from PnP Honey Junction') && $new_status == 'processing' ) {

        echo "<h2>Shipping notice</h2>
        <p>You will be notified when your order is out for delivery</p>";
    
        }
    
    
    elseif(! $order->has_shipping_method('Pickup from PnP Honey Junction') && $new_status == 'completed' ) {
        
              echo "<h2>Shipping notice</h2>
        <p>Your order is out for delivery</p>";
        
        }

}

9nvpjoqh

9nvpjoqh1#

$new_status未定义,您应用的发运方法不正确。
要正确应用此方法,最好首先获取正确的方法ID,这可以通过以下代码完成:

function action_woocommerce_email_customer_details( $order, $sent_to_admin, $plain_text, $email ) { 
    // Get the shipping method Id
    $shipping_items = $order->get_items('shipping');
    $shipping_item = reset( $shipping_items );
    
    echo 'DEBUG INFORMATION: ' . $shipping_item->get_method_id();
}
add_action( 'woocommerce_email_customer_details', 'action_woocommerce_email_customer_details', 10, 4 );

结果将如下所示:

    • 调试信息:本地代答*

然后将上面的代码替换为下面的代码,要针对特定的电子邮件通知,可以使用$email->id

然后,问题是在此答案中应用调试信息:

function action_woocommerce_email_customer_details( $order, $sent_to_admin, $plain_text, $email ) {
    // For 'processing' & has shipping method 'REPLACE WITH THE DEBUG INFORMATION'
    if ( $email->id == 'customer_processing_order' && $order->has_shipping_method( 'local_pickup' ) ) {
        echo __( 'My processing text', 'woocommerce' );
    // For 'completed'
    } elseif( $email->id == 'customer_completed_order' && $order->has_shipping_method( 'local_pickup' ) ) {
        echo __( 'My completed text', 'woocommerce' );      
    }
}
add_action( 'woocommerce_email_customer_details', 'action_woocommerce_email_customer_details', 10, 4 );

相关问题