Woocommerce, different order emails depending on the product are ok

I need to send two different "new_order" emails depending on the products ordered. The point is that the products are stored in different locations and I need to send one email containing only the products present in store 1 and another mail containing the products present in store 2. If only products located in store 2 were ordered 1, letter 2 does not need to be sent at all, and vice versa.

Storage information is a custom meta field for each product.

I added a custom email class that is a copy class-wc-email-new-order

, but only with the name changed where needed.

I was looking through hooks and filters for this, but I couldn't go all the way. I also thought about overriding the file email-order-details.php

and checking each metafile field, but I'm not sure if that's the right way. I think the email will still be sent even if there are no products in the table.

My problem is that I don't know which of the emails are being sent when using different filters, so I cannot check this condition.

Would you only create two new emails with templates and that's it, or is this what I'm missing here?

If I can clarify something, please let me know.

Edit: After talking a bit with @helgatheviking I realized that I needed to clarify that emails regarding store 1 should only be sent when the order contains products located in store 1 and emails regarding store 2 should be sent only when ordering contains products located in warehouse 2. If an order contains both types of products, both emails must be sent, but only their respective products are displayed.

+1


source to share


2 answers


After thinking about it and reflecting on what @helgatheviking said, I was able to come up with a solution.

I have copied class-wc-new-order

and created two new classes just like the original. I changed the names of ids and classes to store 1 and store 2 respectively.

I am loading the classes by doing the following: CHANGE WRONG METHOD, SEE ALL INSTEAD

//Add our custom class to WC email classes
add_filter( 'woocommerce_email_classes', [ $this, 'custom_order_email_add_email_classes' ], 10, 1 );

function custom_order_email_add_email_classes( $email_classes ) {

     require( CUSTOM_ORDER_EMAIL_PLUGIN_DIR . 'classes/class-wc-email-new-order-storage1.php' );
     require( CUSTOM_ORDER_EMAIL_PLUGIN_DIR . 'classes/class-wc-email-new-order-storage2.php' );

     $email_classes['WC_Email_New_Order_Storage1']  = new WC_Email_New_Order_Storage1(); 
     $email_classes['WC_Email_New_Order_Storage2']  = new WC_Email_New_Order_Storage2();

     return $email_classes;
}

      

Note that this is in a dedicated plugin, you will have to change the code a little if you use it in your .php functions.

Then I edited the trigger()

-function in both email classes:

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

    $trigger = false;

    if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
        $order = wc_get_order( $order_id );
    }

    if ( is_a( $order, 'WC_Order' ) ) {
        $this->object                  = $order;
        $this->find['order-date']      = '{order_date}';
        $this->find['order-number']    = '{order_number}';
        $this->replace['order-date']   = wc_format_datetime( $this->object->get_date_created() );
        $this->replace['order-number'] = $this->object->get_order_number();

        $items = $order->get_items();

        foreach ( $items as $item_id => $item ) {

            $product = $item->get_product();

            if ( $product->get_meta( '_product_storage' ) == 'storage2' ) {//storage1 in the other email class
                $trigger = true;
            }
        }
    }

    if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
        return;
    }

    if( $trigger === true) {
        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    }
    else {
        return;
    }
}

      



Everything seems to behave as it should, but I'll do some more tests before accepting my own answer. Thanks again to Helgatheviking for getting me on the right track. :)

Edit: Forgot to add my override email-order-details.php

. This is added just below<tbody>

if ( $email->id == 'new_order_storage1' ) {

        $items = $order->get_items();
        foreach( $items as $item_id => $item ) {

            $product = $item->get_product();

            if ( $product->get_meta( '_product_storage' ) == 'storage1' ) {
        ?>
                <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
                    <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php

                        // Show title/image etc
                        if ( $show_image ) {
                            echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ( $product->get_image_id() ? current( wp_get_attachment_image_src( $product->get_image_id(), 'thumbnail' ) ) : wc_placeholder_img_src() ) . '" alt="' . esc_attr__( 'Product image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-' . ( is_rtl() ? 'left' : 'right' ) . ': 10px;" /></div>', $item );
                        }

                        // Product name
                        echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false );

                        // SKU
                        if ( $show_sku && is_object( $product ) && $product->get_sku() ) {
                            echo ' (#' . $product->get_sku() . ')';
                        }

                        // allow other plugins to add additional product information here
                        do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );

                        wc_display_item_meta( $item );

                        if ( $show_download_links ) {
                            wc_display_item_downloads( $item );
                        }

                        // allow other plugins to add additional product information here
                        do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );

                    ?></td>
                    <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item ); ?></td>
                    <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
                </tr>
            <?php
            }
        }
    }
    else if ( $email->id == 'new_order_storage2' ) {
        $items = $order->get_items();
        foreach( $items as $item_id => $item ) {

            $product = $item->get_product();

            if ( $product->get_meta( '_product_storage' ) == 'storage2' ) {
        ?>
                <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
                    <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php

                        // Show title/image etc
                        if ( $show_image ) {
                            echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ( $product->get_image_id() ? current( wp_get_attachment_image_src( $product->get_image_id(), 'thumbnail' ) ) : wc_placeholder_img_src() ) . '" alt="' . esc_attr__( 'Product image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-' . ( is_rtl() ? 'left' : 'right' ) . ': 10px;" /></div>', $item );
                        }

                        // Product name
                        echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false );

                        // SKU
                        if ( $show_sku && is_object( $product ) && $product->get_sku() ) {
                            echo ' (#' . $product->get_sku() . ')';
                        }

                        // allow other plugins to add additional product information here
                        do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );

                        wc_display_item_meta( $item );

                        if ( $show_download_links ) {
                            wc_display_item_downloads( $item );
                        }

                        // allow other plugins to add additional product information here
                        do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );

                    ?></td>
                    <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo apply_filters( 'woocommerce_email_order_item_quantity', $item->get_quantity(), $item ); ?></td>
                    <td class="td" style="text-align:<?php echo $text_align; ?>; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
                </tr>
            <?php
            }
        }
    }
    else {
     echo wc_get_email_order_items( $order, array(
        'show_sku'      => $sent_to_admin,
        'show_image'    => false,
        'image_size'    => array( 32, 32 ),
        'plain_text'    => $plain_text,
        'sent_to_admin' => $sent_to_admin,
    ) ); 
    }

      

The code displaying the order items is taken directly from order-email-items

. I'm not sure if there is a better way to make this thought, feel free to correct me if this is the case.

EDIT , Skyverge used the old method to load custom email classes. Correct way to load custom email classes:

    //Add our custom class to WC email classes
    add_filter( 'woocommerce_email_classes', [ $this, 'custom_order_email_add_email_classes' ], 10, 1 );

    function custom_order_email_add_email_classes( $email_classes ) {

         $email_classes['WC_Email_New_Order_Storage1']  = include( CUSTOM_ORDER_EMAIL_PLUGIN_DIR . 'classes/class-wc-email-new-order-storage1.php' );
         $email_classes['WC_Email_New_Order_Storage2']  = include( CUSTOM_ORDER_EMAIL_PLUGIN_DIR . 'classes/class-wc-email-new-order-storage2.php' );

     return $email_classes;
}

      

+3


source


You can customize them like this: https://www.sellwithwp.com/customizing-woocommerce-order-emails/



Var_dump that's $ order object, but I'm pretty sure you have access to the product id, which you can then use to figure out which store it belongs to and adjust the email content accordingly.

0


source







All Articles