如果购物车中有 4 件商品,则 WooCommerce 不显示弹出窗口



我有一些工作代码正在执行如下条件以触发购物车页面上的弹出窗口...

  • 如果购物车中的商品少于 8 件,则显示带有 elementor shortcode 的弹出窗口。
  • 如果 8 个项目或更多,则显示带有 wof_wheel 的弹出窗口。

如果购物车中的商品数量 == 4,我如何让它根本不显示弹出窗口?

我认为通过添加一个 if else,并且不返回任何内容,它会起作用。但是弹出窗口仍然会触发。

我的代码:

    //Shortcode Check 
function checkShortCode()
{
    $page = get_post(5);
    if (WC()->cart) {
        $items_count = WC()->cart->get_cart_contents_count();

        if  ( $items_count < 8 ) {
            //Remove the Default Hook function for this shortcode
            remove_shortcode('wof_wheel');
            //Add custom callback for that short to display message required
            add_shortcode('wof_wheel', 'myCustomCallBack');
        }else if ($items_count == 4) {
        return; //Here I am trying to return nothing...
        }
    }
}
add_action('wp_loaded', 'checkShortCode');
function myCustomCallBack()
{
    echo do_shortcode('[elementor-template id="3431"]');
}

你的 if/else 语句不起作用,因为if ($items_count < 8)返回if ($items_count == 4)true。您应该先检查if ($items_count == 4),然后再检查if ($items_count < 8)

希望这有帮助:

//Shortcode Check 
function checkShortCode()
{
    $page = get_post(5);
    if (WC()->cart) {
        $items_count = WC()->cart->get_cart_contents_count();
        if ($items_count == 4) {
            return;
        } 
        if  ($items_count < 8) {
            //Remove the Default Hook function for this shortcode
            remove_shortcode('wof_wheel');
            //Add custom callback for that short to display message required
            add_shortcode('wof_wheel', 'myCustomCallBack');
        }
    }
}
add_action('wp_loaded', 'checkShortCode');
function myCustomCallBack()
{
    echo do_shortcode('[elementor-template id="3431"]');
}

而且您实际上不需要else if因为return将停止执行其余函数。