WooCommerce Email Title / subject line based on shipping method

Viewed 96

I'm trying to customise the woocommerce email title based on the shipping method used. I've searched and there's many conditional examples, but none that seem to work for the shipping option of local pickup. Here's what I am trying:

  add_action( 'woocommerce_email_header', 'local_pickup_processing_email_header', 10, 2);
    function local_pickup_processing_email_header( $email_heading, $email ) {
      if ( $email->id == 'customer_processing_order' ) { 
          $order = wc_get_order($order_id);
            if ($order->has_shipping_method('local_pickup')){
               echo "Thanks for Your Local Pickup Order";
            }
       }
 }

The above results in "Fatal error: Uncaught Error: Call to a member function get_shipping_method() on bool"

How do I resolve get_shipping_method ?

I also tried with a foreach, but had a similar error (Call to a member function get_items() )

   add_action( 'woocommerce_email_header', 'local_pickup_processing_email_header', 10, 2);
    function local_pickup_processing_email_header( $email_heading, $email ) {
      $order = wc_get_order( $order_id );
        if ( $email->id == 'customer_processing_order' ) { 
          
        foreach( $order->get_items('shipping') as $shipping_item ){
            $shipping_rate_id = $shipping_item->get_method_id();
            $method_array = explode(':', $shipping_rate_id );
            $shipping_method_id = reset($method_array);
            // Display a custom text for local pickup shipping method only
            if( 'local_pickup' == $shipping_method_id ){
               echo "Thanks for Your Local Pickup Order";
            }
          }
       }
   }
1 Answers

The reason for the error is, you are using a variable which is not defined anywhere i,e $order.

The two parameters available in your function as parameters are $email_heading and $email. So you have to make use of this variables.

You will get $order object by using the instance of $email. So you can use the line as shown below in your code.

$order = $email->object;
Related