We have a number of products that are defined as "kits". They are product lists made up of other products. The data defining the suite is stored in the metadata array. I've created a hook to display metadata in the product page and shopping cart. I need to do something similar in the "Completed Order Email" and "Admin Order Page" but I don't know how to do it. This is the hook I created:
add_filter( 'bis_show_kit_meta', 'bis_show_kit_meta_contents', 10, 3 ); function bis_show_kit_meta_contents($productID) { global $wpdb; $postMeta = get_post_meta($productID,'',true); if ((isset($postMeta['bis_kit_id'])) and ($postMeta['bis_kit_id'][0] > 1)) { echo 'This is a Kit containing the following items:
'; echo $postMeta['bis_kit_type'][0].'
'; foreach($postMeta as $kititem) { foreach ($kititem as $value) { $newvalue = unserialize($value); if ($newvalue) { $newvalue2 = unserialize($newvalue); if($newvalue2['type']=="kititem"){ echo '' .$newvalue2['quantity']. ' -> ' .chr(9). $newvalue2['name']. ''; } } } } } } The current function is already hooked into the corresponding template in my child theme. I don't know how to apply a similar function to the
customer-completed-email.php file, nor where should I hook it in the admin edit order page. I found some code in another post that looks similar to what I need to do, but I can't figure out which file the modifications to the admin order are in. The code I found is:
add_action('woocommerce_before_order_itemmeta','woocommerce_before_order_itemmeta',10,3); function woocommerce_before_order_itemmeta($item_id, $item, $product){ ... } Any advice would be greatly appreciated
WooCommerce already has a bunch of hooks that you can use in WooCommerce templates instead of adding your own...
A good development rule is to use existing hooks first. If there is no hook convenient or available, then you can override the WooCommerce template via a child theme. Why? Because templates are updated sometimes and then you need to update the edited template, whereas hooks don't.
For the "Customer Completed Order" notification, use the
woocommerce_order_item_meta_endaction hook like this:// 将email_id设置为全局变量 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; } } // 在“客户完成订单”电子邮件通知中显示自定义订单项元数据 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() ); } }This will allow you to display custom metadata only in the "Customer Completed Order" notification.
Alternatively, you can replace the hook with
woocommerce_order_item_meta_startwith the same function variable parameters.Place the code in your child theme’s functions.php file or in a plugin.