【问题标题】:Remove Order # from WooCommerce "Order on-hold" & "New Order" Emails从 WooCommerce“订单保留”和“新订单”电子邮件中删除订单号
【发布时间】:2018-10-17 20:02:43
【问题描述】:

我想从 WooCommerce 生成的“订单暂停”和“新订单”电子邮件中删除自动生成的订单号。

我正在使用第三方插件在下订单后分配自定义订单号,因此我分配的新订单号仍然可以在以后的电子邮件中使用,这一点很重要。在更改之前,我不希望客户(或管理员)看到原始订单号。

任何帮助将不胜感激!

【问题讨论】:

    标签: php wordpress templates woocommerce email-notifications


    【解决方案1】:

    已更新 (仅适用于 woocommerce 3.3+ 特定模板)

    您需要通过您的子主题覆盖 Woocommerce 电子邮件模板,如以下链接的官方文档中所述:

    Template structure & Overriding templates via a theme

    要复制和覆盖的模板是woocommerce/templates/emails/email-order-details.php

    在此模板中(按照说明复制到您的主题),您将需要更改整个块:

    <h2>
        <?php
        if ( $sent_to_admin ) {
            $before = '<a class="link" href="' . esc_url( $order->get_edit_order_url() ) . '">';
            $after  = '</a>';
        } else {
            $before = '';
            $after  = '';
        }
        /* translators: %s: Order ID. */
        echo wp_kses_post( $before . sprintf( __( 'Order #%s', 'woocommerce' ) . $after . ' (<time datetime="%s">%s</time>)', $order->get_order_number(), $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ) );
        ?>
    </h2>
    

    到:

    <?php
        // Targetting specific email notificatoins
        $email_ids = array('new_order', 'customer_on_hold_order');
    
        $date = sprintf( '<time datetime="%s">%s</time>', $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) );
    
        // Displaying order number except for "New Order" and "Customer On Hold Order" notifications
        if( ! in_array($email->id, $email_ids) ){
            $order_number = sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() );
            $date = '('.$date.')';
        } else {
            $date = __('Order date:', 'woocommerce') . ' ' . $date;
            $order_number = '';
        }
    
        if ( $sent_to_admin ) {
            $before = '<a class="link" href="' . esc_url( $order->get_edit_order_url() ) . '">';
            $after  = '</a> ';
        } else {
            $before = '';
            $after  = ' ';
        }
    ?>
    
    <h2><?php echo $before . $order_number . $after . $date; ?></h2>
    

    这将删除“新订单”和“客户保留订单”电子邮件通知中的订单号。你会得到:

    1) 新订单(管理员):

    2) 客户保留订单:

    现在您还需要在 WooCommerce > 设置 > 电子邮件中从“新订单”主题中删除 ({order_number}) 并保存...

    你已经完成了……

    【讨论】:

    • 首先,感谢您的快速回复!但是,我没有在email-order-details.php 模板文件中看到您所指的第一个代码块。我看到一个类似的块包含在

      中,但是用您提供的代码替换它似乎不起作用。我错过了什么吗?我在一个多站点网络上,但我认为这不会产生影响。

    • @RBaum 我已经更新了代码,现在可以匹配了……抱歉,我没有得到正确的模板,该模板已随 woocommerce 3.3 更改。
    【解决方案2】:

    通过 CSS 删除订单号

    如果您不想覆盖电子邮件模板,可以使用 woocommerce_email_styles 挂钩添加一个简单的 CSS 规则,以隐藏 WooCommerce 电子邮件中的订单号。

    这个钩子在模板加载后被激活:/woocommerce/emails/email-styles.php

    仅隐藏“订单暂停”和“新订单”的订单号 您可以使用模板的第二个 $ email 参数 woocommerce_email_styles 钩子来检查 id。在此处查找列表:Targeting specific email with the email id in Woocommerce

    // hides the order number in the email templates
    add_filter( 'woocommerce_email_styles', 'add_woocommerce_email_styles', 10, 2 );
    function add_woocommerce_email_styles( $css, $email ) {
        // define the ids of the emails for which you want to add CSS rules
        $email_ids = array(
            'new_order',
            'customer_on_hold_order'
        );
        // adds CSS rules to these emails only
        if ( in_array( $email->id, $email_ids ) ) {
            $css .= 'div[id^="body_content_inner"] > h2 { display: none; }';
        }
        return $css;
    }
    

    代码已经过测试并且可以运行。将其添加到活动主题的 functions.php 中。

    添加新订单号

    基于this answer,您还可以添加新订单号,如下所示:

    // adds the new order number before the order table in the completed order email
    add_action( 'woocommerce_email_before_order_table', 'add_content_before_order_table', 99, 4 );
    function add_content_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
        // define the ids of the emails for which you want to add CSS rules
        $email_ids = array(
            'new_order',
            'customer_on_hold_order'
        );
        // adds CSS rules to these emails only
        if ( in_array( $email->id, $email_ids ) ) {
            echo '<p>New order number</p>'; // do not use the <h2> tag otherwise it will be hidden
        }
    }
    

    代码已经过测试并且可以运行。将其添加到活动主题的 functions.php 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 2016-03-03
      相关资源
      最近更新 更多