skip to Main Content

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.

enter image description here

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


  1. 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:

    if ( ! function_exists( 'phr4pp_modify_woocommerce_email_order_details' ) ) {
        function phr4pp_modify_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
            echo 'text here!';
        }
    
        add_action( 'woocommerce_email_order_details', 'phr4pp_modify_woocommerce_email_order_details', 10, 4 );
    }
    

    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.

    Login or Signup to reply.
  2. 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:

    // Setting the email ID as a global variable
    add_action('woocommerce_email_before_order_table', 'set_the_email_id_as_a_global_variable', 1, 4);
    function set_the_email_id_as_a_global_variable($order, $sent_to_admin, $plain_text, $email){
        $GLOBALS['email_id'] = $email->id;
    }
    
    // Display product price by Kilo under the product SKU
    add_action( 'woocommerce_order_item_meta_start', 'display_remaining_stock_quantity', 10, 3 );
    function display_remaining_stock_quantity( $item_id, $item, $order ) {
        // Only for order item "line item" type
        if ( !$item->is_type('line_item') ) {
            return;
        }
        $globalsVar = $GLOBALS; // Pass $GLOBALS variable as reference
    
        // Target New Order Email notification 
        if( isset($globalsVar['email_id']) && $globalsVar['email_id'] === 'new_order' ) {
            $product  = $item->get_product();
            $quantity = $item->get_quantity();
            $weight   = $product->get_weight();
    
            if ( $weight > 0 ) {        
                if ( 'excl' === get_option( 'woocommerce_tax_display_cart' ) ) {
                    $subtotal = $order->get_line_subtotal( $item );
                } else {
                    $subtotal = $order->get_line_subtotal( $item, true );
                }
                $price_per_kg = $subtotal / $quantity / $weight;
                $price_args   = array('currency' => $order->get_currency());
    
                echo '<div>' . wc_price( $price_per_kg, $price_args ) . '/kg</div>';
            }
        }
    }
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search