当woocommerce购物车为空时隐藏数字0

时间:2019-01-30 21:59:35

标签: php html woocommerce

嗨,我为我的网上商店购买了woocommerce主题。主题通常在购物车图标旁边显示数字0,即使它为空也是如此。我如何隐藏0并仅在购物车中有商品时才显示商品数量?例如,当购物车为空时,应仅是图标,如果我将其2项放入其中,则应向图标显示一个数字

这是我在主题文件中找到的代码:

       <a itemprop="url" <?php tonda_select_class_attribute( $dropdown_cart_icon_class ); ?> href="<?php echo esc_url(wc_get_cart_url()); ?>">
        <span class="qodef-cart-icon"><?php echo tonda_select_get_dropdown_cart_icon_html(); ?></span>
        <span class="qodef-cart-number"><?php echo sprintf(_n('%d', '%d', WC()->cart->cart_contents_count, 'tonda'), WC()->cart->cart_contents_count); ?></span>
    </a>

1 个答案:

答案 0 :(得分:0)

您可以检查if语句中的项目数。如果数字大于0,请显示数字。

<a itemprop="url" <?php tonda_select_class_attribute( $dropdown_cart_icon_class ); ?> 
    href="<?php echo esc_url(wc_get_cart_url()); ?>">
    <span class="qodef-cart-icon"><?php echo tonda_select_get_dropdown_cart_icon_html(); ?></span>
    <?php if( WC()->cart->cart_contents_count > 0 ) : ?>
        <span class="qodef-cart-number"><?php echo sprintf(_n('%d', '%d', WC()->cart->cart_contents_count, 'tonda'), WC()->cart->cart_contents_count); ?></span>
    <?php endif; ?
</a>