代码之家  ›  专栏  ›  技术社区  ›  Hamza Ahmad

在Woomerce中发送特定运输方式的订单保留电子邮件

  •  3
  • Hamza Ahmad  · 技术社区  · 7 年前

    我正在使用WordPress4.9.6和WooCommerce 3.4.3版本,我需要发送“订单等待”电子邮件以了解具体的发货方式。

    为什么? 我使用DHL航运插件计算航运和'替代'航运方法也可用。如果用户在结账时选择DHL Shipping,则会计算运费,并且订单可以继续。但是,如果他们选择“备用”运输方式,我必须通知他们,因为“备用”运输方式被重命名为“免费运输”,所以他们的订单在支付运费之前一直处于等待状态,我将为他们单独开具发票,以便他们在订购后支付运费。

    在寻找问题的解决方案时,我在这个回答线程中找到了一些符合我需要的代码: Customizing Woocommerce New Order email notification based on shipping method

    但是我不知道如何编辑这段代码以使它适合我的特定场景。

    非常感谢你的帮助。

    1 回复  |  直到 7 年前
        1
  •  3
  •   LoicTheAztec    7 年前

    要使其适用于重命名的免费送货方法,您需要稍微更改代码:

    add_action ('woocommerce_email_order_details', 'custom_email_notification_for_shipping', 5, 4);
    function custom_email_notification_for_shipping( $order, $sent_to_admin, $plain_text, $email ){
    
        // Only for "On hold" email notification and "Free Shipping" Shipping Method
        if ( 'customer_on_hold_order' == $email->id && $order->has_shipping_method('free_shipping') ){
            $order_id = $order->get_id(); // The Order ID
    
            // Your message output
            echo "<h2>Shipping notice</h2>
            <p>Your custom message goes here… your custom message goes here… your custom message goes here… your custom message goes here… your custom message goes here…</p>";
        }
    }
    

    代码放在活动子主题(或活动主题)的function.php文件中。测试和工作。

    enter image description here


    强制“保留”和“已完成”电子邮件通知 (可选)

    订单状态更改时,以下代码将触发“暂停”电子邮件通知,仅适用于您重命名的“免费送货”送货方式和“已完成”电子邮件通知。

    add_action( 'woocommerce_order_status_changed', 'sending_on_hold_email_notification', 20, 4 );
    function sending_on_hold_email_notification( $order_id, $old_status, $new_status, $order ){
        // Only  "On hold" order status and "Free Shipping" Shipping Method
        if ( $order->has_shipping_method('free_shipping') && $new_status == 'on-hold' ){
            // Getting all WC_emails objects
            $notifications = WC()->mailer()->get_emails();
            // Send "On hold" email notification
            $notifications['WC_Email_Customer_On_Hold_Order']->trigger( $order_id );
        } elseif ( ! $order->has_shipping_method('free_shipping') && $new_status == 'completed' ){
            // Getting all WC_emails objects
            $notifications = WC()->mailer()->get_emails();
            // Send "On hold" email notification
            $notifications['WC_Email_Customer_Completed_Order']->trigger( $order_id );
        }
    }
    

    代码放在活动子主题(或活动主题)的function.php文件中。测试和工作。