WooCommerce已經(jīng)有了一堆鉤子,你可以在WooCommerce模板中使用它們,而不是添加自己的鉤子...
良好的開發(fā)規(guī)則是先使用現(xiàn)有的鉤子。如果沒有方便或可用的鉤子,那麼你可以透過子主題覆蓋WooCommerce模板。為什麼?因?yàn)槟0逵袝r(shí)會(huì)更新,然後你需要更新編輯過的模板,而鉤子則不需要。
對(duì)於「客戶完成訂單」通知,請(qǐng)使用woocommerce_order_item_meta_end
動(dòng)作鉤子,如下所示:
// 將email_id設(shè)置為全局變量 add_action('woocommerce_email_before_order_table', 'set_email_id_as_a_global', 1, 4); function set_email_id_as_a_global($order, $sent_to_admin, $plain_text, $email = null ){ if ( $email ) { $GLOBALS['email_id_str'] = $email->id; } } // 在“客戶完成訂單”電子郵件通知中顯示自定義訂單項(xiàng)元數(shù)據(jù) add_action( 'woocommerce_order_item_meta_end', 'custom_email_order_item_meta', 10, 2 ); function custom_email_order_item_meta( $item_id, $item ){ // 獲取email ID全局變量 $refGlobalsVar = $GLOBALS; $email_id = isset($refGlobalsVar['email_id_str']) ? $refGlobalsVar['email_id_str'] : null; // 僅適用于“客戶完成訂單”電子郵件通知 if ( ! empty($email_id) && 'customer_completed_order' === $email_id ) { bis_show_kit_meta_contents( $item->get_product_id() ); } }
這將允許你只在「客戶完成訂單」通知中顯示自訂元資料。
或者,你可以將鉤子替換為具有相同函數(shù)變數(shù)參數(shù)的woocommerce_order_item_meta_start
。
將程式碼放在你的子主題的functions.php檔案中或外掛中。