首页 > 解决方案 > woocommerce 购物车为空时隐藏元素,出现在 AJAX 上添加到购物车

问题描述

我有一个 woocommerce 商店,其中购物车中的商品数量显示在标题中的购物车图标旁边。该区域的 CSS 选择器是.woofc-menu-item-inner:after。我目前正在使用下面的代码在购物车为空时隐藏该元素:

    // Remove Item Count when Bag is Empty //

add_action('wp_head', 'hook_css', 99999);
function hook_css() {
    // If cart is empty hide the item count
    if(WC()->cart->is_empty())
        echo '<style type="text/css"> .woofc-menu-item-inner:after{display:none !important;}</style>';
}

但是,上面的代码仅适用于页面加载。我目前使用几个插件(Ajax add to cart for WooCommerceAjax Cart AutoUpdate for WooCommerce)在不重新加载页面的情况下更新购物车的内容。当使用 AJAX 添加/删除项目时,是否有显示/隐藏此元素?

标签: phpajaxfunctionwoocommercecart

解决方案


推荐阅读