更改购物车内容时使用自定义WooCommerce迷你购物车

时间:2019-06-11 15:39:40

标签: wordpress woocommerce cart

我的主题标题(下面的代码)中有一个不错的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>

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

好-这可行:

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;

}