0

How to make my custom email trigger inside my email class, so that I can make it send, but only if a specific meta key and value from database is specificly meta_key = Pickup location and if the meta_value = Ishøj ?

Down below here I will paste my current code.

        // Trigger email when status is processing
            add_action( 'woocommerce_order_status_cancelled_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
            add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
            add_action( 'woocommerce_order_status_on-hold_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
            add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
  
/**
   * Prepares email content and triggers the email
   *
   * @param int $order_id
*/

    public function trigger( $order_id, $order = false ) {

        $this->setup_locale();
        
        // Bail if no order ID is present
            if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
                $order = wc_get_order( $order_id );
            }
        
        $pickup = get_post_meta( $order_id, 'Pickup Location', true );
        
        
            if ( is_a( $order, 'WC_Order' ) ) {
                $this->object                         = $order;
                $this->recipient                      = $this->object->get_billing_email();
                $this->placeholders['{order_date}']   = wc_format_datetime( $this->object->get_date_created() );
                $this->placeholders['{order_number}'] = $this->object->get_order_number();
            }
        // Send the email only once when an order is made        
        if ( ! get_post_meta( $order_id, 'Pickup Location', true ) && $pickup == 'Ishøj' ) {
            
            
            if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
                return;
            }
            // All well, send the email
            $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
            
            
            $this->restore_locale();
            
        
    }   
    } 
Pierre.Vriens
  • 2,117
  • 75
  • 29
  • 42
FizzelTV
  • 57
  • 1
  • 8
  • 1
    Hey @FizzelTV! Do you get any errors? What's the problem with your code? – dilico May 21 '21 at 14:30
  • Hello @DiegoColantoni - No that is why i am so confused, i dont get any errors. i just dont recieve any "custom" email as i was supposed to :( - And i also need to figure out how to make the trigger only trigger if a meta value in the database is = Ishøj – FizzelTV May 24 '21 at 08:50
  • Just to eliminate the obvious reasons, can you confirm if you're adding your custom email class to the list of email classes WooCommerce loads (i.e. through the `woocommerce_email_classes` filter)? Does your `trigger` function get invoked at all? – dilico May 24 '21 at 09:35

0 Answers0