发送不同产品的自定义电子邮件模板 - woocommerce

时间:2017-07-18 09:43:10

标签: php wordpress email woocommerce

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

我发现唯一的解决方案是在主题文件夹的woocommerce/emails中为每个模板文件添加检查电子邮件。是否有更好的方法可以执行此操作?

中是否有可用选项
add_action( 'woocommerce_email', 'woocommerce_email_function' );

OR

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

2 个答案:

答案 0 :(得分:0)

我有一个解决方案,例如您要显示标题标题999的产品ID 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 );

答案 1 :(得分:0)

我花了点时间来弄清楚如何从'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;
    }

}

希望对别人有帮助!

相关问题