Skip to content

Instantly share code, notes, and snippets.

@ucheng
Last active October 6, 2017 04:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ucheng/377732e3cea215afdca803d76ae925d8 to your computer and use it in GitHub Desktop.
限制購買同分類下的商品
<?php
//不可重複購買的分類(可多個)
global $non_repeatable_cats;
$non_repeatable_cats = array( 21, 22 );
function foo_wc_purchase_disabled_message() {
global $product,$non_repeatable_cats;
$repurchasable = true;
//是否購買過同一個商品
if ( foo_customer_purchased_same_product_in_cat( $product ) ) {
$repurchasable = false;
}
//如果此商品包含不可重複購買的分類,檢查是否購買過此分類下的其他商品
foreach( $non_repeatable_cats as $non_repeatable_cat ) {
if ( has_term( $non_repeatable_cat, 'product_cat', $product->get_id() ) ) {
if ( foo_customer_purchased_items_in_same_cat( $non_repeatable_cat ) ) {
$repurchasable = false;
break;
}
}
}
if ( $product->is_purchasable() && !$repurchasable ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30);
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30);
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30);
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10);
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20);
echo '<div class="woocommerce"><div class="wc-nonpurchasable-message woocommerce-error">您無法重複購買此分類下的商品</div></div>';
}
}
add_action( 'woocommerce_single_product_summary', 'foo_wc_purchase_disabled_message', 20 );
//檢查是否購買重複的商品
function foo_customer_purchased_same_product_in_cat( $product ) {
global $non_repeatable_cats;
//判斷此商品是否包含不可重複購買的分類
foreach( $non_repeatable_cats as $non_repeatable_cat ) {
if ( ! has_term( $non_repeatable_cat, 'product_cat', $product->get_id() ) ) {
return false;
}
}
//檢查使用者是否曾經購買過此商品
return wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $product->get_id() );
}
function foo_customer_purchased_items_in_same_cat( $non_repeatable_cat ) {
$bought = false;
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order',
'post_status' => array('wc-completed', 'wc-processing', 'wc-on-hold', 'wc-pending')
) );
foreach ( $customer_orders as $customer_order ) {
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
$order = wc_get_order( $customer_order );
foreach ($order->get_items() as $item) {
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$product_id = $item['product_id'];
$terms = get_the_terms( $product_id, 'product_cat' );
} else {
$product_id = $item->get_product_id();
$terms = get_the_terms( $product_id, 'product_cat' );
}
// Your condition related to your 2 specific products Ids
foreach ( $terms as $term ) {
if ( $term->term_id == $non_repeatable_cat ) {
$bought = true;
break;
}
}
}//end foreach
}
return $bought;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment