简体   繁体   English

在“收到的WooCommerce订单(谢谢)”页面中检查新客户

[英]Check for new customers in WooCommerce Order received (Thankyou) page

I am trying to show a custom message to new all customer (not returning customer) in Thankyou page and I am using " Checking if customer has already bought something in WooCommerce " answer code. 我试图在Thankyou页面上向新的所有客户(不回头客户)显示自定义消息,并且我正在使用检查客户是否已经在WooCommerce中购买了商品答案代码。

The issue is that can't detect if a customer has bought before. 问题是无法检测到客户是否曾经购买过。 For example if I use the current code in thank You page the new customer become older customer. 例如,如果我在“谢谢”页面中使用当前代码,那么新客户将成为老客户。

SO my question is: How can I check if a customer bought earlier any product not in the current order? 因此,我的问题是:如何检查客户是否较早购买了当前订单中没有的任何产品?

Here is my code attempt, that doesn't work as it should: 这是我的代码尝试,无法正常工作:

function has_bought_before( $user_id = 0 ) {
    global $wpdb;
    $customer_id = $user_id == 0 ? get_current_user_id() : $user_id;
    $paid_order_statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );

    $results = $wpdb->get_col( "
        SELECT p.ID FROM {$wpdb->prefix}posts AS p
        INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
        WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $paid_order_statuses ) . "' )
        AND p.post_type LIKE 'shop_order'
        AND pm.meta_key = '_customer_user'
        AND pm.meta_value = $customer_id
    " );

    // Count number of orders and return a boolean value depending if higher than 0
    return count( $results ) > 0 ? true : false;
}



add_filter('woocommerce_thankyou_order_received_text', 'woo_change_order_received_text', 10, 2 );
function woo_change_order_received_text( $str, $order ) {
    if( has_bought_before() ){
        $new_str = $str . ' Welcome Back Again.';
    }else{
        $new_str = $str . ' Welcome To our site You will get 10% discount on your next order.';
    }
    return $new_str;
}

On thankyou page you are targeting the next paid order, so you need to make a little change in the conditional function has_bought_before() , at the end, replacing the line: thankyou页面上,您要定位下一个已付款订单,因此您需要在条件函数has_bought_before()稍作更改,以替换行:

return count( $results ) > 0 ? true : false;

by: 通过:

return count( $results ) > 1 ? true : false;

It should be working as you expect now. 它现在应该可以按您期望的那样工作。

May be rename this conditional function thankyou_has_bought_before() . 可以重命名此条件函数thankyou_has_bought_before()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM