简体   繁体   English

将多个产品添加到 WooCommerce 购物车 - AJAX

[英]Adding multiple products to WooCommerce Cart - AJAX

I am trying to find a way to select multiple item variations from a Vue frontend inside a WooCommerce website.我正在尝试从 WooCommerce 网站内的 Vue 前端找到 select 多个项目变体的方法。

I installed the WC Ajax Cart plugin in order to have some sort of endpoint that I can call using AXIOS.我安装了 WC Ajax 购物车插件,以便拥有可以使用 AXIOS 调用的某种端点。

Each option I select ads and I to an array of called selectedProducts每个选项我 select 广告和我到一个名为selectedProducts的数组

When the user presses on checkout what I do is:当用户按下checkout时,我所做的是:

     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);

You might ask why I added those setTimeout functions.你可能会问我为什么要添加那些setTimeout函数。 In my head it made sense that in case the call takes longer I am sure I will add all the products to the cart.在我看来,如果通话时间更长,我确信我会将所有产品添加到购物车中,这是有道理的。 The max I can add is two.我最多可以添加两个。

If let's say I select 5 products, when I get redirected to my cart I can only see 2 of them, sometime 3 (it is really really random)如果假设我 select 5 个产品,当我被重定向到我的购物车时,我只能看到其中 2 个,有时是 3 个(真的很随机)

Do you have any idea how can I add all the selected products to the same cart session?您知道如何将所有选定的产品添加到同一个购物车 session 吗?

Thanks in advance!提前致谢!

Ok so after inspecting the network tab this is the solution that works:好的,所以在检查了网络选项卡之后,这是可行的解决方案:

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);
       });

Basically if it is the fist product I am adding the WC items in cart cookie.基本上,如果它是第一个产品,我将在购物车 cookie 中添加 WC 项目。 If it is the last product I redirect to cart.如果它是我重定向到购物车的最后一个产品。

Each call is delayed by i*500 , where i is the index of the array of products每次调用都会延迟i*500 ,其中i是产品数组的索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM