Magento 2 Product Attributes and Conditional Statements

Pretty much every project I work on usually takes advantage of PHP’s Conditional Statements to show or hide content on the front end.

Conditional statements work very well when combined with Magento’s product attributes. Magento’s product attributes are so flexible that they can be used in a number of different ways.
Here are a few examples of implementing what I call conditional logic in Magento 2-

Yes/No Switch

For example, if I have discontinued products in the store, I want people to see them differently than products that are not discontinued.
By creating a Yes/No attribute called is_discontinued, I can hide or show content only for those discontinued products.
You can see an example here Yes No (85)

Another good thing about using Yes/No conditional logic is that you can also use it inside of front end templates to do things like hide add to cart buttons or inventory. Just make sure to do this within your custom theme so you don’t change any of default theme files.

Display Content Selectively

Magento’s Dropdown attributes can be used to show different content based on the value of a specific attribute. For example, variety.
This allows us to show different content based on the value of the variety.
This is very handy for showing things like manufacture’s color charts, size charts and other things.

You can also use it to just output the value of the attribute with some styling, which is still pretty handy.
You can see an example here Dropdown (91)

As you can see from the examples, conditional statements are pretty handy for customizing the product detail page.

Good luck.

This entry was posted in Magento 2. Bookmark the permalink.