2

I am using a plugin (developed by someone else) which is outputting a shortcode [wof_wheel id="1111"]. I am using this shortcode on a page.

I am trying to apply conditional logic to the shortcode to only display/run the shortcode IF the number of items in the cart is greater than 4.

I know how to get and then check the cart item count with WC()->cart->get_cart_contents_count() but not sure if its possible to implement the shortcode display/run logic.

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
}
}

Is this type of conditional logic possible with shortcodes?

3 Answers 3

2

What @LoicTheAztec is one good solution but if you want another solution you can use the following:

WordPress have built in function which allow you to remove the default callback function for your shortcode and replaced it with your custom one.

In this case we are going to check if the cart content count is more than 4 then remove the default callback and replace it with ours .

i will consider for example that the page id which you have is 49 here which you should change when using this code to match the page which contain the shortcode.

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

now we need to add our custom callback to display whatever message you want:

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

the code above is tested and working 100%

Sign up to request clarification or add additional context in comments.

8 Comments

I need the ID in the shortcode to trigger correctly, do I need to modify the code AND the page shortcode?
@Giuls you just need to replace the page id you don't need the id of the short code the name is enough
Okay, I've added the above code with the matching page ID (5). On my page (5), I have the shortcode entered [wof_wheel id="1111"] However it is not triggering at all. Is there something else I'm missing?
@Giuls one sec let me test it with exact shortcode syntax
Sorry, it triggers (The shortcode runs/displays permanently), but it doesn't follow the conditional rules in the code
|
2

You can embed a shortcode in a custom shortcode with the needed conditional logic:

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 ''; 
}

Code goes in function.php file of your active child theme (active theme). tested and works.
(see at the end how I have tested that).

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']

How this has been tested.

As I can't test this shortcode that comes from a specific third party plugin, I have created a [my_wheel] shortcode that will output the ID provided in the shortcode argument 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 '';
}

Code goes in function.php file of your active child theme (active theme).

Then I have added [my_wheel id="1111"] shortcode in the Wordpress text editor of a page and I get this display when the cart items count is 5 or more:

enter image description here

So it works.

4 Comments

It doesn't seem to be working. I have added the code and corrected a typo in your answer on line 10, this should be 'id', i think? On my wordpress page I am using the shortcode my_wheel_id='1111' but the output of the plugin isn't displaying at all now no matter how many items in the cart.
@Giuls I will wtry with another shortcode to check if it works as i can't use yours and I come back to you.
@Giuls My code works: I have added some explanations and I show how I have tested it to be sure that it works
I appreciate your explanations and testing. It is strange. I think perhaps a conflict with third party plugin which doesn't like this way of conditional logic... :(
1

You can add conditional shortcode by using below code :

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

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

Note : In the above question you re-declare the predefined function which is incorrect , do_shortcode() is predefined function for echo the shortcode in template file.

For more help see this link : Click Here

1 Comment

I've implemented your code, and kept the shortcode [wof_wheel id="1111"] on my worpdress page, but it is not respecting the conditional logic. It still displays no matter how many items are in the cart

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.