web-dev-qa-db-ja.com

WooCommerceで保留中の注文ステータスについて管理者にメール通知を送信します

WooCommerceでは、顧客がカートからチェックアウトして注文を送信すると、支払いが処理されない場合、注文は「保留中」の支払いに設定されます。管理者はについての電子メールを受信して​​いません。

この種の注文については、管理者にメールを送信したいと思います。どうすればいいですか?

7
burhan jamil

PDATE 2(CélineGarelのおかげでwoocommerce_new_orderからwoocommerce_checkout_order_processedに変更)

このコードは、新しい注文が保留中のステータスを取得の場合に発生する可能性のあるすべての場合に発生し、「新しい注文」の電子メール通知を自動的にトリガーします。

// New order notification only for "Pending" Order status
add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );
function pending_new_order_notification( $order_id ) {

    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Only for "pending" order status
    if( ! $order->has_status( 'pending' ) ) return;

    // Send "New Email" notification (to admin)
    WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
}

コードは、アクティブな子テーマ(またはテーマ)のfunction.phpファイル、または任意のプラグインファイルに含まれます。

このコードはテスト済みで、WooCommerceバージョン2.6.xおよび3以降で動作します。


コードのよりカスタマイズ可能なバージョン(必要な場合)。これにより、保留中の注文がより見やすくなります

// New order notification only for "Pending" Order status
add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );
function pending_new_order_notification( $order_id ) {
    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );

    // Only for "pending" order status
    if( ! $order->has_status( 'pending' ) ) return;

    // Get an instance of the WC_Email_New_Order object
    $wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order'];

    ## -- Customizing Heading, subject (and optionally add recipients)  -- ##
    // Change Subject
    $wc_email->settings['subject'] = __('{site_title} - New customer Pending order ({order_number}) - {order_date}');
    // Change Heading
    $wc_email->settings['heading'] = __('New customer Pending Order'); 
    // $wc_email->settings['recipient'] .= ',[email protected]'; // Add email recipients (coma separated)

    // Send "New Email" notification (to admin)
    $wc_email->trigger( $order_id );
}

コードは、アクティブな子テーマ(またはテーマ)のfunction.phpファイル、または任意のプラグインファイルに含まれます。

このコードはテスト済みで、WooCommerceバージョン2.6.xおよび3以降で動作します。

このバージョンでは、電子メールの見出し、件名、受信者の追加をカスタマイズできます...

13
LoicTheAztec

私はLoicTheAztec回答で試しました、@ LoicTheAztecはあなたの素晴らしいコードに感謝します。

アクションフックを_woocommerce_new_order_から_woocommerce_checkout_order_processed_に変更して機能させました。

アクションは次のとおりです:add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );

お役に立てば幸いです。

3
Céline Garel