如何使用wc_customer_bought_product函数检查客户是否购买了数组中的产品 [英] How to use wc_customer_bought_product function to check if customer bought product in an array

查看:145
本文介绍了如何使用wc_customer_bought_product函数检查客户是否购买了数组中的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是一名初学者,也是新手,所以请原谅我的无知。我今天在stackoverflow上问了我的第一个问题,有人提供了一个很好的解决方案,所以我再次尝试一下。



我试图使用函数来检查客户是否在登录时在他的帐户页面中购买了一系列产品ID中的产品。如果他从阵列A购买产品或从阵列B产品C获得产品,则需要显示不同的菜单。我希望为每个产品id数组创建一个不同的函数,并将其与不同的shortcode关联。



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

  / ** 
*检查用户(通过电子邮件)是否购买了物品
*
* @access public
* @param string $ customer_email
* @param int $ user_id
* @param int $ product_id
* @return bool
* /
函数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;

$ b $ return $ wpdb-> get_var(
$ wpdb-> prepare(
FROM {$ wpdb->前缀} 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

);

$ / code>

所以我试着用它来实现我想要的只是将最后一个参数:

  //创建函数来检查客户是否从数组中购买了产品A 
函数check_is_category_A_customer()
{
global $ woocommerce;
$ user_id = get_current_user_id();
$ customer_email = $ current_user->电子邮件;

if(wc_customer_bought_product($ customer_email,$ user_id,$ product_id = array('2258','2253',
'2242')))

return真正;

返回false;

//创建简码以显示客户菜单cat
add_shortcode('CATA','check_cat_bought_A');
函数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->电子邮件;

if(''!= wc_customer_bought_product($ customer_email,$ user_id,$ product_id = array('2258',
'2253','2242'),true))

返回true;

返回false;
}

但是这也行不通。简码不再有效,菜单出现在所有情况下。

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

解决方案

大卫帮助我走上了正确的轨道,我应该已经知道如何传递1个产品ID时间在相同的功能,但我没有。

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

因此,即使使用数组或类别id会更实用(但我不知道如何去做),我正在分享这个解决方案对于其他可能想实现相同目标的人:

  //创建函数以检查客户是否从数组A $购买了产品b $ b函数check_is_category_A_customer()
{
global $ woocommerce;
$ user_id = get_current_user_id();
$ current_user = wp_get_current_user();
$ customer_email = $ current_user->电子邮件;

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;
}
返回false;

//创建简码以显示客户菜单cat
add_shortcode('CATA','check_cat_bought_A');
函数check_cat_bought_A($ atts,$ content =){
if(check_is_category_A_customer()){
return do_shortcode($ content);


$ / code $ / pre

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


First of all, I am a beginner and new to php so please forgive my ignorance. I asked my first question on stackoverflow today and somebody was kind enough to provide a great solution, so I am giving this another try.

I am trying to use a function to check if a customer bought a product inside an array of product ids as in his account pages when logged in I need to display a different menu if he bought product from array A or product from array B, array C, you get it. I would like to create a different function for each array of product ids and associate it with a different shortcode.

I have found the wc_customer_bought_product_function in the woocommerce functions reference, which is written like this :

/**
* 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
     )
   );
}

So I tried to use it to achieve what I wanted by transforming just the last parameter:

// 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);
}
}

But it did not work. When I use the shortcode the menu no longer appears, but it does not show once I bought a product which id is in the array.

I tried this version of the function based on another example:

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;
}

But that did not work either. The shortcode no longer has effect, the menu appears in all situations.

I wrote this using different functions I took as models and information I stumbled upon, and I probably made terrible mistakes, because it is not working. If someone has any idea of what I did wrong or how to achieve this goal differently, that would be a huge help! Thank you.

解决方案

So David helped me get on the right track and I should have known how to pass 1 product id at a time inside the same function but I didn't.

After some research and study, I wrote a new function using else if statements for each product id, tested it, and so far it is working.

So, even if using an array or a category id would have been more practical (but I don't know how to do it, yet) I am sharing this solution for other people who might wanna achieve the same goal :

// 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);
}
}

Thanks again to David for pointing the direction :) Of course if someone has a better solution, or sees any mistakes in this, please speak up.

这篇关于如何使用wc_customer_bought_product函数检查客户是否购买了数组中的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆