0
votes

I want to change quantity of products in mini cart widget after add into the cart. I get solution from another post of stackoverflow but for some reason its works slow to update the quantity in cart widget.. Its my first post on stackoverflow not have much idea how it works.

URL of page where have issue

Custom code in 3 parts

Part 1 of 3

//Plus Button for increase quantity 
    <a href="/tbones2/?wc-ajax=get_refreshed_fragments&add-to-cart=<?php echo $cart_item['product_id']; ?>" rel="nofollow" data-product_id="<?php echo $cart_item['product_id'] ?>" data=quantity="1" data-product_sku="<?php echo $cart_item['product_id']; ?>" class="wdiget_qty_btn add_to_cart_button ajax_add_to_cart"><i class="icon-plus-squared"></i>
</a>

Part 2 of 3

I created the template for handling the setquantity triggers.

 <?php   
/**
* Template Name: Request template for Set Quantity
* This page updates mini cart quantity for a product based on the post value
*/
//I dont think this line is needed
global $woo_options;
?>
<html>
<head>
 <?php wp_head(); ?>
</head>
<body>
 <?php
  //the cart key stores information about cart    
 $cartKeySanitized = filter_var($_POST['cart_item_key'], FILTER_SANITIZE_STRING);
 //the new qty you want for the product in cart
 $cartQtySanitized = filter_var($_POST['cart_item_qty'], FILTER_SANITIZE_STRING);  
 //update the quantity
 global $woocommerce;
 ob_start();
 $woocommerce->cart->set_quantity($cartKeySanitized,$cartQtySanitized); 
 ob_get_clean();
 wp_footer(); ?>
 <?php  woo_foot(); ?>
</body>
</html>

Part 3 of 3

Now in this part there is Ajax function for update the quantity in cart widget.

<script type="text/javascript">
    function updateQty(key, qty) {
        url = 'https://buzzpreview.com/tbones2/updatecart/';
        data = "cart_item_key=" + key + "&cart_item_qty=" + qty;

        jQuery.post(url, data).done(function (data) {
            //function updateCartFragment 
            updateCartFragment();
        });
    }

    function updateCartFragment() {
        $('#mini-loader').html('loading...');
        $fragment_refresh = {
            url: woocommerce_params.ajax_url,
            type: 'POST',
            data: {action: 'woocommerce_get_refreshed_fragments'},
            success: function (data) {
                if (data && data.fragments) {
                    jQuery.each(data.fragments, function (key, value) {
                        jQuery(key).replaceWith(value);
                    });

                    if ($supports_html5_storage) {
                        sessionStorage.setItem("wc_fragments", JSON.stringify(
                            data.fragments));
                        sessionStorage.setItem("wc_cart_hash", data.cart_hash);
                    }
                    jQuery('body').trigger('wc_fragments_refreshed');
                }
            }
        };

        //Always perform fragment refresh
        jQuery.ajax($fragment_refresh);
    }
</script>
1

1 Answers

1
votes

Please add this below method to your JS code

function supports_html5_storage() {
try {
    return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
    return false;
}
}

and change if ($supports_html5_storage) to if (supports_html5_storage) , that's it.