Limit a product purchase with another product

LeSoleil
Visitor
1 0 0

I sell small leather goods and accessories, which my customers buy as a gift a lot of the times. Therefore, I started offering gift boxes as an additional service/product in case the customer wants a special box to put the accessory inside.

 

So, lets say I have a keychain as a product (USD 10) , and a gift box as another product (USD 5). What ends up happening is some people will like the gift box too much that they place an order for a gift box ONLY, but do not buy any of my products.  

 

I want to restrict the purchase of the gift box in a way that a customer is only allowed to buy one gift box per product. For example, if the customer wanted to buy 1 keychain, they are allowed to add one gift box to their order, and if they want 3 keychains, they can add 3 gift boxes to the order

Replies 3 (3)

MarinaPetrovic
Shopify Partner
559 124 182

Hi @LeSoleil , for this kind of restriction you would need to include an app, or contact a developer to create a solution for you. Here is an example on how this should look like, of course this is not an exact solution for your case, since I don't have any details. But something like:

function restrictGiftBoxQuantity() {
    fetch('/cart.js')
        .then(response => response.json())
        .then(cart => {
            const giftBoxLimit = cart.attributes.gift_box_limit || 0;
            let giftBoxCount = 0;

            cart.items.forEach(item => {
                if (item.product_type === 'Gift Box') {
                    giftBoxCount += item.quantity;
                }
            });

            if (giftBoxCount > giftBoxLimit) {
                alert(`You can only buy ${giftBoxLimit} gift box(es) based on the other items in your cart.`);
                
                cart.items.forEach(item => {
                    if (item.product_type === 'Gift Box' && item.quantity > giftBoxLimit) {
                        const updatedQuantity = giftBoxLimit;

                        fetch('/cart/change.js', {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json',
                            },
                            body: JSON.stringify({
                                id: item.id,
                                quantity: updatedQuantity
                            })
                        }).then(() => location.reload());
                    }
                });
            }
        });
}

document.addEventListener('cart:updated', restrictGiftBoxQuantity);

 

So you need to restrict users from buying gift box only, and to restrict the quantity of bought gift boxes, based on the number of other products in the cart.

 

Best

M.Petrovic | Shopify Developer
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution

BSSCommerce-HDL
Shopify Expert
733 242 254

Hi, This is BSS Commerce

 

I think you can try B2B Wholesale Solution by BSS. We have all the features to help merchants manage and customize pricing, orders, and registration forms for B2B/B2C Customer Segment.

About the feature "Order Limit," you can easily modify the minimum order quantity so that the buyers are allowed to buy at lease a certain number of products per order. For example, in this case, this will be 1 gift box per product.

BSSTechVenture_0-1716401720481.png

But if you don't want to use all of our features in-app, you can thoroughly describe for me what you expect, then we can check and give you the best suggestion

If our suggestions are useful, please let us know by giving it a like, marking it as a solution, or donating here .


B2B Solution & Custom Pricing | Product Labels by BSS


Need help from our expert? Kindly share your request with us via community@bsscommerce.com


BSS Commerce - Full-service eCommerce Agency

LuffyOnePiece
Shopify Partner
607 87 104

Hi @LeSoleil ,

We can set a limit for the gift box purchase. And when they try to buy more than a limit, we can simply display the message or we can disable the ATC.

 

Let me know if you need help with the implementation.

 

Thank you

Sandeep Pangeni
Need help with your store? sandeeppangeni17@gmail.com
For quick response, Contact In WhatsApp +9779867521184