检查客户是否已经在WooCommerce中购买了东西

问题描述 投票:4回答:5

我想创建一个WooCommerce插件,为客户添加一些优惠(有购买历史记录)。

我怎样才能检查用户之前买过的东西?

谢谢。

php wordpress woocommerce product orders
5个回答
9
投票

更新:New updated improved, light and faster version HERE


是的,当客户已经至少有一个状态已完成的订单时,可以创建一个返回true的条件函数。

以下是此条件函数的代码:

function has_bought() {
    // Get all customer orders
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => 'shop_order', // WC orders post type
        'post_status' => 'wc-completed' // Only orders with status "completed"
    ) );

    // return "true" when customer has already at least one order (false if not)
   return count($customer_orders) > 0 ? true : false; 
}

此代码经过测试和运行。

此代码位于活动子主题或主题的function.php文件中,或者插入到php文件中。


用法(作为条件):

  • 您可以在以前复制到活动子主题或主题的某些WooCommerce templates中使用它。
  • 在你的主题php文件中。
  • 在插件php文件中。
  • 任何PHP功能或WordPress / WooCommerce。

参考


5
投票

这是一个更轻,更快的条件函数,如果客户已经购买,则返回true。

有一个可选参数$user_id,它允许您指定已定义的用户ID:

function has_bought( $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;
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

此代码在Woocommerce 3+上进行测试并且可以工作(它也应该适用于以前的版本)。

对于多种产品:Check if a customer has purchased a specific products in WooCommerce


用法示例1(登录客户)

if( has_bought() )
    echo '<p>You have already maid a purchase</p>';
else
    echo '<p>Welcome, for your first purchase you will get a discount of 10%</p>';

用法示例2(设置$ user_id)

// Define the user ID
$user_id = 85;

if( has_bought( $user_id ) )
        echo '<p>customer have already maid a purchase</p>';
    else
        echo '<p>Customer with 0 purchases</p>';

如果未定义$user_id且当前用户未登录,则此函数将返回false

此函数的代码部分基于内置的WooCommerce函数wc_customer_bought_product源代码。


0
投票

简化版:

function has_bought() {
    // Get all customer orders
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => 'shop_order', // WC orders post type
        'post_status' => 'wc-completed' // Only orders with status "completed"
    ) );
    // return "true" when customer has already one order
    return count( $customer_orders ) > 0 ? true : false;
}

0
投票

如果启用了Guest Check,则此功能可能会有所帮助

只需发送$ customer_email作为参数,函数将检查该电子邮件的所有订单并返回true或false。

function has_bought($customer_email){

  $orders = get_posts(array(
  'numberposts' => -1,
  'post_type' => 'shop_order',
  'post_status' => array('wc-processing', 'wc-completed'),
  ));

$email_array = array();

foreach($orders as $order) {

$order_obj = wc_get_order($order->ID);
$order_obj_data = $order_obj->get_data();

array_push($email_array, $order_obj_data['billing']['email']);

}


if (in_array($customer_email, $email_array)) {
return true;
} else {
return false;
}

}

0
投票

带有付费状态或仅具有状态不可知查询的增强版本,也改进了PHP 5.4+的代码输入:

function has_bought(int $user_id = 0, bool $paid = true ) {

    global $wpdb;

    $user_id = (empty($user_id)) ? get_current_user_id() : $user_id;

    $sql_str = "
        SELECT p.ID FROM ".$wpdb->posts." AS p
        INNER JOIN 
            ".$wpdb->postmeta." AS pm ON p.ID = pm.post_id
        WHERE 
            p.post_type LIKE 'shop_order'
        AND pm.meta_key = '_customer_user'
        AND pm.meta_value = %d
    ";

    $args = [(int) $user_id];

    if ($paid === true) {
        $paid_order_statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
        $sql_str .= "AND p.post_status IN ( 'wc-" . implode( "','wc-", $paid_order_statuses ) . "' )";
    }

    $sql = $wpdb->prepare($sql_str, $args);

    return (bool) $wpdb->get_var( $sql );
}
© www.soinside.com 2019 - 2024. All rights reserved.