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

查看:41
本文介绍了如果条件为真,请勿发送 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天全站免登陆