Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda