อัปเดต 2
อ้างอิงจาก "ตรวจสอบว่าลูกค้าได้ซื้อผลิตภัณฑ์เฉพาะในรหัสคำตอบของ WooCommerce" หรือไม่ สิ่งต่อไปนี้จะป้องกันการซื้อจากผลิตภัณฑ์ "แพ็คเกจทดลอง" หากมีการซื้อไปแล้วครั้งหนึ่ง หรือหากมีผลิตภัณฑ์ใด ๆ สำหรับ มีการซื้อหมวดหมู่ผลิตภัณฑ์ที่กำหนด "A" แล้ว
รหัสจริงของคุณล้าสมัยตั้งแต่ Woocommerce 3 และมีข้อผิดพลาดบางประการ...
ฉันใช้ฟังก์ชันตามเงื่อนไขที่กำหนดเองซึ่งได้รับแรงบันดาลใจจาก wc_customer_bought_product()
ซอร์สโค้ด< /a> เพื่อตรวจสอบว่าลูกค้าสามารถซื้อผลิตภัณฑ์ "ทดลองใช้ฟรี" ได้หรือไม่ ฟังก์ชันนี้จะตรวจสอบรหัสผลิตภัณฑ์หรือหมวดหมู่ผลิตภัณฑ์ด้วย:
function has_bought_items( $user_id = 0, $product_ids = 0, $categories_slugs = '' ) {
global $wpdb;
$customer_id = $user_id == 0 || $user_id == '' ? get_current_user_id() : $user_id;
$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
if ( is_array( $product_ids ) ) {
$product_ids = implode(',', $product_ids);
}
if ( $product_ids != ( 0 || '' ) ) {
$query_in = "IN ($product_ids)";
}
else {
$categories = is_array( $categories_slugs ) ? implode("','", $categories_slugs) : $categories_slugs;
$query_in = "IN (
SELECT DISTINCT products.ID FROM {$wpdb->prefix}posts AS products
INNER JOIN {$wpdb->prefix}term_relationships as term_rel
ON products.ID = term_rel.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy as term_tax
ON term_rel.term_taxonomy_id = term_tax.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms as terms
ON term_tax.term_taxonomy_id = terms.term_id
WHERE products.post_status = 'publish'
AND term_tax.taxonomy = 'product_cat'
AND terms.slug IN ('$categories')
)";
}
// Count the number of products
$product_count_query = $wpdb->get_var( "
SELECT DISTINCT COUNT(orders.ID)
FROM {$wpdb->prefix}posts AS orders
INNER JOIN {$wpdb->prefix}postmeta AS order_meta
ON orders.ID = order_meta.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS order_items
ON orders.ID = order_items.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta
ON order_items.order_item_id = order_itemmeta.order_item_id
WHERE orders.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
AND order_meta.meta_key = '_customer_user'
AND order_meta.meta_value = $customer_id
AND order_itemmeta.meta_key IN ( '_product_id', '_variation_id' )
AND order_itemmeta.meta_value $query_in
" );
// Return a boolean value if count is higher than 0
return $product_count_query > 0 ? true : false;
}
รหัสที่คุณเยี่ยมชมอีกครั้งโดยใช้ฟังก์ชันตามเงื่อนไขที่กำหนดเองด้านบน:
// add_filter( 'woocommerce_variation_is_purchasable', 'trial_package_purchasable_once', 10, 2 );
add_filter( 'woocommerce_is_purchasable', 'trial_package_purchasable_once', 10, 2 );
function trial_package_purchasable_once( $purchasable, $product ) {
// HERE set the free trial product Id to be purchased only once (can be an array of Ids too)
$free_trial_id = 50;
// HERE set the specific product category SLUG (could be an array of slugs too)
$categories = array('lettering'); // Only slug terms
if ( $product->get_id() == $free_trial_id ) {
// Check if any item of the specific product category has already been purchased once
if ( has_bought_items( get_current_user_id(), '', $categories ) ) {
return false;
}
// Check if the free trial product has already been purchased once
elseif ( has_bought_items( get_current_user_id(), $free_trial_id ) ) {
return false;
}
}
}
return $purchasable;
}
รหัสอยู่ในไฟล์ function.php ของธีมลูกที่ใช้งานอยู่ (หรือธีมที่ใช้งานอยู่) ทดสอบแล้วใช้งานได้
ในการจัดการ ID คำหมวดหมู่ผลิตภัณฑ์แทน Term Slugs ให้ใช้ฟังก์ชันตามเงื่อนไขต่อไปนี้แทน:
// For Product category(ies) Id(s)
function has_bought_items( $user_id = 0, $product_ids = 0, $categories_ids = '' ) {
global $wpdb;
$customer_id = $user_id == 0 || $user_id == '' ? get_current_user_id() : $user_id;
$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
if ( is_array( $product_ids ) ) {
$product_ids = implode(',', $product_ids);
}
if ( $product_ids != ( 0 || '' ) ) {
$query_in = "IN ($product_ids)";
}
else {
$categories = is_array( $categories_ids ) ? implode(',', $categories_ids) : $categories_ids;
$query_in = "IN (
SELECT DISTINCT products.ID FROM {$wpdb->prefix}posts AS products
INNER JOIN {$wpdb->prefix}term_relationships as term_rel
ON products.ID = term_rel.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy as term_tax
ON term_rel.term_taxonomy_id = term_tax.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms as terms
ON term_tax.term_taxonomy_id = terms.term_id
WHERE products.post_status = 'publish'
AND term_tax.taxonomy = 'product_cat'
AND terms.term_id IN ($categories)
)";
}
// Count the number of products
$product_count_query = $wpdb->get_var( "
SELECT DISTINCT COUNT(orders.ID)
FROM {$wpdb->prefix}posts AS orders
INNER JOIN {$wpdb->prefix}postmeta AS order_meta
ON orders.ID = order_meta.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS order_items
ON orders.ID = order_items.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta
ON order_items.order_item_id = order_itemmeta.order_item_id
WHERE orders.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
AND order_meta.meta_key = '_customer_user'
AND order_meta.meta_value = $customer_id
AND order_itemmeta.meta_key IN ( '_product_id', '_variation_id' )
AND order_itemmeta.meta_value $query_in
" );
// Return a boolean value if count is higher than 0
return $product_count_query > 0 ? true : false;
}
เป็นทางเลือกที่ดีกว่าสำหรับภาษาต่างประเทศที่มีอักขระพิเศษ
person
LoicTheAztec
schedule
26.03.2019