wc_customer_bought_product بررسی اینکه کاربر مورد نظر محصول را خریده

wc_customer_bought_product بررسی اینکه کاربر مورد نظر محصول را خریده

تابع ووکامرسی wc_customer_bought_product بررسی اینکه کاربر مورد نظر محصول را خریده بر اساس آیدی یا ایمیل کاربر

Syntax – سینتکس

wc_customer_bought_product( (string) $customer_email, (int) $user_id, (int) $product_id ); 

Parameters – پارامتر ها (3)

  • 1- $customer_email (string)
  • 2- $user_id (int)
  • 3- $product_id (int)

Usage – نحوه استفاده

if ( !function_exists( 'wc_customer_bought_product' ) ) { 
    require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-user-functions.php'; 
} 
  
// The customer email. 
$customer_email = ''; 
  
// The user id. 
$user_id = -1; 
  
// The product id. 
$product_id = -1; 
  
// NOTICE! Understand what this does before running. 
$result = wc_customer_bought_product($customer_email, $user_id, $product_id); 
    

Defined – محل تعریف

/includes/wc-user-functions.php

function wc_customer_bought_product( $customer_email, $user_id, $product_id ) { 
    global $wpdb; 
 
    $transient_name = 'wc_cbp_' . md5( $customer_email . $user_id . WC_Cache_Helper::get_transient_version( 'orders' ) ); 
 
    if ( false === ( $result = get_transient( $transient_name ) ) ) { 
        $customer_data = array( $user_id ); 
 
        if ( $user_id ) { 
            $user = get_user_by( 'id', $user_id ); 
 
            if ( isset( $user->user_email ) ) { 
                $customer_data[] = $user->user_email; 
            } 
        } 
 
        if ( is_email( $customer_email ) ) { 
            $customer_data[] = $customer_email; 
        } 
 
        $customer_data = array_map( 'esc_sql', array_filter( array_unique( $customer_data ) ) ); 
        $statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() ); 
 
        if ( sizeof( $customer_data ) == 0 ) { 
            return false; 
        } 
 
        $result = $wpdb->get_col( " 
            SELECT im.meta_value FROM {$wpdb->posts} AS p 
            INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id 
            INNER JOIN {$wpdb->prefix}woocommerce_order_items AS i ON p.ID = i.order_id 
            INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS im ON i.order_item_id = im.order_item_id 
            WHERE p.post_status IN ( 'wc-" . implode( "', 'wc-", $statuses ) . "' ) 
            AND pm.meta_key IN ( '_billing_email', '_customer_user' ) 
            AND im.meta_key IN ( '_product_id', '_variation_id' ) 
            AND im.meta_value != 0 
            AND pm.meta_value IN ( '" . implode( "', '", $customer_data ) . "' ) 
        " ); 
        $result = array_map( 'absint', $result ); 
 
        set_transient( $transient_name, $result, DAY_IN_SECONDS * 30 ); 
    } 
    return in_array( absint( $product_id ), $result ); 
} 

versions – نسخه ها

از نسخه : 3.0.2

نسخه فعلی : 3.0.6

دیگر نسخه ها : 3.0.6 , 3.0.5 , 3.0.4 , 3.0.3 , 3.0.2

ارسال نظر