Re: Help with this Ajex code updating the price makes the Add to Cart button bug

Solved

Help with this Ajex code updating the price makes the Add to Cart button bug

sunnycide1
Tourist
13 0 1
_reloadProductPrice() {
  const productSection = document.querySelector('.product-section'); // Adjust selector as needed
  if (!productSection) return;

  // Fetch updated content via AJAX
  fetch(window.location.href) // Re-fetch the current page
    .then((response) => {
      if (!response.ok) throw new Error('Failed to fetch product section');
      return response.text();
    })
    .then((html) => {
      const parser = new DOMParser();
      const doc = parser.parseFromString(html, 'text/html');

      // Find the updated price
      const updatedPriceElement = doc.querySelector('.price--large');
      if (updatedPriceElement) {
        const updatedPriceContent = updatedPriceElement.innerHTML; // Get only the price's inner HTML

        // Find the current price and update it without affecting any button or dropdown elements
        const currentPriceElement = productSection.querySelector('.price--large');
        if (currentPriceElement) {
          currentPriceElement.innerHTML = updatedPriceContent;
        }
      }
    })
    .catch((error) => console.error('Error reloading product price:', error));
}

I just want to update the Price element in the client side when i change the variants, but it some how also messing with the Add to cart button. 

Accepted Solution (1)
namphan
Shopify Partner
2155 285 321

This is an accepted solution.

Hi @sunnycide1,

I checked and you are using JS code to change price based on discountCode. So when we call fetch in product-info.js file, it will call back the previous discountCode function and the price will be changed again from the beginning.

You just need to check and customize the code at product-info.js file, function handleUpdateProductInfo, it will display well.

I hope it helps!

Coffee tips fuels my dedication.
Shopify Development Service
PageFly Page Builder Optimize your Shopify store (Free plan available)
Need help with your store? namphan992@gmail.com

View solution in original post

Replies 5 (5)

Shadab_dev
Shopify Partner
995 55 99

Why do you have to do this externally. All themes I guess have this feature to dynamically update the price or whatever content associated with it??

Buy me Coffee, if you feel i was helpful. Email Me or WhatsApp me for any help with shopify theme customizations or any project in web dev. If this is helpful, please Like and Accept the solution.
sunnycide1
Tourist
13 0 1

so i want to visually update the price when people come through a discount coupon as the discount coupon get applied to what ever they add to the cart. but it is not visually clear that you are getting a discount. 
i cant use the shopify markdown feature as it actually marks down prices and the coupon will be applied on markdown price.
so my code for changing the price on client side works prefecly fine but when i switch varients it get backto normal and i have to refresh. 
image_2024-11-27_182127024.pngimage_2024-11-27_182150556.png

the code above worked fine and updated the prices as i switch the vareints but it bugged the buy botton please help me if you can. thankyou

namphan
Shopify Partner
2155 285 321

Hi @sunnycide1,

Please send the website link, I will check it for you

Coffee tips fuels my dedication.
Shopify Development Service
PageFly Page Builder Optimize your Shopify store (Free plan available)
Need help with your store? namphan992@gmail.com
sunnycide1
Tourist
13 0 1

rmlldn.com 

but you'll have to go through 
https://rmlldn.com/discount/GRANDOPEN30
to get the markdown price

 

namphan
Shopify Partner
2155 285 321

This is an accepted solution.

Hi @sunnycide1,

I checked and you are using JS code to change price based on discountCode. So when we call fetch in product-info.js file, it will call back the previous discountCode function and the price will be changed again from the beginning.

You just need to check and customize the code at product-info.js file, function handleUpdateProductInfo, it will display well.

I hope it helps!

Coffee tips fuels my dedication.
Shopify Development Service
PageFly Page Builder Optimize your Shopify store (Free plan available)
Need help with your store? namphan992@gmail.com