2017-04-22 112 views

回答

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, 3); 
function product_description_in_new_email_notification($item_id, $item, $order){ 

    // 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) { 

     // Get The product ID (for simple products) 
     $product_id = $item['product_id']; 

     // Get an instance of WC_Product object 
     $product = wc_get_product($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行动挂钩:

PHP Warning for woocommerce_order_item_meta_end (Mike Joley)

+0

谢谢您的回复!它正在工作。我现在收到带产品说明的电子邮件。然而,订货后,被显示在页面上的此消息: 警告:行缺少参数4 product_description_in_new_email_notification()在/home/cbllookbook/public_html/wp-content/themes/you-child/functions.php 22 这是功能product_description_in_new_email_notification($ ITEM_ID,$项目,$秩序,$ plain_text){ 我应该重新安排的论点?再次感谢。 –

+0

我不确定这是否是正确的方法;然而,我将参数4的值设置为NULL,以便我可以摆脱消息。 –

相关问题