Woocommerce购物车为空时,将CSS类添加到项目 [英] Add a CSS class to an item when Woocommerce cart is empty

查看:48
本文介绍了Woocommerce购物车为空时,将CSS类添加到项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在购物车为空时隐藏我的购物车,所以我决定在购物车为空时向购物车HTML项添加CSS类,这是我当前的代码:

I am trying to hide my cart when it is empty, so I decided to add a CSS class to the cart HTML item when the cart is empty, here is my current code:

 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文件中

I am adding this to my functionts.php file

我想念什么吗?

推荐答案

如果将类添加到正文中以在CSS中构成层次结构,那将是很好的.在functions.php中使用以下代码:

It will be good if you add the class to body to make the hierarchy in CSS. Use the following code in 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' );

此代码将在正文中添加一个空购物车"类.
希望这能解决您的问题.

This code will add a class "empty-cart" to body.
Hope this will solve your problem.

这篇关于Woocommerce购物车为空时,将CSS类添加到项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆