Woocommerce添加到购物车ajax和微型购物车 [英] Woocommerce add to cart ajax and mini-cart

查看:290
本文介绍了Woocommerce添加到购物车ajax和微型购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过ajax添加的产品添加到购物车时,我需要重新填充小型购物车。我设法使用过滤器woocommerce_add_to_cart_fragments更新购物车数量,如下所示:

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;

} );

我的HTML标记是

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

被隐藏的div女巫在.cart-contents悬停上显示的家伙

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

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

我想以与woocommerce_add_to_cart_fragments相同或相似的方式更新此div内容。还是应该更改HTML标记并将所有内容保持在1格内?

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?

推荐答案

好,所以我才意识到我可以使用woocommerce_add_to_cart_fragments过滤器2次,像这样:

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.

这篇关于Woocommerce添加到购物车ajax和微型购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆