且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自定义添加到购物车按钮可将多个产品添加到购物车中,数量为:woocommerce

更新时间:2022-11-18 21:38:09

您需要在查询字符串中传递数量,例如:

 ?post_type = product& add-to-cart = 100& quantity = 2 

我已经修改了您的代码

 < script>jQuery('#buy').click(function(e){e.preventDefault();var myStringArray = [<?php echo $ p_id;?>];var arrayLength = myStringArray.length;for(var i = 0; i< arrayLength; i ++){addToCart(myStringArray [i],2);}返回true;//window.location.href ="http://seoexpertiser.ca/glassful/cart/";});函数addToCart(p_id,qu){$ .get('/glassful/?post_type = product& add-to-cart ='+ p_id +'& quantity ='+ qu,function(){//成功$(.show_success").show();});}</script> 

我认为这可以解决您的问题.

I want to create custom add to cart button to add my 3 product into cart with 2 quantity for each..

For add three product into cart I have done using like this:

<a id="buy" class="single_add_to_cart_button shop-skin-btn shop-flat-btn alt" href="#">ADD MY PRODUCT</a>

$p_id = my product id eg: 45,99,152

 <script>    
    jQuery('#buy').click(function(e) {
     e.preventDefault(); 
    var myStringArray = [<?php echo $p_id; ?>];
    var arrayLength = myStringArray.length;
    for (var i = 0; i < arrayLength; i++) {
     addToCart(myStringArray[i]);
    }

    return true;
         });

    function addToCart(p_id) {
     $.get('/glassful/?post_type=product&add-to-cart=' + p_id, function() {
    $(".show_success").show();
     });

    }
    </script>

It will add my resulted product into cart but with only 1 quantity Please let me know how I can add quantity? I want to add 2 quantity of each product.

Mean when click on add to cart three product will added to cart with 2 quantity each.

Thanks for help in advance..

You need to pass quantity in query string like:

?post_type=product&add-to-cart=100&quantity=2

I have modify you code

 <script>
    jQuery('#buy').click(function(e) {
     e.preventDefault(); 
    var myStringArray = [<?php echo $p_id; ?>];
    var arrayLength = myStringArray.length;
    for (var i = 0; i < arrayLength; i++) {
     addToCart(myStringArray[i],2);
    }

    return true;

    //window.location.href = "http://seoexpertiser.ca/glassful/cart/";
    });

    function addToCart(p_id,qu) {
     $.get('/glassful/?post_type=product&add-to-cart=' + p_id +'&quantity='+qu, function() {
     // success
    $(".show_success").show();
     });

    }
    </script>

I think this will solve your problem.