英文:
Custom email notification prevents automatically sent emails by Woocommerce
问题
I recently created a plugin for 'Shipment and Invoice' in Woocommerce, which includes a new order condition for the 'processing to shipped' action. The plugin has a dropdown, some actions, and other features. I also added a new email notification feature that sends an email to the customer when the order status is changed to 'shipped' with the shipping notification.
However, I ran into a problem with the plugin. The code I added for the email notification feature seems to prevent Woocommerce from automatically sending the default email notifications, such as 'New Order (admin)' and 'New Order (customer)' etc. I don't know what went wrong, because I am relatively new to coding and solving these problems.
By the way, when I try to send email notification manually, emails sent as it should be. But when new order placed, completed or on any other status changes, nothing happens.
Here my code pieces;
First I registered custom status for shipped status:
add_action( 'init', 'register_shipped_status', 20 );
function register_shipped_status() {
register_post_status( 'wc-shipped', array(
'label' => 'Shipped',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => 'Shipped <span class="count">(%s)</span>',
) );
}
And then I added this status to related places (ie. dropdown)
add_filter( 'wc_order_statuses', 'shipped_wc_order_statuses', 20, 1 );
function shipped_wc_order_statuses( $order_statuses ) {
$order_statuses[ 'wc-shipped' ] = 'Shipped';
return $order_statuses;
}
add_filter( 'bulk_actions-edit-shop_order', 'shipped_dropdown_bulk_actions_shop_order');
function shipped_dropdown_bulk_actions_shop_order( $actions ) {
$actions['mark_shipped'] = 'Change status to shipped';
return $actions;
}
Then registered for email classes:
add_filter( 'woocommerce_email_classes', 'add_shipped_order_woocommerce_email' );
function add_shipped_order_woocommerce_email( $email_classes ) {
require( SHIPMENT_INVOICE_PLUGIN_DIR . 'includes/classes/ShippedOrderEmail.php' );
$email_classes['WC_Shipped_Order_Email'] = new WC_Shipped_Order_Email();
return $email_classes;
}
And added for email notification action and triggers:
add_filter( 'woocommerce_email_actions', 'shipped_email_actions', 20, 1 );
function shipped_email_actions( $actions ) {
$actions[] = 'woocommerce_order_status_wc-shipped';
return $actions;
}
add_action( 'woocommerce_order_status_wc-shipped', array( 'WC_Emails', 'send_transactional_email' ), 10, 1 );
add_action('woocommerce_order_status_shipped', 'shipped_status_trigger_email_notification', 10, 2 );
function shipped_status_trigger_email_notification( $order_id, $order ) {
WC()->mailer()->get_emails()['WC_Shipped_Order_Email']->trigger( $order_id );
}
And here my extended class for WC_Shipped_Order_Email
(PS: this piece is in ShippedOrderEmail.php I mentioned before while registering email class)
class WC_Shipped_Order_Email extends WC_Email {
public function __construct() {
$this->id = 'customer-shipped-order';
$this->customer_email = true;
$this->title = 'Shipped order';
$this->description = 'This notification is an order notification sent to customers after shipping that includes product details and a shipping tracking number.';
$this->template_html = 'emails/customer-shipped-order.php';
$this->placeholders = array(
'{order_date}' => '',
'{order_number}' => '',
);
add_action( 'woocommerce_order_status_shipped_notification', array( $this, 'trigger' ) );
parent::__construct();
}
public function trigger( $order_id ) {
if ( ! $order_id )
return;
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();
}
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() );
}
public function get_content_html() {
return wc_get_template_html(
$this->template_html,
array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
'additional_content' => $this->get_additional_content(),
'sent_to_admin' => false,
'plain_text' => false,
'email' => $this,
)
);
}
public function init_form_fields() {
$placeholder_text = sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . esc_html( implode( '</code>, <code>', array_keys( $this->placeholders ) ) ) . '</code>' );
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable this email notification', 'woocommerce' ),
'default' => 'yes',
),
'subject' => array(
'title' => __( 'Subject', 'woocommerce' ),
'type' => 'text',
'desc_tip' => true,
'description' => $placeholder_text,
'placeholder' => $this->get_default_subject(),
'default' => '',
),
'heading' => array(
'title' => __( 'Email heading', 'woocommerce' ),
'type' => 'text',
'desc_tip' => true,
'description' => $placeholder_text,
'placeholder' => $this->get_default_heading(),
'default' => '',
),
'additional_content' => array(
'title' => __( 'Additional content', 'woocommerce' ),
'description' => __( 'Text to appear below the main email content.', 'woocommerce' ) . ' ' . $placeholder_text,
'css' => 'width:400px; height: 75px;',
'placeholder' => __( 'N/A', 'woocommerce' ),
'
<details>
<summary>英文:</summary>
I recently created a plugin for 'Shipment and Invoice' in Woocommerce, which includes a new order condition for the 'processing to shipped' action. The plugin has a dropdown, some actions, and other features. I also added a new email notification feature that sends an email to the customer when the order status is changed to 'shipped' with the shipping notification.
However, I ran into a problem with the plugin. The code I added for the email notification feature seems to prevent Woocommerce from automatically sending the default email notifications, such as 'New Order (admin)' and 'New Order (customer)' etc. I don't know what went wrong, because I am relatively new to coding and solving these problems.
By the way, when I try to send email notification manually, emails sent as it should be. But when new order placed, completed or on any other status changes, nothing happens.
Here my code pieces;
First I registered custom status for *shipped* status:
add_action( 'init', 'register_shipped_status', 20 );
function register_shipped_status() {
register_post_status( 'wc-shipped', array(
'label' => 'Shipped',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => 'Shipped <span class="count">(%s)</span>',
) );
}
And then I added this status to related places (ie. dropdown)
add_filter( 'wc_order_statuses', 'shipped_wc_order_statuses', 20, 1 );
function shipped_wc_order_statuses( $order_statuses ) {
$order_statuses[ 'wc-shipped' ] = 'Shipped';
return $order_statuses;
}
add_filter( 'bulk_actions-edit-shop_order', 'shipped_dropdown_bulk_actions_shop_order');
function shipped_dropdown_bulk_actions_shop_order( $actions ) {
$actions['mark_shipped'] = 'Change status to shipped';
return $actions;
}
Then registered for email classes:
add_filter( 'woocommerce_email_classes', 'add_shipped_order_woocommerce_email' );
function add_shipped_order_woocommerce_email( $email_classes ) {
require( SHIPMENT_INVOICE_PLUGIN_DIR . 'includes/classes/ShippedOrderEmail.php' );
$email_classes['WC_Shipped_Order_Email'] = new WC_Shipped_Order_Email();
return $email_classes;
}
And added for email notification action and triggers:
add_filter( 'woocommerce_email_actions', 'shipped_email_actions', 20, 1 );
function shipped_email_actions( $actions ) {
$actions[] = 'woocommerce_order_status_wc-shipped';
return $actions;
}
add_action( 'woocommerce_order_status_wc-shipped', array( 'WC_Emails', 'send_transactional_email' ), 10, 1 );
add_action('woocommerce_order_status_shipped', 'shipped_status_trigger_email_notification', 10, 2 );
function shipped_status_trigger_email_notification( $order_id, $order ) {
WC()->mailer()->get_emails()['WC_Shipped_Order_Email']->trigger( $order_id );
}
And here my extended class for *WC_Shipped_Order_Email*
*(PS: this piece is in ShippedOrderEmail.php I mentioned before while registering email class)*
class WC_Shipped_Order_Email extends WC_Email {
public function __construct() {
$this->id = 'customer-shipped-order';
$this->customer_email = true;
$this->title = 'Shipped order';
$this->description = 'This notification is an order notification sent to customers after shipping that includes product details and a shipping tracking number.';
$this->template_html = 'emails/customer-shipped-order.php';
$this->placeholders = array(
'{order_date}' => '',
'{order_number}' => '',
);
add_action( 'woocommerce_order_status_shipped_notification', array( $this, 'trigger' ) );
parent::__construct();
}
public function trigger( $order_id ) {
if ( ! $order_id )
return;
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();
}
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() );
}
public function get_content_html() {
return wc_get_template_html(
$this->template_html,
array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
'additional_content' => $this->get_additional_content(),
'sent_to_admin' => false,
'plain_text' => false,
'email' => $this,
)
);
}
public function init_form_fields() {
$placeholder_text = sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . esc_html( implode( '</code>, <code>', array_keys( $this->placeholders ) ) ) . '</code>' );
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable this email notification', 'woocommerce' ),
'default' => 'yes',
),
'subject' => array(
'title' => __( 'Subject', 'woocommerce' ),
'type' => 'text',
'desc_tip' => true,
'description' => $placeholder_text,
'placeholder' => $this->get_default_subject(),
'default' => '',
),
'heading' => array(
'title' => __( 'Email heading', 'woocommerce' ),
'type' => 'text',
'desc_tip' => true,
'description' => $placeholder_text,
'placeholder' => $this->get_default_heading(),
'default' => '',
),
'additional_content' => array(
'title' => __( 'Additional content', 'woocommerce' ),
'description' => __( 'Text to appear below the main email content.', 'woocommerce' ) . ' ' . $placeholder_text,
'css' => 'width:400px; height: 75px;',
'placeholder' => __( 'N/A', 'woocommerce' ),
'type' => 'textarea',
'default' => $this->get_default_additional_content(),
'desc_tip' => true,
),
);
}
}
</details>
# 答案1
**得分**: 0
After 1-2 days and re-check my code, I found problem.
I misspelled this part:
WRONG:
> function shipped_email_actions( $action )
CORRECTED:
> function shipped_email_actions( $actions)
I edited my code (and also message). Now it works perfectly.
<details>
<summary>英文:</summary>
After 1-2 days and re-check my code, I found problem.
I misspelled this part:
WRONG:
> function shipped_email_actions( $action )
CORRECTED:
> function shipped_email_actions( $actions)
I edited my code (and also message). Now it works perfectly.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论