【问题标题】:Displaying a custom notice only on some Email notifications仅在某些电子邮件通知上显示自定义通知
【发布时间】:2017-03-15 21:57:00
【问题描述】:

我在 WooCommerce 上为每个具有不同(整数)值的产品使用自定义字段 days_manufacture

我还使用此代码在电子邮件通知中显示一条消息,其中最高值为 “制造天数”

add_action('woocommerce_email_before_order_table', 'days_of_manufacture_view_order_and_email', 99);
    function days_of_manufacture_view_order_and_email($order, $type='email') {
        $day_txt = ' ' . __('day', 'your_theme_domain_slug' );
        $days_txt = ' ' . __('days', 'your_theme_domain_slug' );
        $max_days = 0;

        // Your customized style for the email template (to adapt for your needs)
        $style = 'border:solid 2px #ededed; padding:10px; font-weight:bold;';

        // Your customized text goes in here
        $text = __('Your Order will be produced in: ', 'your_theme_domain_slug' );

        foreach( $order->get_items() as $item )
            if(get_post_meta($item['product_id'], 'days_manufacture', true) > $max_days )
                $max_days = get_post_meta($item['product_id'], 'days_manufacture', true);

        if($max_days != 0) {
            if ($max_days == 1)
                $days_txt = $day_txt;

            $output = $text . $max_days . $days_txt;

            // displayed on the email notifications
            if($type == 'email')
                echo "<div class='woocommerce-info' style='$style'>$output</div>"; // <== Customize the styles if needed
            // displayed on the woocommerce templates   
            else
                echo "<div class='woocommerce-info' style='display:block !important;'>$output</div>"; // displayed on the templates
        }
    }

此代码运行良好,但现在,我想在电子邮件“新订单”、“订单处理”、“订单暂停”和“订单失败”中显示此消息 .

我怎样才能做到这一点?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    可以使用基于订单状态的条件来仅针对特定电子邮件通知来显示此自定义消息。

    这是您更改后的代码:

    add_action('woocommerce_email_before_order_table', 'days_of_manufacture_view_order_and_email', 99);
    
    function days_of_manufacture_view_order_and_email($order, $type='email') {
    
        // Defining the undesired orders status
        $not_this_statuses = array('wc-completed','wc-failed','wc-cancelled');
        if(!in_array($order->post_status, $not_this_statuses)){
    
            $day_txt = ' ' . __('day', 'your_theme_domain_slug' );
            $days_txt = ' ' . __('days', 'your_theme_domain_slug' );
            $max_days = 0;
    
            // Your customized style for the email template (to adapt for your needs)
            $style = 'border:solid 2px #ededed; padding:10px; font-weight:bold;';
    
            // Your customized text goes in here
            $text = __('Your Order will be produced in: ', 'your_theme_domain_slug' );
    
            foreach( $order->get_items() as $item )
                if(get_post_meta($item['product_id'], 'days_manufacture', true) > $max_days )
                    $max_days = get_post_meta($item['product_id'], 'days_manufacture', true);
    
            if($max_days != 0) {
                if ($max_days == 1)
                    $days_txt = $day_txt;
    
                $output = $text . $max_days . $days_txt;
    
                // displayed on the email notifications
                if($type == 'email')
                    echo "<div class='woocommerce-info' style='$style'>$output</div>"; // <== Customize the styles if needed
                // displayed on the woocommerce templates
                else
                    echo "<div class='woocommerce-info' style='display:block !important;'>$output</div>"; // displayed on the templates
            }
    
        }
    
    }
    

    代码位于您的活动子主题(或主题)的 function.php 文件中,也位于任何插件文件中。

    这是经过测试并且有效的。

    【讨论】:

      猜你喜欢
      • 2021-05-05
      • 2020-11-05
      • 2018-08-31
      • 2021-03-26
      • 2021-02-01
      • 2023-03-18
      • 2014-01-02
      • 2019-10-31
      • 2020-12-07
      相关资源
      最近更新 更多