0

I'd like it so that when a customer orders a product from a specific category (in this instance, a voucher, which will reside in a Voucher category, and Emailable category), they receive an extra email, as well as their order confirmation, which has a jpg embedded with some stripped back information of the order (to pass along as a gift for example).

I'm nearly there, using code from this SkyVerge blog and CloudRedux. The extra voucher email basically sends to all orders that are immediately marked as processing. Instead, I'd like it to send only to those who have ordered a product that is in the 'Emailable' category.

We're using WooCommerce 2.5.5.

Thanks!

Here's my custom email class code so far:

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;

    $order_items = $this->object->get_items();

        foreach ( $order_items as $item ) {

            $product = $this->object->get_product_from_item( $item );

            $product_categories[] = wc_get_product_terms( $product->id, 'product_cat', array( 'fields' => 'names' ) ); // Three possible arguments "ids", "slugs" & "names" for "fields" parameter; based on what you want to compare the category against

        }

        if ( ! in_array( 'Emailable', $product_categories ) )  {
            return;
        }

    // 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() );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.