获取新订单电子邮件通知中的客户订单数量

3
我希望在WooCommerce的“新订单”电子邮件通知中指示是否为重复客户。
看起来很简单,但我尝试了约5种不同的方法都没有成功。我尝试将此放入2个不同的挂钩中:
- woocommerce_email_after_order_table - woocommerce_email_subject_new_order 似乎 wc_get_customer_order_count($user->ID) 应该可以工作,但似乎$user对象没有传递到这些挂钩函数中,对吗?
我还想知道当客户是访客而不是注册用户时是否可能实现,也许通过比较电子邮件地址?
谢谢
1个回答

3

WooCommerce电子邮件通知与订单相关。

woocommerce_email_after_order_table挂钩中,你可以在挂钩的自定义函数中使用订单对象作为参数,并且还有$email对象。

通过该$order对象,你可以以以下方式获取用户ID

$user_id = $user_id = $order->get_user_id();
$email 对象可以用于定位新订单邮件通知。
因此,代码将如下所示:
add_action( 'woocommerce_email_after_order_table', 'customer_order_count', 10, 4);
function customer_order_count( $order, $sent_to_admin, $plain_text, $email ){

    if ( $order->get_user_id() > 0 ){

        // Targetting new orders (that will be sent to customer and to shop manager)
        if ( 'new_order' == $email->id ){

            // Getting the user ID
            $user_id = $order->get_user_id();

            // Get the user order count
            $order_count = wc_get_customer_order_count( $user_id );

            // Display the user order count
            echo '<p>Customer order count: '.$order_count.'</p>';

        }
    }
}

你也可以使用 woocommerce_email_before_order_table 钩子,例如...
代码放在你正在使用的自定义主题(或主题)的function.php文件中,也可以放在任何插件文件中。
此代码已经过测试并且有效。

太好了,谢谢!真希望我早点问你。:-P - protohominid
有趣的是,无论我使用哪个电子邮件钩子,wc_get_customer_order_count 始终似乎比实际订单数少 1,好像它没有包括新订单。记住这一点... - Gavin Simpson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接