12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Jun 29, 2022

Conditional Related Products in Oxygen

Sridhar Katakam

When a Product Builder component is used in the single WooCommerce product Template in Oxygen, it (technically, WooCommerce) will ensure that the related products element is not output when there are no related products out of the box.

There could be times when you want to set up related products yourself using Oxygen's Repeater or Easy Posts by pulling x number of products from the database that are in the same product category/categories as the current product whilst excluding the current product.

With WP Query set to advanced mode, one of the available presets is "Related posts by terms". After this is selected and product_cat chosen, it will build the query like this:

posts_per_page and no_found_rows were added

Typically you will have the Easy Posts/Repeater with a Heading above it inside a Section.

We do not want the "Related Products" heading text (essentially, the entire Section) to be output when there are no related products.

This members-only tutorial shows how this can be achieved by writing a custom query with the same query parameters as above, getting the count of the query results and checking if that is at least one.

Step 1

Create a code snippet using a plugin like WPCodeBox.

Title: [Function] Get the number of related products by product category

Code:

This is a premium members-only content.

To view the rest of the content, please sign up for membership ($47/month or $599 one-time).

Already a member? Log in below or here.

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right