进度条随购物车一起消失,但页脚图标列表也在 WooCommerce 网站中消失

问题描述 投票:0回答:1

我在 theme.php 文件中的 WooCommerce 网站上为购物车页面实现了一个进度条,它按预期工作,当购物车为空时消失。但是,我遇到了一个问题,当购物车为空时,不仅进度条消失,而且页脚中的图标列表也消失。

function remove_elementor_widget_container_if_cart_empty() {
    // Check if WooCommerce is active and if we are on the cart page
    if ( class_exists('WooCommerce') && is_cart() && WC()->cart->is_empty() ) {
        // If the cart is empty and it's the cart page, remove the elementor-widget-container div
        add_filter('the_content', 'remove_elementor_widget_container');
    }
}

function remove_elementor_widget_container($content) {
    // Remove the elementor-widget-container div from the content using regular expression
    $content = preg_replace('/<div class="elementor-widget-container">.*?<\/div>/s', '', $content);
    return $content;
}

add_action('wp', 'remove_elementor_widget_container_if_cart_empty');[![Progress bar][1]][1]
php wordpress woocommerce themes devtools
1个回答
0
投票

您遇到的问题是由于

remove_elementor_widget_container()
函数从页面中删除了所有
elementor-widget-container
div,包括页脚中包含图标列表的 div 引起的。

要解决此问题,您可以使用

is_active_widget()
功能在删除之前检查小部件是否处于活动状态。如果小部件处于活动状态,以下代码只会删除
elementor-widget-container
div:

function remove_elementor_widget_container_if_cart_empty() {
    // Check if WooCommerce is active and if we are on the cart page
    if ( class_exists('WooCommerce') && is_cart() && WC()->cart->is_empty() ) {
        // If the cart is empty and it's the cart page, remove the elementor-widget-container div if the widget is active
        add_filter('the_content', 'remove_elementor_widget_container_if_active');
    }
}

function remove_elementor_widget_container_if_active($content) {
    // Check if the widget is active
    $is_active = is_active_widget('elementor_widget_icon_list');

    // Remove the elementor-widget-container div from the content if the widget is active
    if ($is_active) {
        $content = preg_replace('/<div class="elementor-widget-container">.*?<\/div>/s', '', $content);
    }

    return $content;
}

add_action('wp', 'remove_elementor_widget_container_if_cart_empty');

如果

elementor-widget-container
小部件处于活动状态,此代码只会删除
elementor_widget_icon_list
div。如果小部件未处于活动状态,则 div 将不会被删除,并且图标列表将在页面上保持可见,即使购物车是空的也是如此。

© www.soinside.com 2019 - 2024. All rights reserved.