简体   繁体   中英

Add my if statement in Woocommerce WC_Email class

I know this is probably super simple but I cant seem to get it. I have developed a plugin for Woocommerce that send an additional email to customers only when a digital download is purchased.

I have the everything working fine and it currently sends the email perfectly. Now I am trying to only send it when a download is purchase so I know I should be using this: if ($order->has_downloadable_item() ){

However if I wrap the class WC_Gift_Order_Email extends WC_Email in this if statement I get the following error:

Fatal error: Call to a member function has_downloadable_item() on a non-object in .../wp-content/plugins/woocommerce-gift-receipt-emails/includes/class-wc-gift-receipt-order-email.php on line 12

So what is the where would be the best place to add this if statement to make sure it only sends when a digital download is purchased? I have the correct code, just dont know where to put it or best way to implement it.

Here is my woocommerce-gift-receipt-order-email.php file:

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

/**
 *  Add a custom email to the list of emails WooCommerce should load
 *
 * @since 0.1
 * @param array $email_classes available email classes
 * @return array filtered available email classes
 */
function add_gift_receipt_order_woocommerce_email( $email_classes ) {

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

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

    return $email_classes;

}
add_filter( 'woocommerce_email_classes', 'add_gift_receipt_order_woocommerce_email' );

Should I add it in there somewhere or should I add it to my class-ac-gift-receipt-order-email.php file?

Ha, silly me -- The miracles of coffee..

I needed to add the if statement to my WC_Email class and use it as an object.

The correct (working) method:

function trigger( $order_id ) {
                global $woocommerce;

                if ( $order_id ) {
                        $this->object                 = new WC_Order( $order_id );
                        if ($this->object->has_downloadable_item()) {
                        $this->recipient        = $this->object->billing_email;

                        $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;

                $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM