0

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?

1

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.

| improve this answer | |
  • I can't believe I hadn't tried this, but getData did show it. Thanks! – Alex Timmer Nov 15 '19 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.