我有一些有效的代码正在执行以下条件,以触发购物车页面上的弹出窗口...
elementor
shortcode
。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"]');
}
答案 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
将停止执行其余功能。