2017-02-14 52 views
1

我想知道是否有任何方法來檢查成功購買是來自新客戶還是返回客戶。Woocommerce:檢查是否成功購買來自新客戶或返回客戶

我有一個需要添加到訂單成功頁面的腳本。

我有這個迄今爲止,它並沒有真正工作,我需要它,因爲它是隻檢查客人或登錄結帳:

$order = wc_get_order($order->id); 
$user = get_user_by('email', $order->billing_email); 

if (isset($user->ID)) { 
    echo 'User is logged in.'; 
} else { 
    echo 'User is a guest.'; 
} 

謝謝!

回答

0

無論帳單電子郵件地址發生變化,以下代碼應該可以用於返回客戶以及新客戶。這也適用於註冊時註冊的新客戶。

add_action('woocommerce_thankyou', 'is_returning_customer', 10, 1); 

function is_returning_customer($order_id) 
{ 
    if (!$order_id) { 
     return; 
    } 
    if(is_user_logged_in()) { 
     $order_status = array('wc-on-hold', 'wc-processing', 'wc-completed'); 
     $customer_id = get_current_user_id(); 
      $customer_orders=get_posts(array(
       'meta_key' => '_customer_user', 
       'meta_value' => $customer_id, 
       'post_type' => 'shop_order', 
       'post_status' => $order_status, 
       'numberposts' => -1 
      ) 
     ); 
     if(count($customer_orders)>1) { 
      //returning customer 
     } else { 
      //new customer 
     } 
    } 
} 
+0

感謝您的幫助,這兩個答案爲我工作:) –

0

您可以簡單地使用wordpress is_user_logged_in()函數掛鉤woocommerce_thankyou來檢查訂單狀態和用戶是否登錄。

add_action('woocommerce_thankyou', 'my_custom_tracking', 10, 1); 

function my_custom_tracking($order_id) { 
    if (!$order_id) { 
     return; 
    } 
    // Lets grab the order 
    $order = wc_get_order($order_id); 

    $_billing_email = get_post_meta($order_id, '_billing_email', true); 
    $user = get_user_by('email', $_billing_email); 

    //for successful order 
    if (in_array($order->status, ['processing', 'completed'])) { 
     if (is_user_logged_in() || $user) { 
      //it is a returning user 
     } else { 
      //user is a guest 
     } 
    } 
    //unsuccessful order 
    else { 

    } 
} 

請注意:如果你想只檢查用戶登錄或不能再由if (is_user_logged_in())

更換if (is_user_logged_in() || $user)相關問題:woocommerce php snippets for proceeded to checkout to know user is login or not


修訂v2

add_action('woocommerce_thankyou', 'wh_isReturningCustomer', 10, 1); 

function wh_isReturningCustomer($order_id) { 
    if (!$order_id) { 
     return; 
    } 
    // Lets grab the order 
    //$order = wc_get_order($order_id); 

    $_billing_email = get_post_meta($order_id, '_billing_email', true); 

    $args = [ 
     'post_type' => 'shop_order', 
     'post__not_in' => [$order_id], //exclude current Order ID from order count 
     'post_status' => ['wc-processing', 'wc-completed'], 
     'posts_per_page' => -1, 
     'meta_query' => [ 
      'relation' => 'AND', 
      [ 
       'key' => '_billing_email', 
       'value' => $_billing_email, 
       'compare' => '=', 
      ] 
     ] 
    ]; 
    $posts = new WP_Query($args); 
    if ($posts->post_count) { 
     //it is a returning user 
    } else { 
     //user is a guest 
    } 
} 

代碼在你的活動ch的function.php文件中ild主題(或主題)。或者也可以在任何插件php文件中使用。
代碼已經過測試並且可以正常工作。

希望這會有所幫助!

+0

感謝您的幫助! 我唯一可以發現的問題是,如果用戶在結賬時註冊併成功購買了產品,他們將永遠被視爲'正在返回'的客戶,因爲他們將會登錄並且他們的結算電子郵件將被發現。任何方式在這個? 再次感謝。 –

+0

@ stevie-c91:我已經更新了我的答案,請檢查它。使用'wh_isReturningCustomer()'函數。 –

+0

非常感謝!你一直在幫助很大! –

相關問題