How to Add Conditional Content to the Top of WooCommerce Cart and Checkout Pages – by Product ID
In part 3 of our WooCommerce Cart and Checkout Pages series, we’re going to take our hook from part 1 and make the function conditional based on which Product IDs are currently in the cart.
Let’s get moo-vin’!
>> Missed part 1 and want to see how we created our initial function? Catch up here!
///
Why would you need to add conditional content by Product ID?
If you’ve been reading along, you already know that adding content to the Cart and Checkout pages is super duper handy.
However, one of the more common requests we get here at Mad Cow HQ is to only show a message to customers who have certain products in their carts.
///
How do you add conditional content by Product ID?
To illustrate how to do this, we’re going to add (to our existing function from part 1) a conditional statement for the above scenario.
In the video demo (link below), I’m using the standard demo content from WooCommerce. So you will see that we’re going to make our function conditionally display only if there are specific products in the cart.
—
Step 1 – Create a loop function.
- We need to create a function that will loop through all of the items (or “objects” in PHP-land) in the customer’s cart.
- Then we need to create a list (or “array”) of all of the IDs that show up.
—
Step 2 – Compare the array intersect.
In PHP, a list of things is referred to as an array. Our next step is to compare the array we just built with the ID (or IDs) that we’re looking for. (This is called an “array intersect.“)
What this function will do is evaluate two different arrays.
- If any of the items in “array 1” match any of the items in “array 2,” the function will return “true.”
- We’ll then use that “return” of true or false (a boolean) to base our conditional statement on.
- If there’s a match in the two arrays, then the function will show our awesome content.
- If not, do nothing.
///
Let’s take a look at the code.
Firstly, I can’t take credit for this code, but I can’t give credit either because I’ve forgotten where I found it! Just like most of us, I checked a hundred different Stack Overflow pages before finding what I wanted.
Secondly, this code is very nicely commented, so I highly recommend reading all the blue comments below.
I like to show the code visually since VSCode (Visual Studio Code) looks so much better than just plain text.
If you click on the image above, it will bring you to the gist where you can copy it, if you’d like. Or you can view the cart/checkout code from the video.
—
What’s the code doing, in a nutshell?
As with many PHP functions, there are multiple ways of doing this. I have just found that this one makes sense to me and continues to work quite well.
- In a nutshell, this function is getting all of the items in the cart (line 7).
- Then it creates an array of all the IDs in the cart (line 9) by looping through each one and pulling just the ID.
- Lastly (line 12), it will return “true” if the “array intersect” we talked about above finds a match.
—
How is $ids being defined?
You will notice that the two arrays it is comparing (line 12) are defined on line 3 and line 9, but how is $ids being defined?
In order for this function to work correctly, there needs to be a value assigned to the $ids variable. This may get a little in the weeds of PHP functions, but it’s super important to our conditional hook.
In a PHP function (line 3) this is called an “argument,” but it is not like the ones you have with your significant other!
- An argument is something we’ve indicated that our function needs to properly run.
- In our case, we need that argument as the second array for our conditional on line 12 to evaluate.
—
Let’s take a look at our updated hooks!
As the title of this post indicates, we’re now going to make sure our Cart has a specific Product ID (or IDs) in it before we display our awesome content.
As I mentioned just above this image, we need to define our variable “$ids” and then pass that into the function we created up above.
- On line 6, we create an array and place the desired Product IDs into it.
- In this case, we’re looking for id 20.
- If we wanted to check for multiple IDs, we can just add more like this: array(20, 12, 33). You just need to separate them with commas.
- You will notice that since these are numbers, we don’t need the quotes surrounding them like we did with the categories. That’s simply because the categories are “strings” and we need to clarify them with quotes.
- On line 7, we call up the function we created above and pass the argument of $ids into it.
- So now, when the function runs, it will use our variable of $ids as the argument in the array_intersect on line 12 in the first image of the code.
Again we can read this as:
- If there is a product with the ID of 20 in our shopping cart, echo our content.
- If not, do nothing.
///
Watch the training video ► How to Add Conditional Content to the Top of WooCommerce Cart and Checkout Pages – by Product ID
In case you’re more of a watch-er than a read-er, I put all the stuff above into the video below!
—
Thanks for reading our WooCommerce Cart and Checkout Pages series! If you want to check out the whole series again, head on over to part 1.
Want to learn even more WordPress?
Check out our WordPress Training page!
. . .