Два года назад я изменил код из Skyverge для создания пользовательских писем WC, когда клиент делал заказ онлайн ([ Добавление пользовательского электронного письма woocommerce на основе атрибута продукта ).
Всеработал отлично, но теперь я хотел бы обновить его до новой версии Woocommerce.Покопавшись, я написал приведенные ниже коды, но подготовленный мной плагин больше не работает.
Папка плагина содержит основной файл woocommerce-custom-order-email.php, папку классов, содержащую пользовательскийклассы (они основаны на двух разных атрибутах) и папка с пользовательскими шаблонами электронной почты.
woocommerce-custom-order-email.php code
/**
* 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();
Я полагаю, что проблема заключается в пользовательском коде триггера, но я не нахожу способа его эффективной отладки.У кого-нибудь есть идеи?