2017-04-22 111 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,以便我可以擺脫消息。 –

相關問題