自定义电子邮件通知防止WooCommerce自动发送的电子邮件

huangapple go评论58阅读模式
英文:

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 &#39;Shipment and Invoice&#39; in Woocommerce, which includes a new order condition for the &#39;processing to shipped&#39; 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 &#39;shipped&#39; 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 &#39;New Order (admin)&#39; and &#39;New Order (customer)&#39; etc. I don&#39;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 . &#39;includes/classes/ShippedOrderEmail.php&#39; );
$email_classes[&#39;WC_Shipped_Order_Email&#39;] = 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-&gt;id             = &#39;customer-shipped-order&#39;;
$this-&gt;customer_email = true;
$this-&gt;title          = &#39;Shipped order&#39;;
$this-&gt;description    = &#39;This notification is an order notification sent to customers after shipping that includes product details and a shipping tracking number.&#39;;
$this-&gt;template_html  = &#39;emails/customer-shipped-order.php&#39;;
$this-&gt;placeholders   = array(
&#39;{order_date}&#39;   =&gt; &#39;&#39;,
&#39;{order_number}&#39; =&gt; &#39;&#39;,
);
add_action( &#39;woocommerce_order_status_shipped_notification&#39;, array( $this, &#39;trigger&#39; ) );
parent::__construct();
}
public function trigger( $order_id ) {
if ( ! $order_id )
return;
if ( $order_id &amp;&amp; ! is_a( $order, &#39;WC_Order&#39; ) ) {
$order = wc_get_order( $order_id );
}
if ( is_a( $order, &#39;WC_Order&#39; ) ) {
$this-&gt;object                         = $order;
$this-&gt;recipient                      = $this-&gt;object-&gt;get_billing_email();
$this-&gt;placeholders[&#39;{order_date}&#39;]   = wc_format_datetime( $this-&gt;object-&gt;get_date_created() );
$this-&gt;placeholders[&#39;{order_number}&#39;] = $this-&gt;object-&gt;get_order_number();
}
if ( ! $this-&gt;is_enabled() || ! $this-&gt;get_recipient() )
return;
$this-&gt;send( $this-&gt;get_recipient(), $this-&gt;get_subject(), $this-&gt;get_content(), $this-&gt;get_headers(), $this-&gt;get_attachments() );
}
public function get_content_html() {
return wc_get_template_html(
$this-&gt;template_html,
array(
&#39;order&#39;              =&gt; $this-&gt;object,
&#39;email_heading&#39;      =&gt; $this-&gt;get_heading(),
&#39;additional_content&#39; =&gt; $this-&gt;get_additional_content(),
&#39;sent_to_admin&#39;      =&gt; false,
&#39;plain_text&#39;         =&gt; false,
&#39;email&#39;              =&gt; $this,
)
);
}
public function init_form_fields() {
$placeholder_text  = sprintf( __( &#39;Available placeholders: %s&#39;, &#39;woocommerce&#39; ), &#39;&lt;code&gt;&#39; . esc_html( implode( &#39;&lt;/code&gt;, &lt;code&gt;&#39;, array_keys( $this-&gt;placeholders ) ) ) . &#39;&lt;/code&gt;&#39; );
$this-&gt;form_fields = array(
&#39;enabled&#39;   =&gt; array(
&#39;title&#39;     =&gt; __( &#39;Enable/Disable&#39;, &#39;woocommerce&#39; ),
&#39;type&#39;      =&gt; &#39;checkbox&#39;,
&#39;label&#39;     =&gt; __( &#39;Enable this email notification&#39;, &#39;woocommerce&#39; ),
&#39;default&#39;   =&gt; &#39;yes&#39;,
),
&#39;subject&#39;   =&gt; array(
&#39;title&#39;       =&gt; __( &#39;Subject&#39;, &#39;woocommerce&#39; ),
&#39;type&#39;        =&gt; &#39;text&#39;,
&#39;desc_tip&#39;    =&gt; true,
&#39;description&#39; =&gt; $placeholder_text,
&#39;placeholder&#39; =&gt; $this-&gt;get_default_subject(),
&#39;default&#39;     =&gt; &#39;&#39;,
),
&#39;heading&#39;            =&gt; array(
&#39;title&#39;       =&gt; __( &#39;Email heading&#39;, &#39;woocommerce&#39; ),
&#39;type&#39;        =&gt; &#39;text&#39;,
&#39;desc_tip&#39;    =&gt; true,
&#39;description&#39; =&gt; $placeholder_text,
&#39;placeholder&#39; =&gt; $this-&gt;get_default_heading(),
&#39;default&#39;     =&gt; &#39;&#39;,
),
&#39;additional_content&#39; =&gt; array(
&#39;title&#39;       =&gt; __( &#39;Additional content&#39;, &#39;woocommerce&#39; ),
&#39;description&#39; =&gt; __( &#39;Text to appear below the main email content.&#39;, &#39;woocommerce&#39; ) . &#39; &#39; . $placeholder_text,
&#39;css&#39;         =&gt; &#39;width:400px; height: 75px;&#39;,
&#39;placeholder&#39; =&gt; __( &#39;N/A&#39;, &#39;woocommerce&#39; ),
&#39;type&#39;        =&gt; &#39;textarea&#39;,
&#39;default&#39;     =&gt; $this-&gt;get_default_additional_content(),
&#39;desc_tip&#39;    =&gt; true,
),
);
}

}


</details>
# 答案1
**得分**: 0
After 1-2 days and re-check my code, I found problem.
I misspelled this part:
WRONG:
&gt; function shipped_email_actions( $action )
CORRECTED:
&gt; 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:
&gt; function shipped_email_actions( $action )
CORRECTED:
&gt; function shipped_email_actions( $actions)
I edited my code (and also message). Now it works perfectly. 
</details>

huangapple
  • 本文由 发表于 2023年4月20日 06:14:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定