dsjuimtq920056 2018-12-14 21:37
浏览 81

Woocommerce迷你购物车小部件数量变化

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条回答 默认 最新

  • 普通网友 2019-01-30 05:05
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)