如果项目数大于 4,则 Wordpress 显示简码

Wordpress display shortcode if item count is greater than 4

我正在使用一个输出短代码的插件(由其他人开发)[wof_wheel id="1111"]。我在页面上使用这个简码。

如果购物车中的商品数量大于 4,我正在尝试将条件逻辑应用于简码 display/run 简码。

我知道如何使用 WC()->cart->get_cart_contents_count() 获取并检查购物车商品数量,但不确定是否可以实现简码 display/run 逻辑。

function do_shortcode() {     

$items_count = WC()->cart->get_cart_contents_count();

        if ($items_count > 4) {
DISPLAY/RUN SHORTCODE
} else if ($items_count < 4) 
    {
DO NOT DISPLAY/RUN SHORTCODE
}
}

短代码可以实现这种类型的条件逻辑吗?

您可以使用以下代码添加条件简码:

    $cartcount = WC()->cart->get_cart_contents_count();

    if ($cartcount > 4) { 
      echo do_shortcode( '[wof_wheel id="1111"]' );
    }else{
     //item count is less than 4
   }

注意:在上面的问题中你重新声明预定义函数是不正确的,do_shortcode()是预定义函数,用于回显模板文件中的简码。

如需更多帮助,请参阅此 link:Click Here

您可以使用所需的条件逻辑将简码嵌入到自定义简码中:

add_shortcode( 'my_wheel', 'custom_conditional_wof_wheel' );
function custom_conditional_wof_wheel( $atts ){
    $atts = shortcode_atts( array(
        'id' => '',
        'count' => '4', // 4 cart items by default
    ), $atts, 'my_wheel' );

    // If there is more than 4 items count in cart the shortcode [wof_wheel] is executed
    if( WC()->cart->get_cart_contents_count() > $atts['count'] ){
        $id = $atts['id'];
        return do_shortcode( "[wof_wheel id='$id']" );
    }
    // Else it display nothing
    return ''; 
}

代码进入您的活动子主题(活动主题)的 function.php 文件。测试和工作。
(最后看看我是如何测试的)。

USAGE:

1) For more than 4 items in cart (4 items is set by default in the shortcode):

[my_wheel id="1111"]

2) For more than 6 items in cart for example:

[my_wheel id="1111" count='6']

这是如何测试的。

由于我无法测试这个来自特定第三方插件的简码,我创建了一个 [my_wheel] 简码,它将输出简码参数 id 中提供的 ID:

add_shortcode( 'my_wheel', 'custom_conditional_wof_wheel' );
function custom_conditional_wof_wheel( $atts ){
    $atts = shortcode_atts( array(
        'id' => '',
        'count' => 4,
    ), $atts, 'my_wheel' );

    if( WC()->cart->get_cart_contents_count() > $atts['count'] ){
        $id = $atts['id'];
        return do_shortcode( "[wof_wheel id='$id']" );
    }
    return '';
}

代码进入您的活动子主题(活动主题)的 function.php 文件。

然后我在页面的 Wordpress 文本编辑器中添加了 [my_wheel id="1111"] 短代码,当购物车项目数为 5 或更多时,我得到了这个显示:

所以它有效。

@LoicTheAztec 是一个很好的解决方案,但如果您想要其他解决方案,可以使用以下方法:

WordPress 内置函数,允许您删除短代码的默认回调函数,并将其替换为自定义函数。

在这种情况下,我们将检查购物车内容计数是否超过 4,然后删除默认回调并将其替换为我们的。

我会考虑例如您的页面 ID 是 49,当您使用此代码匹配包含短代码的页面时,您应该更改它。

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


        if ($items_count == 4) {
            //Remove the Default Hook function for this shortcode
            remove_shortcode('wof_wheel');
            //Add custom callback for that short to display whatever message you want
            add_shortcode('wof_wheel', 'myCustomCallBack');
        }
    }
}
add_action('wp_loaded', 'checkShortCode');

现在我们需要添加自定义回调以显示您想要的任何消息:

function myCustomCallBack()
{
    echo 'my shortcode is running';
}

上面的代码已经过测试,100% 有效