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

时间:2019-01-08 12:01:13

标签: php wordpress woocommerce popup

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

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

如果购物车中的物品数== 4,如何使它完全不显示弹出窗口?

我想通过添加一个if,然后不返回任何内容将起作用。但是弹出窗口仍然会触发。

我的代码:

    //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"]');
}

1 个答案:

答案 0 :(得分:2)

您的if / else语句不起作用,因为if ($items_count < 8)true返回了if ($items_count == 4)。您应先检查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将停止执行其余功能。