【问题标题】:How to set a WooCommerce email template as default for all emails如何将 WooCommerce 电子邮件模板设置为所有电子邮件的默认模板
【发布时间】:2019-11-29 00:00:37
【问题描述】:

我正在寻找一种使用自定义 WooCommerce 模板发送所有 WordPress 电子邮件的方法,以便所有电子邮件看起来都一样。

模板的路径是:

woocommerce/emails/my-custom-woocommerce-template.php

【问题讨论】:

    标签: wordpress email templates woocommerce


    【解决方案1】:

    是否必须全部模板化在一个文件中?如果没有,这些入口点的组合可能会让您获得您正在寻找的标准化:

    1. email-header.php 允许您自定义电子邮件的开头,包括标题图像(如果您需要做的不仅仅是更改其 URL)。它会打开其余电子邮件内容的布局标签
    2. email-footer.php 允许您自定义页脚,并关闭在页眉中开始的布局标签。
    3. email-styles.phpwoocommerce_email_styles 过滤器可让您自定义 CSS(请参阅我的文章 here 中的一些陷阱)。
    4. 各种操作/过滤器分散在电子邮件中,用于自定义各个部分。

    【讨论】:

    • 感谢@dan-wich,许多电子邮件可以从不同的插件发送,我无法配置每个插件,对我来说一个不错的解决方案是将模板设置为所有 WP 发送的默认模板.
    • 我在另一个答案中看到您找到了使用电子邮件模板插件的解决方案。您可能仍需要编辑我提到的文件,以便您的电子邮件不会被双重包装在模板代码中(从该插件包装,然后使用 WooCommerce 页眉/页脚)。
    【解决方案2】:

    您可以使用以下功能。它正在工作

    function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
        global $woocommerce;
    
        // List of all templates that should be replaced with custom template
        $woo_templates = array( 
            'emails/admin-new-order.php',
            'emails/admin-failed-order.php',
            'emails/admin-cancelled-order.php',
            'emails/customer-completed-order.php',
            'emails/customer-new-account.php',
            'emails/customer-note.php',
            'emails/customer-on-hold-order.php',
            'emails/customer-processing-order.php',
            'emails/customer-refunded-order.php',
            'emails/customer-reset-password.php',
        );
        //Check whether template is in replacable template array
    
        if( in_array( $template_name, $woo_templates ) ){
    
            // Set your custom template path
            $template = your_template_path.'emails/my-custom-woocommerce-template';
        }
    
        // Return what we found
        return $template;
    }
    add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
    

    【讨论】:

    • 谢谢@melvin,它看起来像我要找的东西,问题是我不知道要放入列表中的所有模板的名称,例如一些电子邮件是通过忍者形态。有没有办法为所有发送设置模板,无一例外?
    • @jjj 上述数组包含 WooCommerce 发送的所有默认电子邮件模板名称。我不知道通过 njnia 表单发送的电子邮件的模板名称,因为我以前没有使用过
    • 所以我必须列出通过系统发送的所有电子邮件的名称(WP、WC 和插件)...我了解方法,这是一个很好的方法,但我会继续寻找更灵活的解决方案。
    • 是的,我会的,非常感谢您的努力和帮助,再次感谢。
    • @jjj 一旦我有更灵活的解决方案,我会更新你。
    【解决方案3】:
    add_filter( 'wp_mail', 'your_wp_mail_action' ); //  $args = compact( 'to', 'subject', 'message', 'headers', 'attachments' )
    function your_wp_mail_action( $args ) {
        global $your_prefix_your_email_args; // the args you could use in my-custom-woocommerce-template file
    
        $your_prefix_your_email_args = $args;
    
        ob_clean();
        get_template_part( 'woocommerce/emails/my-custom-woocommerce-template' );
    
        $args['message'] = ob_get_clean();
    
        // ... your logic
    
        return $args;
    }
    

    【讨论】:

    • 感谢您的回答,能否请您再改进一下,因为我无法使其工作,只有没有样式和消息的模板。
    【解决方案4】:

    要查看和更新​​电子邮件设置,请登录您的网站仪表板。在左侧菜单中,单击 WooCommerce → 设置。

    在那里,您会在顶部找到几个选项标签。点击电子邮件查看以下模板

    您可以根据需要自定义所有内容

    【讨论】:

    • 问题是关于在 WooCommerce 电子邮件系统之外发送的电子邮件。
    猜你喜欢
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2013-02-07
    • 2020-08-31
    • 1970-01-01
    • 2014-09-13
    相关资源
    最近更新 更多