【问题标题】:Add a custom text for a particular product on specific email notification in Woocommerce在 Woocommerce 中的特定电子邮件通知上为特定产品添加自定义文本
【发布时间】:2019-04-08 14:58:07
【问题描述】:

我想在 WordPress 的 customer-completed-order.php 中仅针对特定产品(产品 ID:1)添加额外的文本“此特定项目中有报价等”。其他产品不需要有这个额外的线。有人可以帮我找出这个吗?

 <?php
   /**
    * Customer completed order email
    *
    * This template can be overridden by copying it to yourtheme/woocommerce/emails/customer-completed-order.php.
    *
    * HOWEVER, on occasion WooCommerce will need to update template files and you
    * (the theme developer) will need to copy the new files to your theme to
    * maintain compatibility. We try to do this as little as possible, but it does
    * happen. When this occurs the version of the template file will be bumped and
    * the readme will list any important changes.
    *
    * @see https://docs.woocommerce.com/document/template-structure/
    * @package WooCommerce/Templates/Emails
    * @version 3.5.0
    */

   if ( ! defined( 'ABSPATH' ) ) {
    exit;
   }

   /*
    * @hooked WC_Emails::email_header() Output the email header
    */
   do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
<?php /* translators: %s: Customer first name */ ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $order->get_billing_first_name() ) ); ?></p>
<?php /* translators: %s: Site title */ ?>
<p><?php printf( esc_html__( 'Your %s order has been marked complete on our side.', 'woocommerce' ), esc_html( wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) ) ); ?></p>
<?php
   /*
    * @hooked WC_Emails::order_details() Shows the order details table.
    * @hooked WC_Structured_Data::generate_order_data() Generates structured data.
    * @hooked WC_Structured_Data::output_structured_data() Outputs structured data.
    * @since 2.5.0
    */
   do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );

   /*
    * @hooked WC_Emails::order_meta() Shows order meta data.
    */
   do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );

   /*
    * @hooked WC_Emails::customer_details() Shows customer details
    * @hooked WC_Emails::email_address() Shows email address
    */
   do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );

   ?>
<p>
   <?php esc_html_e( 'Thanks for shopping with us.', 'woocommerce' ); ?>
</p>
<?php
/*
* @hooked WC_Emails::email_footer() Output the email footer
*/
do_action( 'woocommerce_email_footer', $email );

【问题讨论】:

    标签: php wordpress woocommerce product email-notifications


    【解决方案1】:

    以下内容将在客户已完成电子邮件通知中的特定产品的订单项目名称下显示自定义文本:

    // Setting the email_is as a global variable
    add_action('woocommerce_email_before_order_table', 'the_email_id_as_a_global', 1, 4);
    function the_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email ){
        $GLOBALS['email_id_str'] = $email->id;
    }
    
    // Displaying product description in new email notifications
    add_action( 'woocommerce_order_item_meta_end', 'product_description_in_new_email_notification', 10, 3 );
    function product_description_in_new_email_notification( $item_id, $item, $order = null ){
        // HERE define your targetted product ID
        $targeted_id = 37;
    
        // HERE define the text information to be displayed for the targeted product id
        $text_information = __("There is an offer in this particular item", "woocommerce");
    
        // Getting the email ID global variable
        $refNameGlobalsVar = $GLOBALS;
        $email_id = $refNameGlobalsVar['email_id_str'];
    
        // If empty email ID we exit
        if(empty($email_id)) return;
    
        // Only for "New Order email notification" for your targeted product ID
        if ( 'customer_completed_order' == $email_id &&
            in_array( $targeted_id, array( $item->get_product_id(), $item->get_variation_id() ) ) ) {
    
            // Display the text
            echo '<div class="product-text" style="margin-top:10px"><p>' . $text_information . '</p></div>';
        }
    }
    

    代码进入您的活动子主题(活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 我的文件在 mytheme/woocommerce/emails/customer-completed-order.php 中,所以我可以用上面更新的文件替换这个文件吗?而且我在上面的代码中找不到产品ID。对不起,我只是一个初学者。
    • @sherin 否...有两种方法:1)在您的活动子主题(活动主题)中,您将在function.php文件中添加代码...... 2)或使用插件Code snippet,您将此答案代码插入新代码 sn-p,然后您将保存并激活 sn-p。如果对您来说太复杂了,请尝试在 Skype 聊天中与我联系,搜索 Loïc de Marcé(巴黎)。
    • 好的,我将复制这段代码并粘贴到我的子主题function.php中。关于 $targeted_id = 37;我可以更改为我的目标产品 ID。对吗?
    • @sherin 当然可以,如您所见:“在这里定义您的目标产品 ID”
    • 当然。会看看
    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2019-10-07
    • 2018-09-19
    • 2020-11-05
    • 2018-05-22
    • 2021-03-26
    • 1970-01-01
    相关资源
    最近更新 更多