【问题标题】:Woocommerce need to update total count shows next to minicart iconWoocommerce 需要更新 minicart 图标旁边的显示总数
【发布时间】:2021-11-26 02:12:27
【问题描述】:

我正在使用 Riode 主题。我需要更改标题菜单迷你购物车图标中显示的购物车项目总数。我有两种产品。 1是基于体重其他是正常的。 例如:我的购物车物品 => Product1 - 35gm - $50.00 ; 产品 2 - 2(项目) - 30.00 美元; 在这里,我需要将购物车商品总数计为 3。但现在它显示为 37。

这是我的代码。

<?php
add_filter( 'woocommerce_add_to_cart_fragments', 'add_to_cart_fragment',10,1);

function add_to_cart_fragment($fragments){
   
   ob_start();
   $items_count=0;
    
   foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
   $product = $cart_item['data'];
   $product_id = $cart_item['product_id'];
   $quantity = $cart_item['quantity'];
   if('some conditions'){
       $items_count+=1;
      }else{
       $items_count+=$quantity;
      }
   }
        
?>
    <a class="cart-toggle" href="<?php echo wc_get_cart_url(); ?>"><i class="d-icon-bag">
        <span class="cart-count" style="opacity: 1;"><?php echo $items_count; ?> </span></i>
    </a>

    <?php 
        $fragments['a.cart-toggle'] = ob_get_clean();
        return $fragments;
}

【问题讨论】:

    标签: wordpress woocommerce hook-woocommerce minicart


    【解决方案1】:

    简化您的代码

    add_filter( 'woocommerce_add_to_cart_fragments', 'add_to_cart_fragment',10,1);
    
    function add_to_cart_fragment($fragments){
       
       ob_start();
       global $woocommerce;
       $total_items = $woocommerce->cart->cart_contents_count;
            
    ?>
        <a class="cart-toggle" href="<?php echo wc_get_cart_url(); ?>"><i class="d-icon-bag">
            <span class="cart-count" style="opacity: 1;"><?php echo $total_items; ?> </span></i>
        </a>
    
        <?php 
            $fragments['a.cart-toggle'] = ob_get_clean();
            return $fragments;
    }
    

    【讨论】:

    • 请不要发布多个答案,除非您试图提出两个不同的观点
    猜你喜欢
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多