2017-07-15 106 views
1

我試圖在空的時候隱藏我的購物車,因此我決定在購物車爲空時添加CSS類到購物車HTML項目,這裏是我的當前代碼:當Woocommerce購物車爲空時,爲商品添加CSS類

function x_woocommerce_navbar_menu_item($items, $args) { 
     if (WC()->cart->cart_contents_count == 0) { 
     echo '<script type="text/javascript"> 
        $(document).ready(function() { 
         $("#header_cart").addClass("zero"); 
        }); 
       </script>'; 
     } 

我加入這個我functionts.php文件

我錯過了什麼?

+1

add_action('wp_footer','x_woocommerce_navbar_menu_item');如果你正在添加腳本 –

回答

1

,如果你的類添加到人體,使在CSS層級這將是很好的。在functions.php中使用以下代碼:

function tristup_body_classes($classes) 
    { 

     global $woocommerce; 
     if(is_cart() && WC()->cart->cart_contents_count == 0){ 
      $classes[]='empty-cart'; 
     } 
     return $classes; 
    } 
    add_filter('body_class', 'tristup_body_classes'); 

此代碼將爲類「body-cart」添加一個類。
希望這能解決你的問題。

+0

謝謝,它的工作原理,但我需要刪除is_cart()條件 –

0

請試試這個。

function cart_empty_add_class() { 
 
    global $woocommerce; 
 
    if (empty($woocommerce->cart->cart_contents)) { 
 
     echo '<script type="text/javascript"> 
 
        $(document).ready(function() { 
 
         $("#header_cart").addClass("zero"); 
 
        }); 
 
       </script>'; 
 
exit; 
 
    } 
 
} 
 
add_action('wp_head', 'cart_empty_add_class');

+0

它是失蹤,它不工作,我的網頁變成空白 –

+0

請詳細說明它或寫你遇到的實際問題。 –

相關問題