I want to add the price per kilogram of an item to the admin-new-order.php template. I’ve managed to get the weight and quantity and to echo it (see "104" in screenshot) in the admin-new-order.php
and customer-completed-order.php
templates by adding the following code to email-order-items.php
template file:
$product = $item->get_product();
$product_weight = $product->get_weight();
echo $product_weight;
$qty = $item->get_quantity();
echo $qty;
What I’m missing is the price variable (see "82,80" screenshot) so I can echo a calculation like this:
echo ($subtotal / $product_weight / $qty);
I haven’t figured out how to get the item subtotal to echo a calculation with variables yet. But primarily I need to get the the item subtotal, which is shown in the screenshot below.
Afterward I want to echo my calculation as mentioned above. Ideally, this should show in the admin-new-order.php template only. But if it’s only possible to show it in both emails by putting the code in the email-order-items.php
it’s fine as well.
2
Answers
As I understand it, you want to modify the
admin-new-order.php
email.A way to do it is to hook into the action
'woocommerce_email_order_details'
and output all the information you need there.Example:
Changing the priority (third parameter in
add_action()
) to a lower number will display the text before anything else.The above code could go into the file
functions.php
inside your theme.An advice: Always try to use available hooks, instead of overriding templates.
The following will display the product price by Kilo under the SKU on email order items only for New Order notification:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
Make sure that the template
email-order-items.php
is the default WooCommerce one, without any modifications.