【问题标题】:Add the product description to WooCommerce email notifications将产品描述添加到 WooCommerce 电子邮件通知
【发布时间】:2017-04-22 20:26:48
【问题描述】:

如何将产品描述/单个产品页面的内容(不是简短描述)添加到 WooCommerce 新订单电子邮件通知?

我需要知道我的产品的具体书面描述,因为它们中的大多数几乎相同。

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    由于您要定位特定的电子邮件通知,首先我们需要获取 电子邮件 ID 以定位“新订单”电子邮件通知。唯一的方法是先获取它,然后在全局变量中设置值。

    然后在一个挂在 woocommerce_order_item_meta_end 动作钩子中的自定义函数中,我们专门为新订单电子邮件通知显示产品描述。

    代码如下:

     ## Tested on WooCommerce 2.6.x and 3.0+
    
    // 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, 4 );
    function product_description_in_new_email_notification( $item_id, $item, $order = null, $plain_text = false ){
    
        // 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"
        if ( 'new_order' == $email_id ) {
    
            if( version_compare( WC_VERSION, '3.0', '<' ) ) { 
                $product_id = $item['product_id']; // Get The product ID (for simple products)
                $product = wc_get_product($item['product_id']); 
            } else {
                $product = $item->get_product();
                
                if( $product->is_type('variation') ) {
                    $product = wc_get_product( $item->get_product_id() );
                }
            }
    
            // Get the Product description (WC version compatibility)
            if ( method_exists( $item['product'], 'get_description' ) ) {
                $product_description = $product->get_description(); // for WC 3.0+ (new)
            } else {
                $product_description = $product->post->post_content; // for WC 2.6.x or older
            }
    
            // Display the product description
            echo '<div class="product-description"><p>' . $product_description . '</p></div>';
        }
    }
    

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

    此代码已经过测试并且可以工作。

    woocommerce_order_item_meta_end action hook 中的代码更新和错误说明:

    PHP Warning for woocommerce_order_item_meta_end (Mike Joley)

    【讨论】:

    • 感谢您的回复。它正在工作。我现在收到带有产品描述的电子邮件。但是,下订单后,页面上会显示此消息:Warning: Missing argument 4 for product_description_in_new_email_notification() in /home/cbllookbook/public_html/wp-content/themes/you-child/functions.php on line 22 which is function product_description_in_new_email_notification( $item_id, $item, $order, $plain_text ){ 我应该重新排列参数吗?再次感谢。
    • 我不确定这是否是正确的方法;但是,我将参数 4 的值设置为 NULL,以便我可以摆脱该消息。
    猜你喜欢
    • 2021-07-22
    • 2020-10-03
    • 2023-03-23
    • 2021-07-14
    • 2021-03-22
    • 1970-01-01
    • 2021-05-22
    • 2018-08-31
    • 2020-06-08
    相关资源
    最近更新 更多