Have your say in Community Polls: What was/is your greatest motivation to start your own business?

How can I render a section template with AJAX on a product page?

How can I render a section template with AJAX on a product page?

mikewa33
Visitor
2 0 6

I am looking for a button click to render a section template or reload the current product via AJAX. Currently, I have all the products set up and can click a button to trigger an API call to get the section. But even though I am passing the product ID the section comes back without product data. Is there a way to have an AJAX call on a product page that will allow me to replace the current product with another one?

request = new XMLHttpRequest()
request.open('GET', '/?section_id=product-name-template&product_id='+event.target.dataset.productId, true)
request.send()

 My current request including the product_id 

Reply 1 (1)

Leo_Huynh
Tourist
7 1 6

Hey

Your API call is a request for a homepage's section! So, obviously, it has no product/collection data

With section rendering, if you want to add product/collection context simply call 

fetch(`/products/your-product-handle?section_id=your-section-id`)

 

Same for collection.

Leo Huynh | Shopify Software Engineer @ Insights Studio
Email: leo@insights.is
Personal site: https://leohuynh.dev