根据项目自定义字段的最大值在电子邮件上显示自定义通知


Displaying a custom notice on Emails based on item custom field highest value

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

我也有这个代码,显示购物车页面上的消息与最大值"制造天数":

add_action('woocommerce_before_cart', 'days_of_manufacture');
function days_of_manufacture() {
    $day_txt = ' ' . __('day', 'your_theme_domain_slug' );
    $days_txt = ' ' . __('days', 'your_theme_domain_slug' );
    $text = __('Your Order will be produced in: ', 'your_theme_domain_slug' );
    $max_days = 0;
    foreach( WC()->cart->get_cart() as $cart_item )
        if($cart_item['days_manufacture'] > $max_days)
            $max_days = $cart_item['days_manufacture'];
    if($max_days != 0) {
        if ($max_days == 1)
            $days_txt = $day_txt;
        $output = $text . $max_days . $days_txt;
        echo "<div class='woocommerce-info'>$output</div>";
    }
}

现在我想在电子邮件中显示这条消息。

我怎样才能做到这一点?

谢谢

你可以这样做,稍微改变一下的函数,这是你上一个问题的答案,我另外把这个函数挂钩到 woocommerce_email_before_order_table 钩子上,用于订单通知电子邮件。

那么你将用这个函数替换现有的函数。你还可以替换两个模板中插入的函数(见下文)

这个函数可以处理在模板(和以前一样)和电子邮件通知中显示您的自定义动态消息。

代码如下:

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
    }
}

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

模板变化

现在在你的活动子主题(或主题) woocommerce 模板文件夹:

  • your_theme/woocommerce/myaccount/view-order.php
  • your_theme/woocommerce/checkout/thankyou.php

你将改变这一行:

<?php days_of_manufacture_order_view($order); // <== inserted Here ?>

通过这行:

<?php days_of_manufacture_view_and_email($order, 'template'); // <== inserted Here ?>

现在自定义通知也显示在电子邮件上,并且仍然在 Thankyou查看订单页面和

上工作

My account > Orders > view order ' pages too.


引用:

  • WooCommerce -在"谢谢"answers"我的帐户"查看订单页面上的自定义通知
  • 模板结构+通过主题覆盖模板
  • Woocommerce模板myaccount> view-order.php
  • Woocommerce模板checkout> thanks .php