【问题标题】:Remove mini-cart when no items in cart (MayaShop)当购物车中没有物品时删除迷你购物车 (MayaShop)
【发布时间】:2016-11-22 22:10:39
【问题描述】:

我在 WooCommerce 中寻求帮助。当购物车中没有物品时,我想隐藏迷你购物车和顶栏(因为它不会经常使用)。

下面是 HTML 输出。

是否需要在functions.php中添加某种“WooCommerce Hook”?

抱歉,如果需要,我什至不知道要查看哪些 PHP 文件来为该特定区域发布任何代码 sn-ps。

如果是,请告诉我。

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce cart product


    【解决方案1】:

    您位于主题中的 header.php 文件正在显示该迷你购物车。 (最好使用子主题将该文件从父主题复制到子主题,避免在更新主主题时松散自定义)。

    编辑 header.php 文件,您必须在显示 Mini-cart 的代码周围使用一个简单的条件,这样:

    // If cart is not empty display the mini-cart
    if(!WC()->cart->is_empty()){
    
        // Here goes the code that display mini cart in your header
    }
    

    这应该可以,但是如果为添加到购物车启用了 ajax,那么迷你购物车只会在移动到另一个页面或刷新实际页面时显示。


    替代方案:简单的方法(用 CSS 隐藏)

    另一种选择,应该是在 html 头部注入 CSS 规则,当购物车为空时:

    add_action('wp_head', 'hook_css', 99999);
    function hook_css() {
        // If cart is empty hide the mini-cart
        if(WC()->cart->is_empty())
            echo '<style type="text/css">#top > #cart{display:none !important;}</style>';
    }
    

    此代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

    代码有效。如果你想隐藏所有的顶栏(和小推车同时),用替换css规则(只需删除&gt; #cart

    #top{display:none !important;}
    

    【讨论】:

    • 感谢您的回复,我尝试使用以下内容:&lt;!-- TOPBAR --&gt; &lt;?php if(!WC-&gt;cart-&gt;is_empty()){ get_template_part( 'topbar' ) ?&gt; &lt;!-- END TOPBAR --&gt; 但我收到此错误:解析错误:语法错误,C:***** 中的意外'->' (T_OBJECT_OPERATOR) \theme-child\header.php 第 196 行
    • @LoicTheAztec 是否可以使用 ajax 来实现?代码运行良好,但要再次显示购物车,需要重新加载页面。
    【解决方案2】:

    感谢您的建议 LoicTheAztec 已修复它,对代码进行了一些尝试!谢谢。

    <!-- TOPBAR --> <?php if (sizeof(WC()->cart->get_cart()) != 0) { get_template_part( 'topbar' ); }?> <!-- END TOPBAR -->

    【讨论】:

    • 抱歉,我以为您可以选择两个正确答案供以后查看的人使用。已更改正确答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多