Support » Plugins » woocommerce order status emails

  • I’m doing this poor hack and would like to change the order status behavior of PROCESSING to send the email to xyz@gmail.com instead of the customer or admin, basically a third party.

    Does anyone know by any chance? I have a feeling its this line

    $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    but I dont know how to change it to email another email.

    Open to other suggestions.

    <?php
    /**
     * @package   WC-Sanmar-Order-Email/Classes
     * @author    JC
     * @copyright JC
     * @license   http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
     */
    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    /**
     * A custom Cancelled Order WooCommerce Email class
     *
     * @since 0.1
     */
    class WC_Sanmar_Order_Email extends WC_Email {
    
    	/**
    	 * Set email defaults
    	 *
    	 * @since 0.1
    	 */
    	public function __construct() {
    
    		// set ID, this simply needs to be a unique name
    		$this->id = 'wc_sanmar_order';
    
    		// this is the title in WooCommerce Email settings
    		$this->title = 'Sanmar Order';
    
    		// this is the description in WooCommerce email settings
    		$this->description = 'Sanmar Order Notification emails are sent when a customer order is cancelled';
    
    		// these are the default heading and subject lines that can be overridden using the settings
    		$this->heading = 'Sanmar Order';
    		$this->subject = 'New Order';
    
    		// these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar
    		$this->template_html  = 'emails/sanmar-order.php';
    		$this->template_plain = 'emails/plain/sanmar-order.php';
    
    		$path = explode( DIRECTORY_SEPARATOR, untrailingslashit( plugin_dir_path( __FILE__ ) ) );
    		array_pop( $path );
    		$path = implode( DIRECTORY_SEPARATOR, $path );
    
    		$this->template_base  = $path . '/templates/';
    
    		// Trigger on new paid orders
    		add_action( 'woocommerce_order_status_sanmar_notification', array( $this, 'trigger' ) );
    
    		// Call parent constructor to load any other defaults not explicity defined here
    		parent::__construct();
    	}
    
    	/**
    	 * Determine if the email should actually be sent and setup email merge variables
    	 *
    	 * @since 0.1
    	 * @param int $order_id
    	 */
    	public function trigger( $order_id ) {
    
    		// bail if no order ID is present
    		if ( ! $order_id ) {
    			return;
    		}
    
    		// setup order object
    		$this->object    = new WC_Order( $order_id );
    		$this->recipient = $this->object->billing_email;
    
    		// replace variables in the subject/headings
    		$this->find[] = '{order_date}';
    		$this->replace[] = date_i18n( woocommerce_date_format(), strtotime( $this->object->order_date ) );
    
    		$this->find[] = '{order_number}';
    		$this->replace[] = $this->object->get_order_number();
    
    		if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
    			return;
    		}
    
    		// woohoo, send the email!
    		$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    
    		}
    
    	/**
    	 * get_content_html function.
    	 *
    	 * @since 0.1
    	 * @return string
    	 */
    	public function get_content_html() {
    		ob_start();
    		woocommerce_get_template(
    			$this->template_html,
    			array(
    				'order'          => $this->object,
    				'email_heading'  => $this->get_heading(),
    			),
    			'',
    			$this->template_base
    		);
    		return ob_get_clean();
    	}
    
    	/**
    	 * get_content_plain function.
    	 *
    	 * @since 0.1
    	 * @return string
    	 */
    	public function get_content_plain() {
    		ob_start();
    		woocommerce_get_template(
    			$this->template_plain,
    			array(
    				'order'          => $this->object,
    				'email_heading'  => $this->get_heading(),
    			),
    			'',
    			$this->template_base
    		);
    		return ob_get_clean();
    	}
    
    	/**
    	 * Initialize Settings Form Fields
    	 *
    	 * @since 0.1
    	 */
    	public function init_form_fields() {
    
    		$this->form_fields = array(
    			'enabled'    => array(
    				'title'   => 'Enable/Disable',
    				'type'    => 'checkbox',
    				'label'   => 'Enable this email notification',
    				'default' => 'yes'
    			),
    			'subject'    => array(
    				'title'       => 'Subject',
    				'type'        => 'text',
    				'description' => sprintf( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', $this->subject ),
    				'placeholder' => '',
    				'default'     => ''
    			),
    			'heading'    => array(
    				'title'       => 'Email Heading',
    				'type'        => 'text',
    				'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.' ), $this->heading ),
    				'placeholder' => '',
    				'default'     => ''
    			),
    			'email_type' => array(
    				'title'       => 'Email type',
    				'type'        => 'select',
    				'description' => 'Choose which format of email to send.',
    				'default'     => 'html',
    				'class'       => 'email_type',
    				'options'     => array(
    					'plain'     => __( 'Plain text', 'woocommerce' ),
    					'html'      => __( 'HTML', 'woocommerce' ),
    					'multipart' => __( 'Multipart', 'woocommerce' ),
    				)
    			)
    		);
    	}
    
    } // end WC_Sanmar_Order_Email class
Viewing 1 replies (of 1 total)
  • Joey

    (@leglesslizard)

    Hey,

    If it is just the recipient you want to change can you not just change this line:
    $this->recipient = $this->object->billing_email;
    (which grabs the email address off the order to use)

    If email address is static you could probably just stick it in there:
    $this->recipient = 'xyz@gmail.com';

    If it is dynamic then you will have to pull it in from wherever you need.

    Hope this helps!

    Regards,
    Joey

Viewing 1 replies (of 1 total)
  • The topic ‘woocommerce order status emails’ is closed to new replies.