Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Add product cart with Fetch API Shopify

I am trying to add a product to the cart with shopify fetch api. I have created this code, but I can not add the product to the cart. any help please?

let submitButton = document.getElementById('submit');
let variantId = submitButton.getAttribute('data-variant-id');

let newVariantIdArray = variantId.split(',');

let product_data = newVariantIdArray.map(variantId => {
    return {quantity: 1, id: variantId}
  })

let data = {
    items: product_data
  }
   

function addToCart(data) {
  const cartData = data

  fetch('/cart/add.js', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(cartData),
  })
    .then((response) => {
      if (response.ok) {
      }
    })
    .catch((error) => console.error(error))
}

submitButton.addEventListener('click', (event) => {
  event.preventDefault()

  addToCart(variantId)
})

console.log(data)
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!