我有一个很好的小WooCommerce自定义购物车在我的主题标题(代码如下)。然而..。当客户在购物车页面上更改订购的商品数量,或者从购物车中移除商品时-购物车中的商品数量和价格在页面重新加载之前不会更新...有没有办法在更新购物车表格以更新我的定制主题标题中的定制迷你购物车时获得AJAX重新加载?
<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>
发布于 2019-06-12 00:25:40
在您的函数文件中尝试以下代码:
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。
发布于 2019-06-12 03:18:32
好的--这是可行的:
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;
}
https://stackoverflow.com/questions/56547271
复制相似问题