php - 如果 WooCommerce 购物车为空,则隐藏一些 html 元素
问题描述
我的标题中有一个菜单购物车图标。目前,菜单购物车图标始终可见。我希望它只有在购物车中有物品时才可见。所以我试图让它在购物车为空时隐藏菜单购物车图标。
我试过了:
add_action( 'wp_footer', function() {
if ( WC()->cart->is_empty() ) {
echo '<style type="text/css">.elementor-widget-hfe-cart{ display: none; }</style>';
}
});
但它不起作用。为什么?
编辑
事实证明,我的托管服务提供商不久前为我的网站打开了缓存,因此我看不到更改。代码片段工作正常...
解决方案
您可以使用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>';
}
});
推荐阅读
- hibernate - 用户创建的休眠标识符问题
- javascript - 从 android 应用程序“net::ERR_NAME_NOT_RESOLVED”运行 tor.so
- firebase - 当 Firestore 文档可能包含或不包含 Flutter 的字段时如何处理
- spring - 如何使用 spring-hibernate 集成以非事务方式执行方法(具有 INSERT 和 UPDATE 操作)
- c - C - 用随机整数填充二维数组
- android - 如何检查 SwitchPreference 的当前状态?
- r - 在 R/dplyr 中,当使用 'pivot_wide' 重塑数据框时,如何将数字与重塑过程相加
- android - 在 MaterialToolbar 中更改 navigationIcon 的大小
- pytorch - 在pytorch中,方括号索引和“index_select”有什么区别?
- salesforce - 案例分配规则公式