Magento 2 implements the service contracts design pattern – a set of PHP interfaces that are defined for a module. A service contract includes service and data interfaces, which hide business logic details from service requestors such as controllers, web services, and other modules.
Quite often Magento developers face a problem with accessing custom attributes during product collection loading. For example, when you try to get a product from a quote or a wishlist item. As the result, we’ve noticed some interesting workarounds to bypass the problem and to access the attribute value at any price, like repetitive product loading etc. However, there is a proper way to make these attributes accessible. We would like to tell more about it.
In this post we want to share our expertise on how to create a category attribute in Magento 2. As you may have noticed, we need to change (add or customize) category attributes from time to time. We have done it many times for Magento 1 and now we faced the same task in Magento 2. This process is very similar for both Magento versions, however there are some differences. So let’s check how we can do it step by step.
Upon creating an extension for Magento, quite often you face with a necessity to create a custom attribute for product, category, customer etc. Of course, you can add a description somewhere in the extension’s documentation how to do it and do not bother you head. However, all Magento versions allow you to create an attribute on the fly during an extension’s installation. There were many topics how to do that in Magento 1.x, and let’s check what is the difference in the next major version of Magento.
In this article we would like to share with the community one of the ways to display a custom CMS attribute on the Magento frontend. In my previous article I have explained how to add a CMS attribute at the backend. Check it out here (Adding a custom attribute to the CMS page).
So you have added a new custom product attribute. It is not a problem to access and use it in Product View. And you probably know that you should enable it under the attribute settings page of Magento backend in order to make it available for the Product List (or category view) section. But what about checkout/cart area?
Getting all options of a specific attribute in Magento is fairly easy:
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color'); foreach ($attribute->getSource()->getAllOptions(true) as $option) { echo $option['value'] . ' ' . $option['label'] . "\n"; }