Experiments
We use quote_item as an example.
We create a custom product attribute with product_brand
code.
$productResp = $this->_objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface');
$product = $productResp->getById(1);
var_dump($product->getData());
Ok, we can get product_brand
attribute value.
$checkoutSession = $this->_objectManager->get('\Magento\Checkout\Model\Session');
$quote = $checkoutSession->getQuote();
$quoteItems = $quote->getAllItems();
foreach ($quoteItems as $item) {
$product = $item->getProduct();
}
var_dump($product->getData());
However, we can’t get product_brand
attribute value through qutoe item.
Let’s create a etc/catalog_attributes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
<group name="quote_item">
<attribute name="product_brand"/>
</group>
<group name="wishlist_item">
<attribute name="product_brand" />
</group>
</config>
This time, we can get product_brand
attribute value through qutoe item.
Source: vendor\magento\module-quote\Model\ResourceModel\Quote\Item\Collection.php::_assignProducts
...
$productCollection = $this->_productCollectionFactory->create()->setStoreId(
$this->getStoreId()
)->addIdFilter(
$this->_productIds
)->addAttributeToSelect(
$this->_quoteConfig->getProductAttributes()
);
...
$this->_quoteConfig
is an instance of Magento\Quote\Model\Quote\Config
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Quote\Model\Quote;
class Config
{
/**
* @var \Magento\Catalog\Model\Attribute\Config
*/
private $_attributeConfig;
/**
* @param \Magento\Catalog\Model\Attribute\Config $attributeConfig
*/
public function __construct(\Magento\Catalog\Model\Attribute\Config $attributeConfig)
{
$this->_attributeConfig = $attributeConfig;
}
/**
* @return array
*/
public function getProductAttributes()
{
return $this->_attributeConfig->getAttributeNames('quote_item');
}
}
Default Groups:
quote_item
wishlist_item
catalog_product
catalog_category
unassignable
used_in_autogeneration