【问题标题】:How to use wc_customer_bought_product function to check if customer bought product in an array如何使用 wc_customer_bought_product 函数检查客户是否购买了数组中的产品
【发布时间】:2015-02-03 02:07:11
【问题描述】:

首先,我是一个初学者和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;
}

但这也不起作用。简码不再有效,菜单出现在所有情况下。

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

【问题讨论】:

  • 顺便说一句:你应该接受前面问题的答案(点击勾号)
  • 完成 - 我希望我能在这个问题上做同样的事情;)

标签: php wordpress function woocommerce shortcode


【解决方案1】:
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 对象,请参阅上文进行更正。

【讨论】:

  • 谢谢,我刚试了下,可惜不行。无论客户是否购买了产品,无论购买的商品的产品 ID 是什么,菜单都会保持显示。我一定错过了其他东西:(
  • 更新了上述内容,但如果它不起作用,请尝试一次传递 1 个产品 ID
  • 感谢您对此的跟进 :) 我确实尝试了最后一个更正的版本,它使菜单消失了,但是一旦购买了阵列中的一个产品就不会出现。我试着只留下一个产品ID,它奏效了。但不幸的是,为每个产品 ID 创建一个函数和一个简码是行不通的,我需要能够使用数组或类别 ID,但这不是函数的初始参数之一。
  • 或者作为最后的手段,也许我可以使用这个模型为每个产品 ID 创建一个函数,但将一个短代码与多个函数相关联,但我不知道这是否可能。
  • 更新了上面的简单答案,它的效率并不高,因为它执行 3 次数据库查询,但您无法使用原始功能,该功能仅检查他们是否购买了所有产品而不是任何产品。你最终可以重做 woo 函数,但这是一个 sql 问题:)
【解决方案2】:

所以 David 帮助我走上了正轨,我应该知道如何在同一个函数中一次传递 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);
}
}

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

【讨论】:

    【解决方案3】:

    我使用它并且它的工作原理

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 2021-05-29
      • 2020-07-30
      • 2018-03-26
      • 2019-02-27
      • 2013-09-05
      • 2022-10-17
      相关资源
      最近更新 更多