Develo Design

Develo Design Menu

Magento 2 – Displaying Custom Product Attributes in the Storefront

We recently had to create a custom product attribute to allow a client to set a “Best Seller” flag on certain products. I found a lot of outdated examples of how to access this attribute on the front end, so for others out there; here is how I did this.

Firstly, create the attribute in the Magento Admin via Stores > Attributes > Product

Create New Magento Product Attribute Button Position Top Right

And set the values as below.

Product Attribute Property Values. Name - Best Seller, Input type - Yes/No, Required - No

Next, in the left hand tabs select Storefront Properties and make sure Visible on Catalog Pages on Storefront and Used in product Listing are set to Yes.

Once saved. Go to Stores > Attributes > Attribute Set. Select the Attribute Set you wish to add this to ( in my case Default ) and drag your attribute ( you may need to scroll down to find this ) from the right column to the left. With this done, everything is now in place for this to be used on product and category pages.

To pull this data through to the template, I used the following:

$_product->getBestSeller()

( notice this is the attribute code in upper camel case, which can be found in the Product Attribute grid where it was created )

In our case we used this to display flags on products to give customers a quick heads-up.

Best Seller Flag

To find out how we went about creating these flags look out for the next post coming soon…

Share

Loading Disqus Comments ...
Loading Facebook Comments ...

Post Comment

Your email address will not be published. Required fields are marked *

Discuss My Project

We want to hear about your project! Provide us with a few details and we’ll get back to you for a chat.