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:
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:
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;
}
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.
, , , , , , , . - , -, ! .