duanna2026 2016-10-11 01:42
浏览 69
已采纳

Woocommerce添加到购物车ajax和迷你购物车

I need to re-populate mini-cart when product added via ajax add to cart. I manage to update cart quantity with filter woocommerce_add_to_cart_fragments like this:

add_filter( 'woocommerce_add_to_cart_fragments', function($fragments) {

    ob_start();
    ?>

    <div class="cart-contents">
        <?php echo WC()->cart->get_cart_contents_count(); ?>
    </div>

    <?php $fragments['div.cart-contents'] = ob_get_clean();

    return $fragments;

} );

And my HTML markup is

<div class="cart-contents">
    <?php echo WC()->cart->get_cart_contents_count(); ?>
</div>

Bellow that is hidden div witch showing on .cart-contents hover

<div class="header-quickcart"><?php woocommerce_mini_cart(); ?></div>

I want to update this div content same way or similar to woocommerce_add_to_cart_fragments. Or should I change HTML markup and hold everything in 1 div? What is common way or best practice to doing that?

  • 写回答

1条回答 默认 最新

  • dpd20130 2016-10-11 02:05
    关注

    Ok so I just realized that I can use woocommerce_add_to_cart_fragments filter 2 times, like so:

    add_filter( 'woocommerce_add_to_cart_fragments', function($fragments) {
    
        ob_start();
        ?>
    
        <div class="cart-contents">
            <?php echo WC()->cart->get_cart_contents_count(); ?>
        </div>
    
        <?php $fragments['div.cart-contents'] = ob_get_clean();
    
        return $fragments;
    
    } );
    
    add_filter( 'woocommerce_add_to_cart_fragments', function($fragments) {
    
        ob_start();
        ?>
    
        <div class="header-quickcart">
            <?php woocommerce_mini_cart(); ?>
        </div>
    
        <?php $fragments['div.header-quickcart'] = ob_get_clean();
    
        return $fragments;
    
    } );
    

    First updating quantity and aother refreshing mini-cart view.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao