In WooCommerce, display order details metadata in checkout confirmation emails and admin order pages
P粉022140576
2023-08-15 12:06:40
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
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: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.