woocommerce/woocommerce-subscriptions-restrict-product

Account for multiple quantities being purchased at once

Closed this issue · 6 comments

Currently, the plugin doesn't account for multiple quantities of a subscription being added to the cart. When hooking into woocommerce_subscription_is_purchasable I need to also check if there are multiple quantities in the cart, and add those to my check.

Here's the spot where it probably should happen: https://github.com/Prospress/woocommerce-subscriptions-restrict-product/blob/master/wcs-restrict-product.php#L209

You could request that stores who set up a product to be a restricted product to also set the product to be sold individually. Then you could piggyback on WC handling all that logic.

By using the existing "sold individually" feature it could also open the opportunity to not set a product to be sold individually and create some bizarre use case.

If you want to force it to limit purchases to 1 quantity in the cart, you could override the sold individually setting by hooking onto the woocommerce_is_sold_individually hook.

That had occurred to me. It would be a bit of a "cheat" , but would save a lot of development time.

Wait, I may have missed an important detail here. Should it be possible to purchase multiple products at once if the restriction number allows it?

For example, if a product is restricted to 4, could a customer buy all 4 at once? When I made the recommendation around using the sold individually setting, I was under the impression that you would only allow 1 to be purchased and so making the product sold individually made a lot more sense.

If customers should be allowed to purchase multiples of the product at once, this becomes quite a bit more complex.

What happens if you select 5 from the product quantity selection but only 4 are allowed? To handle that you can use the woocommerce_quantity_input_max filter to make sure the max option matches the maximum possible (the total number allowed - the total quantity of active subscriptions to this product - number of products in the cart).

And like you said, to achieve that you'll also need to find the number of items in the cart already.

Something like this should get you the combined quantity of a certain product in the cart:

function get_product_quantity_in_cart( $product_id ) {

	$quantity = 0;

	if ( ! empty( WC()->cart->cart_contents ) ) {
		foreach ( WC()->cart->cart_contents as $cart_item ) {
			if ( $cart_item['product_id'] == $product_id ) {
				$quantity += $cart_item['quantity'];
			}
		}
	}

	return $quantity;
}

Thanks for circling back around on this, I appreciate the boost - Bernat and I were just looking through the cart validation hooks, but no good approach had stuck yet.

Should it be possible to purchase multiple products at once if the restriction number allows it?

I couldn't think of a good reason why this shouldn't be allowed. If I'm allowing a restriction number > 1, then I think a user would expect the plugin to validate up to that number at any point along the way without putting an arbitrary limit on one product per purchase. They could theoretically just go back and purchase it a second time, so it doesn't make sense to not allow multiple quantities to be purchased in one cart. That's my reasoning anyway :)

I wasn't aware of the woocommerce_quantity_input_max filter, I suspect that this approach could be what I'm looking for. Thanks again for the push in the right direction, I'm back off to the races.

This is currently accounted for and testing correctly. Closing issue