Woocommerce迷你购物车小部件数量更改

时间:2018-12-14 21:37:12

标签: php wordpress woocommerce cart

添加到购物车后,我想更改迷你购物车小部件中的产品数量。我从stackoverflow的另一篇文章中获得了解决方案,但是由于某种原因,它在更新购物车小部件中的数量上工作缓慢。.我在stackoverflow上的第一篇文章对它的工作原理并不了解。

有问题的页面的网址

3个部分的自定义代码

第1部分,共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>

第2部分(共3部分)

我创建了用于处理setquantity触发器的模板。

 <?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>

3之3

这部分现在有Ajax函数来更新购物车小部件中的数量。

<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 个答案:

答案 0 :(得分:0)

请将以下方法添加到您的JS代码中

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

然后将if($ supports_html5_storage)更改为if(supports_html5_storage)。