alexandresaiz
alexandresaiz

Reputation: 2766

Dynamically add a product to Shopify cart

I need to add a product which is an extra cost for customer when s/he adds to cart products from different brands. This is due to the fact that we ship products from different warehouses based on brands. So if user adds to cart:

no extra product/charge added

if user adds to cart 2 items from different brands, we dynamically add a product called different warehouse extra charge:

Question is then: How can I dynamically add products to cart if certain conditions are met?

Upvotes: 2

Views: 1801

Answers (1)

Gavin Terrill
Gavin Terrill

Reputation: 1039

Use the Ajax API to do this.

In cart.liquid you can interrogate the cart items to see what products you have, determining what you need to add:

<script>
    jQuery.ajaxSetup({cache: false});
    Shopify.getCart(function(cart) {
        // add logic to see what items are in the cart and add a delivery one
        // if needed
        console.log(cart);
    });
</script>

Upvotes: 1

Related Questions