How to change the product price in liquid file in shopify?

How to change the product price in liquid file in shopify?

abhisheq
Shopify Partner
3 0 0

I'm new to shopify and I'm trying to modify it for a specific feature. I'm trying to implement zipcode based pricing, I have access to various prices and zipcodes on the main-product.liquid already. Now whenever the customer adds the product to cart or clicks buy now, I want this zipcode based price to go in the cart and checkout, not the default price. How can I achieve this?

I tried to change the default product.price value but I read somewhere that it is read only and cannot be changed.

Replies 2 (2)

heddykhalifa
Shopify Partner
150 12 34

Hi Abhisheq, 

Heddy from Gameball here. 

 

It sounds like you're on the right track with having the zipcode-based prices accessible in your main-product.liquid file. You're right that `product.price` is a read-only value, so you can't directly modify it. However, you can implement a workaround to achieve what you're trying to do.

Here's an approach you could try:

1. Create a custom line item property: When a customer adds a product to their cart, you can pass a custom price based on the customer's zipcode as a line item property. This won’t change the product’s default price, but you can display it as part of the checkout process.

2. Use JavaScript to select the correct price: You can write a JavaScript function to dynamically set the price based on the customer's zipcode input. When they click "add to cart" or "buy now," this function could calculate and pass the price to the cart.

3. Adjust pricing in the checkout process: Unfortunately, Shopify's checkout process is more restricted, so you can't directly change the price at this stage. But if the customer sees the right price in the cart and throughout the shopping process, it might still be a solution.

If you're comfortable with more custom work, you could consider using Shopify's Shopify Scripts (available with Shopify Plus) to create more dynamic pricing logic during the checkout process.

If this is a little over your head, a Shopify developer could help make sure everything runs smoothly!

abhisheq
Shopify Partner
3 0 0

On main-product.liquid, I guess this is the code that renders buy buttons:

 {%- when 'buy_buttons' -%}
                {%- render 'buy-buttons',
                  block: block,
                  product: product,
                  product_form_id: product_form_id,
                  section_id: section.id,
                  show_pickup_availability: true
                -%}

Here, the product object is passed into the component. I can modify the product object, but if I'm not wrong, there's no way to take this modified price to the checkout. Shopify will still get price from the backend and not from the cart. So I guess this is not possible without shopify plus. Please, correct me if I'm wrong somewhere.