代码不适用于基于产品属性的自定义WC客户订单电子邮件

时间:2019-07-03 13:26:46

标签: woocommerce email-integration

两年前,我修改了Skyverge的代码,以便在客户在线订购([{Adding a custom woocommerce email based on the product attribute)时生成WC自定义电子邮件。

一切正常,但是我现在想用新的Woocommerce版本进行更新。深入研究后,我在下面编写了代码,但是我准备的插件不再起作用。

plugin文件夹包含一个基本文件woocommerce-custom-order-email.php,一个包含自定义类的类文件夹(它们是基于两个不同属性的两个)和一个包含电子邮件自定义模板的文件夹。

woocommerce-custom-order-email.php代码

/**
 *  Add a custom email to the list of emails WooCommerce should load
 *
 * @since 1.0
 * @param array $email_classes available email classes
 * @return array filtered available email classes
 */
class Custom_WC_Email {
    /**
     * Custom_WC_Email constructor.
     */
    public function __construct() {
    // Filtering the emails and adding our own email.
        add_action( 'woocommerce_email_classes', array( $this, 'register_email' ), 90, 1 );
    // Absolute path to the plugin folder.
    }
        /**
         *  Add a custom email to the list of emails WooCommerce should load
         *
         * @param array $email_classes available email classes
         * @return array filtered available email classes
         */
    public function register_email( $emails ) {
        require_once 'classes/class-wc-email-customer-processing-idp-order.php';
        require_once 'classes/class-wc-email-customer-processing-csr-order.php';
        $emails['WC_Email_Customer_Processing_IDP_Order'] = new WC_Email_Customer_Processing_IDP_Order();
        $emails['WC_Email_Customer_Processing_CSR_Order'] = new WC_Email_Customer_Processing_CSR_Order();
        return $emails;
    }   
}
new Custom_WC_Email();

类代码(我使用class-wc-email-customer-processing-csr-order.php)

 * Class WC_Email_Customer_Processing_CSR_Order file.
 *
 * @package WooCommerce-Custom-Email
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}
define('WOOCOMMERCE_CUSTOM_EMAIL_PLUGIN_DIR', dirname(__FILE__) );
if ( ! class_exists( 'WC_Email_Customer_Processing_CSR_Order', false ) ) :
    /**
     * Customer Processing Order Email.
     *
     * An email sent to the customer when a new order is paid for.
     *
     * @class       WC_Email_Customer_Processing_CSR_Order
     * @version     3.5.0
     * @package     WooCommerce-Custom-Email/Classes
     * @extends     WC_Email
     */
    class WC_Email_Customer_Processing_CSR_Order extends WC_Email {
        /**
         * Constructor.
         */
        public function __construct() {
            // set ID, this simply needs to be a unique name
            $this->id             = 'customer_processing_idp_order';
            $this->customer_email = true;
            // this is the title in WooCommerce Email settings
            $this->title          = __( 'CSR Cruise Processing Order', 'woocommerce_custom_email_domain' );
            // this is the description in WooCommerce email settings
            $this->description    = __( 'This is an order notification sent to customers who bought a CSR Cruise containing order details after payment.', 'woocommerce_custom_email_domain' );
            $this->template_html  = 'emails/customer-processing-order-csr.php';//qui posso duplicare il template e farne uno ad hoc per questo tipo di mail con i file attached
            $this->template_plain = 'emails/plain/customer-processing-order-csr.php';
            $this->template_base  = WOOCOMMERCE_CUSTOM_EMAIL_PLUGIN_PATH . 'templates/';
            $this->placeholders   = array(
                '{site_title}'   => $this->get_blogname(),
                '{order_date}'   => '',
                '{order_number}' => '',
            );
            // Triggers for this email.
            add_action( 'woocommerce_order_status_cancelled_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
            add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
            add_action( 'woocommerce_order_status_on-hold_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
            add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
            // Call parent constructor.
            parent::__construct();
        }
        /**
         * Get email subject.
         *
         * @since  3.1.0
         * @return string
         */
        public function get_default_subject() {
            return __( 'Your {site_title} order has been received!', 'woocommerce' );
        }
        /**
         * Get email heading.
         *
         * @since  3.1.0
         * @return string
         */
        public function get_default_heading() {
            return __( 'Thank you for your order', 'woocommerce' );
        }
        /**
         * Trigger the sending of this email.
         *
         * @param int            $order_id The order ID.
         * @param WC_Order|false $order Order object.
         */
        public function trigger( $order_id, $order = false ) {
            $trigger = false; //it sets trigger as faulse--> it will be switched to true following the below conditions (cusom code)
            $this->setup_locale();
            if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
                $order = wc_get_order( $order_id );
            }
            if ( is_a( $order, 'WC_Order' ) ) {
                $this->object                         = $order;
                $this->recipient                      = $this->object->get_billing_email();
                $this->placeholders['{order_date}']   = wc_format_datetime( $this->object->get_date_created() );
                $this->placeholders['{order_number}'] = $this->object->get_order_number();
            }
            //** Custom code**//
                //find first the product_id
                $items = $order->get_items();
                 foreach ( $items as $item ) {
                    $product_id = $item['product_id'];
                }
                  //from the product_id get the product attribute
                $product = new WC_Product( $product_id );  // create an object of WC_Product class
                $patt = $product->get_attributes();  // call get_attribute method
                if ( array_key_exists('pa_csr-dates' , $patt)) 
                {
                    $trigger = true;
                }
                if ( $this->is_enabled() && $this->get_recipient() && $trigger === true) {
                    $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
                }
                else {return;}
            //** end of custom code**//
            $this->restore_locale();
        }
        /**
         * Get content html.
         *
         * @return string
         */
        public function get_content_html() {
            return wc_get_template_html(
                $this->template_html,
                array(
                    'order'         => $this->object,
                    'email_heading' => $this->get_heading(),
                    'sent_to_admin' => false,
                    'plain_text'    => false,
                    'email'         => $this,
                )
            );
        }
        /**
         * Get content plain.
         *
         * @return string
         */
        public function get_content_plain() {
            return wc_get_template_html(
                $this->template_plain,
                array(
                    'order'         => $this->object,
                    'email_heading' => $this->get_heading(),
                    'sent_to_admin' => false,
                    'plain_text'    => true,
                    'email'         => $this,
                )
            );
        }
    }
endif;
return new WC_Email_Customer_Processing_CSR_Order(); 

我认为问题出在触发器自定义代码中,但是我没有找到有效调试它的方法。有人有什么主意吗?

1 个答案:

答案 0 :(得分:0)

好吧,我查看了@LoicTheAztec https://stackoverflow.com/a/44708344/4275508所发布的有关如何使用新的WC 3.0版本检索product_id的信息,并按如下所示修改了自定义代码

#!/usr/bin/python3
import  pytz
import random
from datetime import datetime
randZoneName = random.choice(pytz.all_timezones)
randZone=datetime.now(pytz.timezone(randZoneName))
offset=randZone.strftime('%z')
print("(GMT%s:%s) %s"%(offset[:3],offset[3:],randZoneName))

但仍然无法正常工作。最糟糕的是它给了e内部错误