【问题标题】:Send custom email template for different product - woocommerce为不同的产品发送自定义电子邮件模板 - woocommerce
【发布时间】:2017-07-18 09:43:10
【问题描述】:

我有一个使用 woocommerce 插件的 Wordpress 网站。我有不同种类的产品,我需要为不同的产品发送不同的电子邮件模板。

我发现唯一的解决方案是在主题文件夹中的woocommerce/emails 中添加对电子邮件的每个模板文件的检查。有没有更好的方法来做到这一点?

里面有没有可用的选项
add_action( 'woocommerce_email', 'woocommerce_email_function' );

是否可以为特定产品的订单添加不同的标题?

【问题讨论】:

  • 如您所知,有两种方法:1)使用与电子邮件通知相关的all available hooks(其中一些位于模板本身中)。 2)Override woocommerce templates via the theme ...由于您的问题不是很详细(我们无法猜测您要做什么)并且您没有显示您的自定义代码,因此无法回答这个问题...
  • @LoicTheAztec 我的商店有不同的产品。如果下订单,将使用模板发送订单电子邮件。我需要根据产品 ID 使用不同的模板
  • 是的,我已经明白了,谢谢……哪个代码和在哪里……你能提供你在这个被覆盖的模板中使用的代码吗? ...谢谢
  • @LoicTheAztec 还没写。我打算编码的方法是,检查模板文件中的订单id,如果是带有productid的订单,则返回模板的html
  • 好的,但答案取决于您要显示html代码的位置(以及什么订单状态?)所以请您添加您要添加​​的代码和确切位置(当条件匹配)...这样我就可以提供帮助...

标签: php wordpress email woocommerce


【解决方案1】:

我有一个解决方案,例如对于产品 ID 999 你要显示标题标题This is a very special title

function change_email_title_header_depending_of_product_id( $email_heading, $order ) {
    global $woocommerce;
    $items = $order->get_items();

    // check products
    foreach ( $items as $item ) {
        $product_id = $item['product_id'];
        if ( $product_id == 999 ) {
            $email_heading = 'This is a very special title';
        }
        return $email_heading;
    }
}
add_filter( 'woocommerce_email_heading_customer_processing_order', 'change_email_title_header_depending_of_product_id', 10, 2 );

【讨论】:

    【解决方案2】:

    我花了很长时间弄清楚如何从 'woocommerce_email' 获取订单 ID,但在 StackOverflow 上找不到答案,所以我想我可以分享一下对我有用的方法。

    覆盖模板是更改电子邮件内容的最明显方式,但它会扰乱来自其他插件(如 Smart Coupons)的电子邮件模板。

    我能够以编程方式从包含特定产品的电子邮件中删除 email_order_details.php 模板,如下所示:

    add_action( 'woocommerce_email', 'remove_woocommerce_email_order_details' );
    
    // Using $object in function was key to remove_action below
    function remove_woocommerce_email_order_details( $object ) {
    
        // Simple way to get the order ID
        $order_id = get_the_ID();
    
        $order = new WC_Order( $order_id );
        $items = $order->get_items();
    
        foreach ( $items as $item ) {
    
            // If product ID in order, remove order details
            $product_id = $item['product_id'];
            if ( $product_id == 1234 ) {
                remove_action( 'woocommerce_email_order_details', array( $object, 'order_details' ), 10, 4 );
            } 
        }
    
    }
    

    现在我用我自己的数据添加了woocommerce_email_order_details 表。

    add_filter( 'woocommerce_email_order_details', 'add_custom_email_order_details' );
    
    function add_custom_email_order_details() {
    
        // With these, you should be able access most elements in the 'woocommerce_email_order_details' table
        $order_id = get_the_ID();
        $order = new WC_Order( $order_id );
        $order_type = $order->order_type;
        $customer_id = $order->get_user_id();
    
        $items = $order->get_items();
        foreach ( $items as $item ) {
            $product_id = $item['product_id'];
        }
    
        if( $product_id == 1234 ) {
            echo "Order Type: " . $order_type;
            echo "Customer ID: " . $customer_id;
            echo "Product ID:" . $product_id;
        }
    
    }
    

    希望对其他人有所帮助!

    【讨论】:

    • 原来这个解决方案只适用于从编辑订单页面手动触发的电子邮件。所以它可以在新订单上正常工作,我将 remove_action( 'woocommerce_email_order_details', array( $object, 'order_details' ), 10, 4 );remove_woocommerce_email_order_details 函数移动到 add_custom_email_order_details 函数,紧随 if( $product_id == 1234 )
    猜你喜欢
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    相关资源
    最近更新 更多