如果条件为真,请勿发送Woocommerce新客户电子邮件 [英] Do not send Woocommerce new customer email if a condition is true

查看:166
本文介绍了如果条件为真,请勿发送Woocommerce新客户电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用REST API向Woo添加新客户,则需要避免发送新客户电子邮件.REST API文档没有讨论此问题,也没有设置可防止发送"WC_Email_Customer_New_Account"电子邮件的参数

If i add a new customer to Woo using the REST API i need to avoid sending the new customer email. The REST API docs don't talk about this and there's no parameter to set that can prevent the "WC_Email_Customer_New_Account" email

我尝试了10种不同的东西,我将列出最新的东西

I've tried about 10 different things, I'll list the most recent ones

  • 直接编辑Woo源代码class-wc-emails.php.即使这样也不行,因为当我收集用户元数据时,它仍然是空白的,只有用户ID和漂亮的名字

  • Editing the Woo source directly class-wc-emails.php. Not even that works, because when i collect the user meta it's still blank and only has the user ID and nice name

创建一个检查外部API以及是否满足条件的插件,即可执行 remove_action('woocommerce_created_customer_notification',array($ email_class-> emails ['WC_Email_Customer_New_Account'],'trigger'));

Creating a plugin that checks an external API and if a condition is met does remove_action('woocommerce_created_customer_notification', array($email_class->emails['WC_Email_Customer_New_Account'], 'trigger'));

处理插件中的所有内容,但我遇到的问题与1.相同.

Processing everything inside the plugin but i have the same problem as 1.

推荐答案

我认为我成功实现了这一目标.我的目标是通过WooCommerce REST API(客户终结点)创建用户(客户),而不触发新帐户电子邮件.

I think I managed to pull it off. My goal was to create a user (customer) via WooCommerce REST API (Customers endpoint) and not to trigger the New Account email.

第一个可疑对象是 woocommerce_email_enabled_customer_new_account 过滤器.它为我们提供了 \ WP_User \ WC_Email_Customer_Completed_Order 对象.显然,第一个想法是对用户运行 get_user_meta().

The first suspect is the woocommerce_email_enabled_customer_new_account filter. It gives us the \WP_User and \WC_Email_Customer_Completed_Order objects. The first idea obviously is to just run get_user_meta() against the user.

add_filter( 'woocommerce_email_enabled_customer_new_account', function( $enabled, $user, $email ) {
    /**
     * @var bool $enabled
     * @var \WP_User $user
     * @var \WC_Email_Customer_Completed_Order $email
     */

    $isOurGuy = 'foobar' === get_user_meta( $user->ID, 'meta_key_is_so_meta', true );
    if ( $isOurGuy ) {
        return false;
    }

    return $enabled;
}, 10, 3 );

事实证明,REST终结点处理程序只能在创建用户后推送元数据,并且电子邮件通知是通过 \ WC_Emails()中的钩子隐式触发的.这意味着当我们的代码在该电子邮件挂钩期间运行时,尚无可用元数据.

As it turns out, the REST endpoint handler can only push metadata after the user is created, and email notification is triggered implicitly via a hook in \WC_Emails(). It means when our code is running during that email hook, no metadata is available yet.

接下来要检查的是去到将用户完全推到任何地方的那一刻.这将是 woocommerce_before_data_object_save ,这是一次操作.它给我们两个对象, \ WC_Customer \ WC_Customer_Data_Store . \ WC_Customer 对象具有我们的元数据,是的!让我们将现有代码封装到另一个处理程序中.

Next thing to check was to go to the moment before the user is pushed anywhere at all. That would be woocommerce_before_data_object_save, an action this time. It gives us two objects, \WC_Customer and \WC_Customer_Data_Store. The \WC_Customer object has our metadata, yay! Let's enclose the existing code into another handler.

add_action( 'woocommerce_before_data_object_save', function( $customer, $store ) {
    /**
     * @var \WC_Customer $customer
     * @var \WC_Customer_Data_Store $store
     */

    if ( !( $customer instanceof \WC_Customer ) ) { // I guess other object types may end up here, let's make sure we only get to work with customers.
        return;
    }

    $isOurGuy = 'foobar' === $customer->get_meta( 'meta_key_is_so_meta', true );
    if ( !$isOurGuy ) {
        return;
    }

    add_filter( 'woocommerce_email_enabled_customer_new_account', function( $enabled, $user, $email ) use ( $customer ) {
        /**
         * @var bool $enabled
         * @var \WP_User $user
         * @var \WC_Email_Customer_Completed_Order $email
         */
        if ( $customer->get_id() !== $user->ID ) { // Is it our guy?
            return $enabled;
        }

        return false;
    }, 10, 3 );

}, 10, 2 );

它仍然不起作用.因为这是在保存对象之前 ,所以当我们将 $ customer 捕获到我们的范围中时,该用户不存在.因此 $ customer-> get_id()返回0(零).似乎我们赶超了点-需要向前滚动. woocommerce_created_customer 似乎是一个不错的候选人.它为我们提供了新的用户ID.

It still does not work. Because this is before object save, the user did not exist at the moment when we captured $customer into our scope. So $customer->get_id() returns 0 (zero). It seems that we overshot in time a bit - need to roll forward. woocommerce_created_customer seems like a good candidate. It gives us among other things the new user ID.

让我们一起编译所有内容.

Let's compile everything together.

add_action( 'woocommerce_before_data_object_save', function( $customer, $store ) {
    /**
     * @var \WC_Customer $customer
     * @var \WC_Customer_Data_Store $store
     */

    if ( !( $customer instanceof \WC_Customer ) ) { // I guess other object types may end up here, let's make sure we only get to work with customers.
        return;
    }

    $isOurGuy = 'foobar' === $customer->get_meta( 'meta_key_is_so_meta', true );
    if ( !$isOurGuy ) {
        return;
    }

    /**
     * Hook into the Customer Created event to capture the new customer ID.
     */
    add_action( 'woocommerce_created_customer', function( $customerId ) {
        add_filter( 'woocommerce_email_enabled_customer_new_account', function( $enabled, $user, $email ) use ( $customerId ) {
            /**
             * @var bool $enabled
             * @var \WP_User $user
             * @var \WC_Email_Customer_Completed_Order $email
             */
            if ( $customerId !== $user->ID ) { // Is it our guy?
                return $enabled;
            }

            return false;
        }, 10, 3 );
    }, 1 ); // NB: Email is also hooked here with priority 10.
}, 10, 2 );

现在让我们来回顾一下.我们挂接到数据存储区并捕获保存 \ WC_Customer 对象的那一刻.我们执行基于元数据的定制逻辑来决定是否继续.然后,我们跳到创建用户以检索其ID的时刻.然后,我们在启用电子邮件?"过程中跳得更远一些.检查以实际禁用给定用户的通知.

Now let's recap. We hook into the data store and capture the moment a \WC_Customer object is saved. We perform custom metadata-based logic to decide whether to proceed. Then we skip to the moment when the user is created to retrieve their ID. Then we hop a little further in time during the "email enabled?" check to actually disable the notification for the given user.

这篇关于如果条件为真,请勿发送Woocommerce新客户电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆