Home  >  Q&A  >  body text

In WooCommerce, display order details metadata in checkout confirmation emails and admin order pages

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: <pre class="brush:php;toolbar:false;">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:<br>'; echo $postMeta['bis_kit_type'][0].'<br>'; foreach($postMeta as $kititem) { foreach ($kititem as $value) { $newvalue = unserialize($value); if ($newvalue) { $newvalue2 = unserialize($newvalue); if($newvalue2['type']=="kititem"){ echo '<li>' .$newvalue2['quantity']. ' -> ' .chr(9). $newvalue2['name']. '</li>'; } } } } } }</pre> 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 <code>customer-completed-email.php</code> 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: <pre class="brush:php;toolbar:false;">add_action('woocommerce_before_order_itemmeta','woocommerce_before_order_itemmeta',10,3); function woocommerce_before_order_itemmeta($item_id, $item, $product){ ... }</pre> Any advice would be greatly appreciated
P粉022140576P粉022140576453 days ago419

reply all(1)I'll reply

  • P粉736935587

    P粉7369355872023-08-16 10:40:06

    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_end action 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_start with the same function variable parameters.

    Place the code in your child theme’s functions.php file or in a plugin.

    reply
    0
  • Cancelreply