Update product price dynamically on "Add to Cart"

Hello everyone,

I got a question regarding adding a product to the cart using the Ajax API.

The product this is about would be a custom bundle product whose price should change dynamically based on which options the user has selected. Once the user clicks the “Add to cart” button the bundle product should be added to the cart with the dynamically calculated price.

While it is not too difficult to use the API to add a product to the cart, it seems that it is not possible to also submit a custom price via the Ajax API. If I got it right, this would only be possible using the Admin API. But then again it always seemed to me as if the Admin API would not really be about working within the actual Theme but rather for building more complex applications outside of the theme.

What I would like to do is to implement a price-update functionality simply within a given .js or .liquid file in the Theme’s code once the product is being added to the cart. Is this even possible?

A workaround might be to use Shopify Scripts to reduce the bundles price based on the selected options. But I’d prefer a more direct solution by sending the reduced product’s price to the user’s cart immediately.

Here’s how it would work ideally for my usecase (but obviously doesn’t):

const formData = {
          'items': [{
            id: 39641211961437,
            quantity: 1,
            price: 2200 // dynamically calculated price based on bundle options 
          }]
      };
    
    fetch('/cart/add.js', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(formData)
    });

Any help or clarification would be greatly appreciated.

Kind regards

Yannic

1 Like

Hi YannicC

did you ever figure out how to do this or a workaround this? I’m looking for a similar solution so I can move my site to shopify.

thanks!

Hi Jerry,

Actually I did figure it out but the solution only works on Shopify Plus stores. If you got Shopify Plus, you got access to Shopify Scripts which are based on Ruby and are a way to manipulate line item prices and more after a product is being added to the cart.

Usually you would add a line item property during the add to cart event to the product that would then trigger the Shopify Script for price reduction.

So it is possible, but you got to get into how Scripts and Line Item Properties work. And especially as said you need to be subscribed to Shopify Plus.

Best,

Yannic

1 Like

Hi Yannic,

Your message was very helpful, can you please provide more details about the change. What is the name and location of script to update the line items?

I need to do that in my store. And planning to upgrade to Shopify plus, but I want to make sure i know what to do after that.

Thank you for your help.

Regards,

Rajendra

1 Like

This topic is old, but I still wanted to provide information. You can achieve this with the Cart Transform API along with a function. However, this feature is available only in the Plus plan.