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
And set the values as below.
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.
To find out how we went about creating these flags look out for the next post coming soon…