How to add a custom email functionality in woocommerce,wordpress for example new email template for shipping order

How to add a custom email template option in woocommerce
Just copy and paste below code in your theme's functions.php

<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * A custom shipping WooCommerce Email class
 *
 * @since 0.1
 * @extends \WC_Email
 */
 if(!class_exists('WC_Shipping_Email'))
 {
    include_once WP_PLUGIN_DIR.'/woocommerce/includes/emails/class-wc-email.php' ;
class WC_Shipping_Email extends WC_Email {
    public function __construct() {

    if ( ! class_exists('Emogrifier') ){
            require_once( 'includes/emogrifier/Emogrifier.php' );
        }
    $this->id = 'wc_shipping_email';

   
    $this->title = 'Shipping Order';

   
    $this->description = 'Shipping Order Notification emails are sent when order are ready for  shipping to customer';

   
    $this->heading = '';
    $this->subject = 'Shipping Order';

     $this->_templates = array('emails/shipping-order.php',
    'emails/plain/shipping-order.php');
    $this->template_html = 'emails/shipping-order.php';
    $this->template_plain ='emails/plain/shipping-order.php';
       
   add_filter( 'woocommerce_locate_core_template',array( $this, 'rj_woocommerce_locate_template' ), 10, 3 );
   add_filter('woocommerce_template_directory',array( $this, 'rj_woocommerce_locate_template_dir' ), 10, 2);
         parent::__construct();
        $this->recipient = $this->get_option( 'recipient' );

   
    if ( ! $this->recipient )
        $this->recipient = get_option( 'admin_email' );
        add_filter('wc_get_template', array( $this, 'custom_template') , 999, 5 );
}

public function rj_woocommerce_locate_template($template, $template_name, $template_path )
{
    global $woocommerce;
    if( in_array( $template_name, $this->_templates ) )
    {
     return    $template = plugin_dir_path( __FILE__ ).$template_name;
    }else
    {
        return $template;
    }
}
public function rj_woocommerce_locate_template_dir($dir,$template)
{
   
    if( in_array( $template, $this->_templates ) )
    {
            return plugin_dir_path( __FILE__ ).$template;
    }else
    {
        return $dir;
    }
}

public function custom_template($located, $template_name, $args, $template_path, $default_path){

            if( in_array( $template_name, $this->_templates ) )
            {
               
            return plugin_dir_path( __FILE__ ).$template_name;
            }
            return $located;
               
        }
public function get_content_html() {
    ob_start();
     wc_get_template( $this->template_html, array(
        'order'         => $this->object,
        'email_heading' => $this->get_heading()
    ),'',plugin_dir_path( __FILE__ ) );
   
    return ob_get_clean();
   
   // return plugin_dir_path( __FILE__ ).$this->template_html;
}
public function trigger( $order_id ) {

   
  
    if ( ! $order_id )
        return;

    $this->object = new WC_Order( $order_id );

   
    $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->get_recipient()
  $return =  $this->send( $this->object->billing_email, $this->get_subject(), $this->get_content_html(), $this->get_headers(), $this->get_attachments() );
  
   
   
   
}

public function get_content_plain() {
    ob_start();
    wc_get_template( $this->template_plain, array(
        'order'         => $this->object,
        'email_heading' => $this->get_heading()
    )  );
   
    return ob_get_clean();
}

public function init_form_fields() {

    $this->form_fields = array(
        'enabled'    => array(
            'title'   => 'Enable/Disable',
            'type'    => 'checkbox',
            'label'   => 'Enable this email notification',
            'default' => 'yes'
        ),
        'recipient'  => array(
            'title'       => 'Recipient(s)',
            'type'        => 'text',
            'description' => sprintf( 'Enter recipients (comma separated) for this email. Defaults to <code>%s</code>.', esc_attr( get_option( 'admin_email' ) ) ),
            'placeholder' => '',
            'default'     => ''
        ),
        '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'     => $this->get_email_type_options(),
           
        )
    );
}


} // end \WC_Expedited_Order_Email class
}


// second file for develop a custom plugin and how to use custom template

<?php

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) :
final class Rj_Shipping_Tracking{
    public $version = '1.0.0';
    protected  static $_instance = null;
    public static function instance() {
        if(is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    public function __construct() {
        add_action('init', array($this, 'init'),0);
    }
    public function init(){
        self::register_shipping_post_status();
        add_filter( 'wc_order_statuses', __CLASS__ .'::add_shipping_to_order_statuses' );
        add_filter( 'woocommerce_email_classes', __CLASS__.'::shipping_woocommerce_email' );
      add_action( 'woocommerce_order_status_shipping', array( $this, 'trigger' ) );
      add_action( 'woocommerce_order_status_refunded', array( $this, 'refund_trigger' ) );
        add_filter( 'woocommerce_payment_complete_order_status', array($this,'rj_woocommerce_payment_complete_order_status'), 10, 2 );
    //    add_filter('woocommerce_add_cart_item',__CLASS__ .'::check_switch_subscription_added_in_cart',20,2);
    }
    // Add to list of WC Order statuses
    public function register_shipping_post_status(){
        register_post_status( 'wc-shipping', array(
            'label'                     => 'Shipping',
            'public'                    => true,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop( 'Shipping <span class="count">(%s)</span>', 'Shipping <span class="count">(%s)</span>' )
        ) );
    }
   
public function rj_woocommerce_payment_complete_order_status ( $order_status, $order_id )
{

    global $woocommerce;
    $woocommerce->cart->empty_cart();

}
    public function add_shipping_to_order_statuses( $order_statuses ) {
        $new_order_statuses = array();
        // add new order status after processing
        foreach ( $order_statuses as $key => $status ) {
            $new_order_statuses[ $key ] = $status;
            if ( 'wc-processing' === $key ) {
                $new_order_statuses['wc-shipping'] = 'Shipping';
            }
        }
        return $new_order_statuses;
    }
    public function trigger($order_id)
    {
       
         require( 'shipping_tracking_email.php' );
         $email = new WC_Shipping_Email();
         $email->trigger($order_id);
    }
    public function refund_trigger($order_id)
    {
        require( 'refunded_tracking_email.php' );
         $email = new WC_refunded_Email();
         $email->trigger($order_id);
    }
    public function shipping_woocommerce_email( $email_classes ) {

    require( 'shipping_tracking_email.php' );
    require( 'refunded_tracking_email.php' );
    $email_classes['WC_Shipping_Email'] = new WC_Shipping_Email();
    $email_classes['WC_refunded_Email'] = new WC_refunded_Email();


    return $email_classes;

}
}
Rj_Shipping_Tracking::instance();
endif;

Comments

Popular posts from this blog

How to add a custom sorting or order by option on category archive or product archive page in woocommerce wordpress

How to create a custom wp_list_table and bulk action in wordpress

How to add image option in nav menu in wordpress