Limit customer to purchase only 3 products per vendor
-
I am trying to limit 3 orders per vendor on my woocommerce wc vendors market place. I am new to php, managed to write the below code. But it is not working. I am i missing anything.
add_filter( ‘woocommerce_add_cart_item_data’, ‘woo_custom_add_to_cart’ ); // function to execute function woo_custom_add_to_cart( $cart_item_data ) { global $woocommerce; $items = $woocommerce->cart->get_cart(); //getting cart items $_product = array(); //getting all the products from the cart foreach($items as $item => $values) { $_product[] = $values[‘data’]->post; } $_vendor_ids_in_cart = array(); if (count ($_product[]) === 3) { //if cart has 3 products, getting all the vendor id's for($x = 0; $x <= count($_product); $x++) { $_vendor_ids_in_cart[] = get_post_field( 'post_author', $_product[$x]->ID); } //getting vendor id for the current product that is being added to cart $prodId = (int) apply_filters( 'woocommerce_add_to_cart_product_id', $_GET['add-to-cart'] ); $product_added_vendor_id = get_post_field( 'post_author', $prodId ); //getting the count of products each vendor has in the cart $count_of_each_vendor_id = array_count_values($_vendor_ids_in_cart); //count of vendor id's $cnt_how_many_times_vendor_id_exits = $count_of_each_vendor_id[product_added_vendor_id]; //check the count, if the count of the cart items are greater than 3 for a single vendor if ($cnt_how_many_times_vendor_id_exits > 3) { $woocommerce->cart->empty_cart(); wc_add_notice( __("You can add only 3 order items from 1 vendor !", "wcvendors")); } } return $cart_item_data; }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Limit customer to purchase only 3 products per vendor’ is closed to new replies.