1
votes

I've a plugin for manage my shipments with two custom status : awaiting-shipment and shipped.

I try to add an email sent when the order passes to shipped.

I find this on Stack Overflow : Woocommerce Refund Email but I can figure how it's work

Here is my plugin file code :
I updated my code with the recommendations of helgatheviking and Adrien Leber below

function shippement_tracking_filter_actions( $actions ){
    $actions[] = "woocommerce_order_status_shipped";
    return $actions;
}
add_filter( 'woocommerce_email_actions', 'shippement_tracking_filter_actions' );

function add_expedited_order_woocommerce_email( $email_classes ) {
    require( 'includes/class-wc-expedited-order-email.php' );
    $email_classes['WC_Expedited_Order_Email'] = new WC_Expedited_Order_Email();
    return $email_classes;
}
add_filter( 'woocommerce_email_classes', 'add_expedited_order_woocommerce_email' );`

And my Class :

class WC_Expedited_Order_Email extends WC_Email {
    public function __construct() {

        $this->id               = 'expedited_order_tracking';
        $this->customer_email   = true;
        $this->title            = __( 'Shippement Traking', 'customEmail' );
        $this->description      = __( 'Sent tracking email to customer', 'customEmail' );
        $this->heading          = __( 'Your {site_title} order is shipped', 'customEmail' );
        $this->subject          = __( 'Your {site_title} order from {order_date} is shipped', 'customEmail' );

        $this->template_html    = 'emails/customer-order_tracking.php';
        $this->template_plain   = 'emails/plain/customer-order_tracking.php';

        add_action( 'woocommerce_order_status_shipped', array( $this, 'trigger' ) );

        parent::__construct();
    }

    public function trigger( $order_id )
    {
        var_dump($order_id);die();
    }

When I change my order status, nothing happens! My trigger function is never call.

Can anyone help me?

1
I would probably use $email_actions[] = 'woocommerce_order_status_shipped; instead of the transition. Though I think the real deal-killer is that you aren't passing a new instance of the class here $email_classes['WC_Expedited_Order_Email'] you are passing the included file. Include the file on the line above and then instantiate the class. See this tutorialhelgatheviking
Thank for your answer @helgatheviking . I tried, but I thought my trigger function is still not calledqhaensler
Maybe the $order_id is not passed ? What if you echo "something" before die() ?JazZ
Yes I tried but I don't seen my echo, I think my trigger function is never call but I don't understand whyqhaensler
After some research! I feel that the problem come from the status that I use in my add_action : he is added by an external module and not in the core of WooCommerce! An idea about this?qhaensler

1 Answers

1
votes

I think you misunderstood this part :

function add_expedited_order_woocommerce_email( $email_classes ) {
   $email_classes['WC_Expedited_Order_Email'] = include( plugin_dir_path( __FILE__ ) . '/class-wc-expedited-order-email.php' );
   return $email_classes;
}

You have to include the class file first and then create a new instance of this class :

function add_expedited_order_woocommerce_email( $email_classes ) {

    // include our custom email class
    require( 'includes/class-wc-expedited-order-email.php' );

    // add the email class to the list of email classes that WooCommerce loads
    $email_classes['WC_Expedited_Order_Email'] = new WC_Expedited_Order_Email();

    return $email_classes;

}

Hope it helps.