2014-07-22 3 views
0

Удивительно, может ли кто-нибудь помочь.Woocommerce: ограничивать клиентов только одной категорией за транзакцию

Я хочу либо найти надстройку, либо создать свою собственную работу, которая позволит разрешить покупателю приобретать только одну категорию за транзакцию.

E.g они могут купить из категории X, но после того, как они выбрали товары из той категории, которую они больше не могут приобрести в категории Y. Сначала они должны выполнить транзакцию. Для этого я, очевидно, нуждаюсь в сообщении об ошибке.

Есть ли какие-либо дополнения, способные на это? Как бы вы это сделали?

Это мой текущий план мысль, но мне нужен совет по этому вопросу, является ли лучшим решением:

1. Loading product page 

2. Check cart contents to see if products exist already 

    a. product already exists in cart = check it's category 

     i. same category? display "add to cart button" 
     ii. not same category? display error message instead of button 

    b. no product in cart? display "add to cart button" 
+0

Почему вы не видите, есть аддон http://codecanyon.net/item/woocommerce-sell-individual/7873865, который является премиальным, я считаю, что это поможет вам –

+0

@Fresher, который является только одним продуктом. Я хочу несколько продуктов из одной категории. – Francesca

ответ

2
function is_product_the_same_cat($valid, $product_id, $quantity) { 
    global $woocommerce; 
    // start of the loop that fetches the cart items 
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { 
     $_product = $values['data']; 
     $terms = get_the_terms($_product->id, 'product_cat'); 
     $target_terms = get_the_terms($product_id, 'product_cat'); //get the current items 
     foreach ($terms as $term) { 
      $cat_ids[] = $term->term_id; //get all the item categories in the cart 
     } 
     foreach ($target_terms as $term) { 
      $target_cat_ids[] = $term->term_id; //get all the categories of the product 
     }   
    } 
    $same_cat = array_intersect($cat_ids, $target_cat_ids); //check if they have the same category 
    if(count($same_cat) > 0) return $valid; 
    else { 
     wc_add_notice('This product is in another category!', 'error'); 
     return false; 
    } 
} 
add_filter('woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3); 
Смежные вопросы