首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当购物车内容发生变化时更新自定义WooCommerce迷你购物车

当购物车内容发生变化时更新自定义WooCommerce迷你购物车
EN

Stack Overflow用户
提问于 2019-06-11 23:39:40
回答 2查看 677关注 0票数 0

我有一个很好的小WooCommerce自定义购物车在我的主题标题(代码如下)。然而..。当客户在购物车页面上更改订购的商品数量,或者从购物车中移除商品时-购物车中的商品数量和价格在页面重新加载之前不会更新...有没有办法在更新购物车表格以更新我的定制主题标题中的定制迷你购物车时获得AJAX重新加载?

代码语言:javascript
复制
<span class="shoppingSummaryBar_Total">
<!-- render value -->
<?php global $woocommerce; ?>
<?php echo $woocommerce->cart->get_cart_total(); ?>
</span>
<span class="shoppingSummaryBar_Items">
<!-- render no of items -->
<?php if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

$count = WC()->cart->cart_contents_count;
?><a class="cart-contents" href="<?php echo WC()->cart->get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php 
if ( $count > 0 ) {
    ?>
    <span class="cart-contents-count"><?php echo esc_html( $count ); ?></span>
    <?php
}
    ?>
    Items
    </a>

<?php } ?>
</span>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-12 00:25:40

在您的函数文件中尝试以下代码:

代码语言:javascript
复制
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );

function woocommerce_header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;
    ob_start();
    ?>

    <span class="cart-contents-count"><?php echo $woocommerce->cart->cart_contents_count; ?></span>

    <?php
    $fragments['span.cart-contents-count'] = ob_get_clean();
    return $fragments;
}

此外,通过$woocommerce->cart,您还可以获得$woocommerce->cart->get_cart_total();和许多其他数据。检查here

票数 0
EN

Stack Overflow用户

发布于 2019-06-12 03:18:32

好的--这是可行的:

代码语言:javascript
复制
add_filter( 'woocommerce_add_to_cart_fragments', 'iconic_cart_count_fragments', 10, 1 );

function iconic_cart_count_fragments( $fragments ) {

$fragments['span.shoppingSummaryBar_Total'] = '<span class="shoppingSummaryBar_Total">' . WC()->cart->get_cart_total() . '</div>';
$fragments['span.cart-contents-count'] = '<span class="cart-contents-count">' . WC()->cart->get_cart_contents_count() . '</div>';

return $fragments;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56547271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档