如何使用wc_customer_bought_product函数来检查客户是否在数组中购买了产品


How to use wc_customer_bought_product function to check if customer bought product in an array

首先,我是 php 的初学者和新手,所以请原谅我的无知。我今天问了我关于stackoverflow的第一个问题,有人很好心地提供了一个很好的解决方案,所以我再试一次。

我正在尝试使用一个功能来检查客户是否在产品 ID 数组中购买了产品,就像登录时在他的帐户页面中一样,如果他从阵列 A 购买产品或从阵列 B 购买产品,我需要显示不同的菜单,阵列 C,你明白了。我想为每个产品 ID 数组创建一个不同的函数,并将其与不同的简码相关联。

我在woocommerce函数参考中找到了wc_customer_bought_product_function,它是这样写的:

/**
* Checks if a user (by email) has bought an item
*
* @access public
* @param string $customer_email
* @param int $user_id
* @param int $product_id
* @return bool
*/
function wc_customer_bought_product( $customer_email, $user_id, $product_id ) {
    global $wpdb;
 
    $emails = array();
    if ( $user_id ) {
        $user     = get_user_by( 'id', $user_id );
        $emails[] = $user->user_email;
    }
    if ( is_email( $customer_email ) ) {
        $emails[] = $customer_email;
    }
    if ( sizeof( $emails ) == 0 ) {
        return false;
    }
    return $wpdb->get_var(
        $wpdb->prepare( "
            FROM {$wpdb->prefix}woocommerce_order_items as order_items
            LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS itemmeta ON order_items.order_item_id = itemmeta.order_item_id
            LEFT JOIN {$wpdb->postmeta} AS postmeta ON order_items.order_id = postmeta.post_id
            LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
            WHERE
            posts.post_status IN ( 'wc-completed', 'wc-processing' ) AND
            itemmeta.meta_value  = %s AND
            itemmeta.meta_key    IN ( '_variation_id', '_product_id' ) AND
            postmeta.meta_key    IN ( '_billing_email', '_customer_user' ) AND
            (
                postmeta.meta_value  IN ( '" . implode( "','", array_unique( $emails ) ) . "' ) OR
                (
                    postmeta.meta_value = %s AND
                    postmeta.meta_value > 0
                )
            )
         ", $product_id, $user_id
      )
   );
}

所以我试图使用它来实现我想要的,只转换最后一个参数:

// Create function to check if client bought a product from array A
function check_is_category_A_customer()
{
    global $woocommerce;
    $user_id = get_current_user_id();
    $customer_email = $current_user->email;
    if (wc_customer_bought_product( $customer_email, $user_id, $product_id=array ('2258', '2253',     
'2242')))
        return true;
    return false;
}
// Create shortcode to display menu for customers cat A
add_shortcode('CATA','check_cat_bought_A');
function check_cat_bought_A($atts,$content=""){
    if( check_is_category_A_customer() ){
        return do_shortcode($content);
    }
}

但它没有奏效。当我使用简码时,菜单不再出现,但是一旦我购买了数组中id的产品,它就不会显示。

我根据另一个示例尝试了此版本的函数:

function check_is_category_A_customer()
{
    global $woocommerce;
    $user_id = get_current_user_id();
    $customer_email = $current_user->email;
    if ( '' !=wc_customer_bought_product( $customer_email, $user_id, $product_id=array ('2258',    
'2253', '2242'), true))
        return true;
    return false;
}

但这也没有用。短代码不再有效,菜单在所有情况下都会出现。

使用我偶然发现的模型和信息的不同功能编写了这篇文章,我可能犯了可怕的错误,因为它不起作用。如果有人知道我做错了什么或如何以不同的方式实现这一目标,那将是一个巨大的帮助!谢谢。

function check_is_category_A_customer(array $product_ids)
{
    $product_ids= array ('2258','2253','2242');//for testing
    global $woocommerce;
    $user_id = get_current_user_id();
    $current_user= wp_get_current_user();
    $customer_email = $current_user->email;

    foreach($product_ids as $item):
        if ( wc_customer_bought_product( $customer_email, $user_id, $item) )
           return true;
    endforeach; 
    return false;
}

您没有设置 $current_user 对象,请参阅上面的更正。

我已经使用了这个并且它起作用了:

function has_bought_items($product_ids)
{
    global $woocommerce;
    $user_id = get_current_user_id();
    $current_user = wp_get_current_user();
    $customer_email = $current_user->email;
    $ex = false;
    
    foreach($product_ids as $item):
        if ( wc_customer_bought_product( $customer_email, $user_id, $item) )
            $ex = true;
    endforeach; 
    return $ex;
}

所以大卫帮助我走上了正确的轨道,我应该知道如何在同一函数中一次传递 1 个产品 ID,但我没有。

经过一些研究和研究,我为每个产品 id 使用 else if 语句编写了一个新函数,对其进行了测试,到目前为止它正在工作。

因此,即使使用数组或类别 ID 会更实用(但我还不知道该怎么做(,我也为其他可能想要实现相同目标的人分享此解决方案:

// Create function to check if client bought a product from array A
function check_is_category_A_customer()
{
    global $woocommerce;
    $user_id = get_current_user_id();
    $current_user= wp_get_current_user();
    $customer_email = $current_user->email;
    if ( wc_customer_bought_product( $customer_email, $user_id,'2258')) {
        return true;
    } else if ( wc_customer_bought_product( $customer_email, $user_id,'2253')) {
        return true;
    } else if ( wc_customer_bought_product( $customer_email, $user_id,'2242')) {
        return true;
    }
    return false;
}
// Create shortcode to display menu for customers cat A
add_shortcode('CATA','check_cat_bought_A');
function check_cat_bought_A($atts,$content=""){
    if( check_is_category_A_customer() ){
        return do_shortcode($content);
    }
}

再次感谢大卫指出方向:)当然,如果有人有更好的解决方案,或者看到其中的任何错误,请说出来。