How to use the wc_customer_bought_product function to check if a customer is buying a product in an array

First of all, I am new and new to php, so please forgive my ignorance. I asked my first stackoverflow question today, and someone was kind enough to provide a great solution, so I give it another try.

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

I found wc_customer_bought_product_function in a link to the woocommerce function, which is written as follows:

/**
* 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 converting only 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 that did not work. When I use short code, the menu no longer appears, but it does not appear as soon as I bought a product whose identifier 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 didn't work either. The barcode is no longer valid, the menu appears in all situations.

, , , , , , , . - , -, ! .

0
2
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, . .

0

, , , 1 , .

, else if , .

, ( , , ), , :

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

:) , - - , , .

0

Source: https://habr.com/ru/post/1650196/


All Articles