0

Currently my two options for shipping are regular shipping (using the Flexible Shipping plugin) and local pickup. I would like my Completed Order emails to be customized according to the shipping method used for the order (e.g. I'd like the header to say "We've shipped your order!" for regular shipping and "Your order is ready for pickup!" for local pickup).

Here's my first attempt at modifying the text through my child theme's functions.php (code based on LoicTheAztec answer in this thread). Currently, instead of inserting the desired text, it shows the default text that I entered in Woocommerce>Settings>Emails>Order Completed>Completed order>Email heading, regardless of shipping method selected.

    add_action( 'woocommerce_email_header ', 'modify_header_by_shipping', 10, 2 );

    function modify_header_by_shipping( $email_heading, $email ) {

      // Only modify "Customer Completed Order" email notification
      if( 'customer_completed_order' != $email->id ) return;

      //Initialize variable
      $found = false;
    
      // Get $order object from $email 
      $order = $email->object;
    
      // Iterating through Order shipping methods
      foreach($order->get_shipping_methods() as $value){
        $rate_id = $value->get_method_id(); // Get the shipping rate ID
        if ( 'shipping_method_0_local_pickup5' == $rate_id )
            $found = true;
      }

      if ($found)
          echo '<p>'.__("Your order is ready for pickup!","woocommerce").'</p>';
      else
          echo '<p>'.__("We've shipped your order!","woocommerce").'</p>';
    }

Here's my ordering page: https://carrickseeds.ca/checkout/

Andre W.
  • 1
  • 1
  • 1
    **1)** Use the first part of [this anwer](https://stackoverflow.com/a/69298436/11987538) to get the correct shipping method id, based on that information you can adjust your current code. **2)** _"instead of inserting the desired text, it shows the default text that I entered in Woocommerce"_ - I assume you at least get to see the else text? – 7uc1f3r Dec 23 '21 at 15:44
  • Thanks for the tip, @7uc1f3r. **1)** Done (it was actually "local_pickup") **2)** No, surprisingly not. I'm just getting the original text from Woocommerce settings: "We have shipped your order of seeds. We hope you enjoy them!" – Andre W. Dec 23 '21 at 17:51
  • Welcome to Stack Overflow _ Editing comment on a well presented question: The last closing curly bracket has 'fallen out' of the code block _ Open your post to edit, scoop up the stray curly bracket and place it inside the code-block for 100% pass ; ) – inputforcolor Jan 01 '22 at 15:46
  • Welcome to Stack Overflow _ Editing comment on a well presented question: The last closing curly bracket has 'fallen out' of the code block _ Open your post to edit, scoop up the stray curly bracket and place it inside the code-block for 100% pass ; ) – inputforcolor Jan 01 '22 at 15:46

0 Answers0