Magento2; how to call attributes for upsell products product/list/items.phtml
-
20-03-2021 - |
Question
I need to call some product attributes in the Upsell Products view for Magento 2. This would be in the Magento_Catalog/templates/product/list/items.phtml
This is said case;
case 'upsell':
/** @var \Magento\Catalog\Block\Product\ProductList\Upsell $block */
if ($exist = count($block->getItemCollection()->getItems())) {
$type = 'upsell';
$class = $type;
$image = 'upsell_products_list';
$title = __('Best alternatives');
$items = $block->getItemCollection()->getItems();
$limit = $block->getItemLimit('upsell');
$shuffle = 0;
$showAddTo = false;
$showCart = true;
$templateType = null;
$description = true;
$canItemsAddToCart = true;
}
break;
And in the template I can call the product name, image, sku like this;
<?= $block->escapeHtml($_item->getName()) ?>
<?= $block->escapeHtml($_item->getSku()) ?>
That works fine, but this doesn't;
<?= $block->escapeHtml($_item->getAttribute('mycustomattribute')); ?>
This attribute is visible on frontend and all that, but it doesn't echo anything in this block. How can I fix that?
Solution
try below code.
$_item->getData("Your_attribute_code");
or
$_item->getYourAttributeCode();
if you still not get the attribute value then got to
Stores -> Attributes
and edit your attribute and set Used in Product Listing to Yes
under Storefront Properties
Hope this will help you.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange