In the context of making a B2B platform only based on a wordpress ecommerce website, I need to change the email sent to customers after placing an order by removing the list of items ordered as this can be a long list) but still sending cart (order) total. I followed the answer provided by @LoicTheAztec which works perfectly here is the function and the hook used
function pkdb2b_remove_order_details_from_emails(){
$mailer = WC()->mailer();
remove_action('woocommerce_email_order_details', array($mailer, 'order_details'), 10);
}
add_action('woocommerce_email_order_details','pkdb2b_remove_order_details_from_emails', 5, 4);
This removes the order details. For adding the cart total I am using another function and hook like so.
function pkdb2b_add_cart_total_email(){
;
$cart_total = WC()->cart->cart_contents_total;
echo "<p> Votre Commande montant Total = "."<strong>".$cart_total." DT"."</strong> est bien reçue</p>";
}
add_action('woocommerce_email_order_details','pkdb2b_add_cart_total_email', 5, 4);
This is also working when the order is validated. However, when order is canceled, in the received email no more cart total is seen (which is expected as we do not have a cart anymore).
I have tried to use this $order = WC_Emails()->order_details(); $total = $order-> get_total(); or this
$total = WC_Order()->get_total()
I tried reading the woocommerce doc but still could not find a way to access the order total or even $order variable
The Cart is a frontend live object, that is never used in the email notifications, for many reasons.
If you enable WP_DEBUG, you will see that your code attemps triggers errors.
You missed adding the hook variables arguments in your function, to get the order object.
Try the following instead:
add_action( 'woocommerce_init', 'pkdb2b_customize_email_order_details' );
function pkdb2b_customize_email_order_details(){
$mailer = WC()->mailer();
remove_action( 'woocommerce_email_order_details', array($mailer, 'order_details'), 10 );
add_action ( 'woocommerce_email_customer_details', 'custom_email_order_details', 10, 4 );
}
function custom_email_order_details( $order, $sent_to_admin, $plain_text, $email ){
printf('<p>Le montant total de votre commande = <strong>%s DT</strong> est bien reçue</p>',
wc_price( $order->get_total(), array( 'currency' => $order->get_currency() ) ) );
}
It should work.