Skip to main content
81 votes
Accepted

Get product attribute in cart in Magento2

There is no necessity to change any PHP code for doing this. You just need to create {MODULE_NAME}/etc/catalog_attributes.xml with such content: <?xml version="1.0"?> <config xmlns:xsi="...
Igor Sydorenko's user avatar
46 votes
Accepted

Magento 2 : Get Custom Attribute Value of Product

To fetch value of attribute Use: First get Your Product Collection and then $this->getProduct()->getAttributeText('attribute_text'); e.g: $this->getProduct()->getAttributeText('metal_pads'...
Sameer Bhayani's user avatar
34 votes

addFieldToFilter() with OR Condition in magento2

I assume that in your example, yellow, small and china are options in dropdowns. You will need their ids first, but let's assume the are $yellowId, $smallId and $chinaId. You can try this filtering....
Marius's user avatar
  • 198k
29 votes
Accepted

Magento 2: what is the catalog_attributes.xml file?

In general, these files contain a list of attributes that serve different purposes. The group used_in_autogeneration from the file in catalog module serves for listing attributes that have the value ...
Marius's user avatar
  • 198k
27 votes

Magento 2: Delete magento product attributes?

If you do not see the delete attribute button then that means the table column 'is_user_defined' in the eav_attribute table is set to 0. By changing this to 1 it will show the delete button. If you ...
danmentzer's user avatar
25 votes
Accepted

Magento 2: How to display custom attribute in catalog?

We can get custom attribute value like below Attribute Value <?php /* @escapeNotVerified */ echo $_product->getResource()->getAttribute('c_address')->getFrontend()->getValue($_product)...
Bilal Usean's user avatar
24 votes

Magento 2 - How to get attribute of product?

Best practice in magento is to do it via xml. To get a standard attribute you do something like this in catalog_product_view.xml for example: <referenceContainer name="product.info.main"> &...
Claudiu Creanga's user avatar
24 votes
Accepted

Magento 2 - How to get attribute of product?

Another way, for the custom attributes: we can simply get the value by using getCustomAttribute() if (null !== $product->getCustomAttribute('your_custom_attribute')) { echo $product->...
Khoa TruongDinh's user avatar
24 votes
Accepted

Best way to update product's attribute value

I have used method updateAttributes of class \Magento\Catalog\Model\ResourceModel\Product\Action. Method can update multiple products and attribute values, but if you want to update just one product ...
Bartosz Kubicki's user avatar
21 votes

Magento2 : Create multi select category attribute with custom source

For a mult select attribute, you can need to add below at your installer: source model: [VendorName]\[ModuleName]\Model\Category\Attribute\Source\Custom.php backend Model [VendorName]\[ModuleName]\...
Amit Bera's user avatar
  • 77.6k
20 votes
Accepted

Magento 2: get attribute label (considering current store view)

Use getStoreLabel() ,instead of getFrontendLabel()
Amit Bera's user avatar
  • 77.6k
15 votes
Accepted

Change order of configuration options in customer product view

The position of the attributes of Configurable Product are stored at catalog_product_super_attribute Use SQL to update position in PDP 192 = Id attribute Size 193 = Id Attribute Color update ...
HoangHieu's user avatar
  • 1,358
14 votes

Magento 2: How to display custom attribute in catalog?

I created an article about this, that have only 5 steps to do it: 1 - Create your attribute in Stores > Attributes > Product. 2 - Set the attribute in the Default group, go to Stores > Attributes > ...
Rafael Corrêa Gomes's user avatar
14 votes
Accepted

Configurable product attribute sort order in Dropdown 2.12

I faced the same attribute options sorting issue at front-end, As I checked this issue and found that while fetching the attribute options, there is no sorting filter added in the query by default in ...
Jagdish Ram's user avatar
14 votes

addFieldToFilter() with OR Condition in magento2

Try this, It work for me $collection->addAttributeToFilter([ ['attribute' => 'attribute1','null' => true ], ['attribute' => 'attribute1','gteq' => 1 ] ]); // Don't ...
Châu Văn Khánh Nhật's user avatar
14 votes
Accepted

Magento2 custom attribute for specific product type

Yes , You can create specific attribute for specific product type programmatically. Below i have created Boolean type of attribute for configurable product only. You need to pass 'apply_to' ...
Manthan Dave's user avatar
  • 9,856
13 votes

Magento 2: new attribute not showing in list configurable product

If you want to use an Attribute for Configurations, you have to make sure, that it has the Scope Global. That is (I think) like this, because it does not make sense to override a configuration ...
David Verholen's user avatar
13 votes
Accepted

"Undefined Index Notice" in EavAttribute.php when adding a swatch option

Looks like problem in server settings. Please try increase max_input_vars setting to 100000 and try reproduce problem.
KAndy's user avatar
  • 20.9k
13 votes
Accepted

"discount_tax_compensation_amount" attribute in Sales Order Item (Magento 2)

It is used by Magento to calculate the amount of tax that is not originally calculated when a product that has a tax is under discount. For example. Let’s suppose you have a product named “Pencil”. ...
Kavitha mano's user avatar
13 votes
Accepted

how to save image custom attribute in magento 2

In your observer there is a lot of unnecessary code. You can change it like: <?php namespace Dcw\Vm\Observer; use Magento\Framework\Event\ObserverInterface; class ProductSaveAfter implements ...
Siarhey Uchukhlebau's user avatar
13 votes
Accepted

Magento 2 : How to know "used_in_forms" param of eav attribute for a form?

You can get all used_in_forms in magento by typing below query, Used in form customer attribute: SELECT DISTINCT form_code FROM customer_form_attribute WHERE 1 adminhtml_checkout ...
Rakesh Jesadiya's user avatar
12 votes

Get Option Value by Id of Attribute in Magento

Simply put - use the getAttributeText method. $product->getAttributeText('brand')
PromInc's user avatar
  • 460
12 votes

Magento 2 How to uninstall attributes added by custom module?

In a module, you would use the following code that utilizes dependency injection for uninstallation. It works equally well anywhere else, just be sure to inject the EavSetupFactory into the ...
CarComp's user avatar
  • 1,256
12 votes
Accepted

magento 2 Get attribute option label by attribute option Id?

You need to add following in your __construct method protected $optionFactory; protected $_attributeOptionCollection; public function __construct( \Magento\Eav\Api\Data\...
Chander Shekhar's user avatar
11 votes

Magento 2: what is the catalog_attributes.xml file?

Just yesterday I stumbled upon that for the first time. It's used for example to add custom attributes to quote item products, otherwise they won't be loaded to save resources (in my case I wanted to ...
fmrng's user avatar
  • 3,398
11 votes

Magento 2 - Add custom attribute in customer registration form

If your install data script successfully installed your custom attribute, now you just need to override addition information phtml file and set your custom attribute in that. Create Vendor/TestModule/...
Janak Bhimani's user avatar
11 votes

Magento 2 how to add product attribute of type textarea with editor programmatically

I have use this script to add custom attribute in products. It's working fine for me. /** @var EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); /** ...
Dharmendra Jadav's user avatar
11 votes
Accepted

Magento 2 : How to Get all User Defined Attributes in Product Model

We should use Service Contracts Layer. /** * @var \Magento\Framework\Api\SearchCriteriaBuilder */ protected $searchCriteriaBuilder; /** * @var \Magento\Eav\Api\AttributeRepositoryInterface */ ...
Khoa TruongDinh's user avatar
11 votes
Accepted

SQL Query for custom attribute

Magento uses the Eav structure to save the product attribute value. First, you have to find out your attribute type of upc.So, you have to open eav_attribute and run the below for getting backend_type]...
Amit Bera's user avatar
  • 77.6k
10 votes
Accepted

How can I add a custom block to the products page in Magento 2?

In app\code\Namespace\Modulename\view\frontend\layout\catalog_product_view.xml add like below code. You can use your required referenceContainer <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-...
Ajay's user avatar
  • 919

Only top scored, non community-wiki answers of a minimum length are eligible