【问题标题】:Add class to div when woocommerce cart is empty当 woocommerce 购物车为空时向 div 添加类
【发布时间】:2020-05-01 17:27:50
【问题描述】:

我想添加一个类(或直接设置样式)一个元素,该元素仅在购物车为空时包含购物车计数。

下面是我目前使用的代码,效果很好。购物车数量很好,但颜色没有更新。

header.php(在菜单中的购物车图标之后添加购物车计数)

?>          
<div class="counter_bubble">
        <a href="/cart">
            <span class="counter" id="cart-count"><?php
            $cart_count = WC()->cart->get_cart_contents_count();
            echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
            ?></span>
        </a>
</div>
<?php

functions.php(排队自定义 javascript + 购物车刷新)

// ADD CUSTOM JS FILE FROM CHILD THEME
add_action('wp_enqueue_scripts', 'bubblescript');

function bubblescript() {
    wp_enqueue_script( '_tandt-main', get_template_directory_uri() . 
    '/../Graffont/js/gft-script.js', array("jquery"), $my_js_ver, true );
}

// REFRESH CART FRAGMENTS (ITEM COUNT) SO CORRECT NUMBER IS ALWAYS SHOWN
add_filter( 'woocommerce_add_to_cart_fragments', 'refresh_cart_count', 50, 1 );

function refresh_cart_count( $fragments ){
    ob_start();
    ?>
    <span class="counter" id="cart-count"><?php
    $cart_count = WC()->cart->get_cart_contents_count();
    echo sprintf ( _n( '%d', '%d', $cart_count ), $cart_count );
    ?></span>
    <?php
     $fragments['#cart-count'] = ob_get_clean();

    return $fragments;
}

gft-script.js

jQuery(document).ready(function($) {

function changecartbubble() {
    if ($('body').find('#cart-count').length > 0) {
        if (parseInt($('#cart-count').html()) > 0) {
            // cart has number > 0 so cart has items
            //change background class
            $('#cart-count').addClass('other-background-of-bubble');
        }
        else {
            // cart has no number so cart has no items
            //remove background class
            $('#cart-count').removeClass('other-background-of-bubble');
        }
    }
}

});

style.css - 当前的 CSS

.counter {
    background-color: #ff0;
    border-radius: 20px;
    font-size: 10px;
    padding: 0 4px 0 4px;
}

.other-background-of-bubble{
    background-color:red!important;
}

【问题讨论】:

    标签: php css wordpress woocommerce cart


    【解决方案1】:

    您可以在文档准备好时调用此函数,并且只要 ajax 更新购物车。

    Javascript/jQuery:

    function changecartbubble() {
        if ($('body').find('#cart-count').length > 0) {
            if (parseInt($('#cart-count').html()) > 0) {
                // cart has number > 0 so cart has items
                //change background class
                $('#cart-count').addClass('other-background-of-bubble');
            }
            else {
                // cart has no number so cart has no items
                //remove background class
                $('#cart-count').removeClass('other-background-of-bubble');
            }
        }
    }
    

    风格:

    .other-background-of-bubble{
        background:red!important;
    }
    

    【讨论】:

    • 根据您的建议,我在我的 functions.php 中添加了一个 wp_enque_script,它使用您的代码调用 js 文件。 js文件中的代码被jQuery(document).ready(function($) { //CODE// });包围但我没有看到任何变化。有关如何实施的任何进一步建议?
    • 确保在控制台中看不到任何错误,并且在调用函数之前已成功加载 jQuery。另外,请确保您在 functions.php 中具有正确的脚本顺序
    • 我已经测试了上面的javascript函数,它工作正常。回显所有内容后,您在 #cart-count 中看到的输出是什么?
    • 感谢您的跟进和帮助。我有点菜鸟,所以我可能会遗漏一些东西。我已经用我添加的新代码更新了我原来的问题。控制台中没有错误,我没有看到正在回显的类。购物车中商品的输出为 1 - 如果有助于查看实际情况,则站点为 graffont.com
    • 我在运行函数 changecartbubble() 时在控制台中看到错误;
    猜你喜欢
    • 2017-12-20
    • 2013-09-16
    • 2019-12-26
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2020-10-28
    • 1970-01-01
    相关资源
    最近更新 更多