【问题标题】:Udating custom WooCommerce mini cart when cart contents get changed更改购物车内容时更新自定义 WooCommerce 迷你购物车
【发布时间】:2019-06-11 15:39:40
【问题描述】:

我的主题标题中有一个不错的 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>

【问题讨论】:

    标签: wordpress woocommerce cart


    【解决方案1】:

    在你的函数文件中试试这个代码:

    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-&gt;cart 获得$woocommerce-&gt;cart-&gt;get_cart_total(); 和许多其他数据。检查here

    【讨论】:

    • 谢谢 - 为项目计数工作梦想 - 但总数没有得到更新?尝试添加以下行:调用更新后的 $woocommerce->cart->get_cart_total 但没有用...有什么想法吗?
    【解决方案2】:

    好的 - 这有效:

    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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-25
      • 2016-05-02
      • 2016-12-16
      • 2023-04-08
      • 1970-01-01
      • 2019-08-07
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多