首页 > 解决方案 > 如果 WooCommerce 购物车为空,则隐藏一些 html 元素

问题描述

我的标题中有一个菜单购物车图标。目前,菜单购物车图标始终可见。我希望它只有在购物车中有物品时才可见。所以我试图让它在购物车为空时隐藏菜单购物车图标。

我试过了:

add_action( 'wp_footer', function() {
    
    if ( WC()->cart->is_empty() ) {
        
        echo '<style type="text/css">.elementor-widget-hfe-cart{ display: none; }</style>';
    
    }

});

但它不起作用。为什么?


编辑

事实证明,我的托管服务提供商不久前为我的网站打开了缓存,因此我看不到更改。代码片段工作正常...

标签: phpcsswordpresswoocommercecart

解决方案


您可以使用sizeof下面的代码检查功能检查购物车的大小。

add_action( 'wp_footer', function() {    
    if ( sizeof( WC()->cart->get_cart() ) < 1 ) {
        echo '<style type="text/css">.elementor-widget-hfe-cart{ display: none; }</style>';
    }
});

推荐阅读