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

118
Views
Agregar varios productos al carrito de WooCommerce - AJAX

Estoy tratando de encontrar una manera de seleccionar múltiples variaciones de elementos de una interfaz de Vue dentro de un sitio web de WooCommerce.

Instalé el complemento WC Ajax Cart para tener algún tipo de punto final al que pueda llamar usando AXIOS.

Cada opción selecciono anuncios y le doy a una matriz de productos selectedProducts llamada

Cuando el usuario presiona en el checkout lo que hago es:

 this.textCheckout = "Checking you out..."; //No pun intended actually xD this.products.forEach((item, i) => { if(this.selectedProducts.includes(item.variation_id)) { let adder = new FormData(); adder.append('attribute_alege-zona', Object.values(item.attributes)[0]); adder.append('attribute_tratament', Object.values(item.attributes)[1]); adder.append('quantity', 1); adder.append('product_id', item.variation_id); adder.append('variation_id', item.variation_id); console.log(adder); let me = this; let apel = setTimeout(function(){ axios({ method: "post", url: "https://HOST/?wc-ajax=add_to_cart", data: adder, headers: { "Content-Type": "multipart/form-data" }, }) .then(function (response) { }) .catch(function (response) { console.log(response); }); },1000) } }); let goCart = setTimeout(function() { window.location.href = "https://HOST/cart"; }, 1500 * this.alese.length);

Puede preguntar por qué agregué esas funciones setTimeout . En mi cabeza tenía sentido que en caso de que la llamada se demore, estoy seguro de que agregaré todos los productos al carrito. El máximo que puedo agregar es dos.

Si digamos que selecciono 5 productos, cuando me redirigen a mi carrito solo puedo ver 2 de ellos, a veces 3 (es realmente aleatorio)

¿Tiene alguna idea de cómo puedo agregar todos los productos seleccionados a la misma sesión de carrito?

¡Gracias por adelantado!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Bien, después de inspeccionar la pestaña de red, esta es la solución que funciona:

 this.toBeAdded.forEach((item, i) => { let adder = new FormData(); adder.append('attribute_alege-zona', Object.values(item.attributes)[0]); adder.append('attribute_tratament', Object.values(item.attributes)[1]); adder.append('quantity', 1); adder.append('product_id', item.variation_id); adder.append('variation_id', item.variation_id); console.log(adder); let me = this; let send = setTimeout(function() { axios({ method: "post", url: "https://HOST/?wc-ajax=add_to_cart", data: adder, headers: { "Content-Type": "multipart/form-data" }, withCredentials: true, }) .then(function (response) { if(i == 0) { document.cookie = document.cookie + " woocommerce_items_in_cart=1; path=/"; }else if (i==me.toBeAdded.length-1){ window.location.href = "https://HOST/cart"; } }) .catch(function (response) { console.log(response); }); }, i*500); });

Básicamente, si es el primer producto, estoy agregando los artículos de WC en la cookie del carrito. Si es el último producto redirijo al carrito.

Cada llamada tiene un retraso de i*500 , donde i es el índice de la matriz de productos

about 4 years ago · Juan Pablo Isaza Report
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!