How can I add an attribute to related products?Display new attribute in related ProductsAdd to cart button in related products leads to some view source pageRelated products select button missing and Size attribute in related productshow to add default products to related products listAttribute code in related productsAutomatically set inverse related productsrelated products not showingEmpty Related Products collectionRestore Magento 2 attribute and related productsGet Configurable Product options, Price And Add to cart Button On Related Product Section

Can the Witch Sight warlock invocation see through the Mirror Image spell?

Why does a car's steering wheel get lighter with increasing speed

How can I have x-axis ticks that show ticks scaled in powers of ten?

How can I portion out frozen cookie dough?

Cycles on the torus

What do you call someone who likes to pick fights?

3.5% Interest Student Loan or use all of my savings on Tuition?

An Undercover Army

Should I file my taxes? No income, unemployed, but paid 2k in student loan interest

What is the orbit and expected lifetime of Crew Dragon trunk?

Does an unused member variable take up memory?

What is the purpose of a disclaimer like "this is not legal advice"?

Why aren't there more Gauls like Obelix?

Insult for someone who "doesn't know anything"

Use Mercury as quenching liquid for swords?

A running toilet that stops itself

Is divide-by-zero a security vulnerability?

Why would /etc/passwd be used every time someone executes `ls -l` command?

Will the concrete slab in a partially heated shed conduct a lot of heat to the unconditioned area?

Was it really inappropriate to write a pull request for the company I interviewed with?

What should I do when a paper is published similar to my PhD thesis without citation?

Who has more? Ireland or Iceland?

Did Amazon pay $0 in taxes last year?

Create chunks from an array



How can I add an attribute to related products?


Display new attribute in related ProductsAdd to cart button in related products leads to some view source pageRelated products select button missing and Size attribute in related productshow to add default products to related products listAttribute code in related productsAutomatically set inverse related productsrelated products not showingEmpty Related Products collectionRestore Magento 2 attribute and related productsGet Configurable Product options, Price And Add to cart Button On Related Product Section













2















I would like to Add the sku to the related products. I am in the template Magento_Catalog/templates/product/list/items.phtml. This is what it looks like now-



<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

// @codingStandardsIgnoreFile

/* @var $block MagentoCatalogBlockProductAbstractProduct */
?>

<?php
switch ($type = $block->getType())

case 'related-rule':
if ($exist = $block->hasItems())
$type = 'related';
$class = $type;

$image = 'related_products_list';
$title = __('Accessories');
$items = $block->getAllItems();
$limit = $block->getPositionLimit();
$shuffle = (int) $block->isShuffled();
$canItemsAddToCart = $block->canItemsAddToCart();

$showAddTo = true;
$showCart = false;
$templateType = null;
$description = false;

break;

case 'related':
/** @var MagentoCatalogBlockProductProductListRelated $block */
if ($exist = $block->getItems()->getSize())
$type = 'related';
$class = $type;

$image = 'related_products_list';
$title = __('Related Products');
$items = $block->getItems();
$limit = 0;
$shuffle = 0;
$canItemsAddToCart = $block->canItemsAddToCart();

$showAddTo = true;
$showCart = false;
$templateType = null;
$description = false;

break;

case 'upsell-rule':
if ($exist = $block->hasItems())
$type = 'upsell';
$class = $type;

$image = 'upsell_products_list';
$title = __('We found other products you might like!');
$items = $block->getAllItems();
$limit = $block->getPositionLimit();
$shuffle = (int) $block->isShuffled();

$showAddTo = false;
$showCart = false;
$templateType = null;
$description = false;
$canItemsAddToCart = false;

break;

case 'upsell':
/** @var MagentoCatalogBlockProductProductListUpsell $block */
if ($exist = count($block->getItemCollection()->getItems()))
$type = 'upsell';
$class = $type;

$image = 'upsell_products_list';
$title = __('We found other products you might like!');
$items = $block->getItemCollection()->getItems();
$limit = $block->getItemLimit('upsell');
$shuffle = 0;

$showAddTo = false;
$showCart = false;
$templateType = null;
$description = false;
$canItemsAddToCart = false;

break;

case 'crosssell-rule':
/** @var MagentoCatalogBlockProductProductListCrosssell $block */
if ($exist = $block->hasItems())
$type = 'crosssell';
$class = $type;

$image = 'cart_cross_sell_products';
$title = __('More Choices:');
$items = $block->getItemCollection();

$showAddTo = true;
$showCart = true;
$templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
$description = false;
$canItemsAddToCart = false;

break;

case 'crosssell':
/** @var MagentoCatalogBlockProductProductListCrosssell $block */
if ($exist = count($block->getItems()))
$type = 'crosssell';
$class = $type;

$image = 'cart_cross_sell_products';
$title = __('More Choices:');
$items = $block->getItems();

$showAddTo = true;
$showCart = true;
$templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
$description = false;
$canItemsAddToCart = false;

break;

case 'new':
if ($exist = $block->getProductCollection())
$type = 'new';
$mode = 'grid';
$type = $type . ' ' . $mode;

$class = 'widget' . ' ' . $type;

$image = 'new_products_content_widget_grid';
$title = __('New Products');
$items = $exist;

$showAddTo = true;
$showCart = true;
$templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
$description = ($mode == 'list') ? true : false;
$canItemsAddToCart = false;

break;

default:
$exist = null;

?>

<?php if ($exist):?>

<?php if ($type == 'related' || $type == 'upsell'): ?>
<?php if ($type == 'related'): ?>
<div class="block <?= /* @escapeNotVerified */ $class ?>" data-mage-init='"relatedProducts":"relatedCheckbox":".related.checkbox"' data-limit="<?= /* @escapeNotVerified */ $limit ?>" data-shuffle="<?= /* @escapeNotVerified */ $shuffle ?>">
<?php else: ?>
<div class="block <?= /* @escapeNotVerified */ $class ?>" data-mage-init='"upsellProducts":' data-limit="<?= /* @escapeNotVerified */ $limit ?>" data-shuffle="<?= /* @escapeNotVerified */ $shuffle ?>">
<?php endif; ?>
<?php else: ?>
<div class="block <?= /* @escapeNotVerified */ $class ?>">
<?php endif; ?>
<div class="block-title title">
<strong id="block-<?= /* @escapeNotVerified */ $class ?>-heading" role="heading" aria-level="2"><?= /* @escapeNotVerified */ $title ?></strong>
</div>
<div class="block-content content" aria-labelledby="block-<?= /* @escapeNotVerified */ $class ?>-heading">
<?php if ($type == 'related' && $canItemsAddToCart): ?>
<div class="block-actions">
<?= /* @escapeNotVerified */ __('Check items to add to the cart or') ?>
<button type="button" class="action select" role="select-all"><span id="selectall"><?= /* @escapeNotVerified */ __('select all') ?></span></button>
</div>
<?php endif; ?>
<div class="products wrapper grid products-grid products-<?= /* @escapeNotVerified */ $type ?>">
<ol class="products list items product-items">
<?php $iterator = 1; ?>
<?php foreach ($items as $_item): ?>
<?php $available = ''; ?>
<?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
<?php if (!$_item->getRequiredOptions()): ?>
<?php $available = 'related-available'; ?>
<?php endif; ?>
<?php endif; ?>
<?php if ($type == 'related' || $type == 'upsell'): ?>
<?= /* @escapeNotVerified */ ($iterator++ == 1) ? '<li class="item product product-item" style="display: none;">' : '</li><li class="item product product-item" style="display: none;">' ?>
<?php else: ?>
<?= /* @escapeNotVerified */ ($iterator++ == 1) ? '<li class="item product product-item">' : '</li><li class="item product product-item">' ?>
<?php endif; ?>
<div class="product-item-info <?= /* @escapeNotVerified */ $available ?>">
<?= /* @escapeNotVerified */ '<!-- ' . $image . '-->' ?>
<a href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>" class="product photo product-item-photo">
<?= $block->getImage($_item, $image)->toHtml() ?>
</a>
<div class="product details product-item-details">
<strong class="product name product-item-name"><a class="product-item-link" title="<?= $block->escapeHtml($_item->getName()) ?>" href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>">
<?= $block->escapeHtml($_item->getName()) ?></a>
</strong>

<?= /* @escapeNotVerified */ $block->getProductPrice($_item) ?>

<?php if ($templateType): ?>
<?= $block->getReviewsSummaryHtml($_item, $templateType) ?>
<?php endif; ?>

<?php if ($canItemsAddToCart && !$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
<?php if (!$_item->getRequiredOptions()): ?>
<div class="field choice related">
<input type="checkbox" class="checkbox related" id="related-checkbox<?= /* @escapeNotVerified */ $_item->getId() ?>" name="related_products[]" value="<?= /* @escapeNotVerified */ $_item->getId() ?>" />
<label class="label" for="related-checkbox<?= /* @escapeNotVerified */ $_item->getId() ?>"><span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span></label>
</div>
<?php endif; ?>
<?php endif; ?>

<?php if ($showAddTo || $showCart): ?>
<div class="product actions product-item-actions">
<?php if ($showCart): ?>
<div class="actions-primary">
<?php if ($_item->isSaleable()): ?>
<?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)): ?>
<button class="action tocart primary" data-mage-init='"redirectUrl": "url": "<?= /* @escapeNotVerified */ $block->getAddToCartUrl($_item) ?>"' type="button" title="<?= /* @escapeNotVerified */ __('Add to Cart') ?>">
<span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
</button>
<?php else: ?>
<?php $postDataHelper = $this->helper('MagentoFrameworkDataHelperPostHelper');
$postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()])
?>
<button class="action tocart primary"
data-post='<?= /* @escapeNotVerified */ $postData ?>'
type="button" title="<?= /* @escapeNotVerified */ __('Add to Cart') ?>">
<span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
</button>
<?php endif; ?>
<?php else: ?>
<?php if ($_item->getIsSalable()): ?>
<div class="stock available"><span><?= /* @escapeNotVerified */ __('In stock') ?></span></div>
<?php else: ?>
<div class="stock unavailable"><span><?= /* @escapeNotVerified */ __('Out of stock') ?></span></div>
<?php endif; ?>
<?php endif; ?>
</div>
<?php endif; ?>

<?php if ($showAddTo): ?>
<div class="secondary-addto-links actions-secondary" data-role="add-to-links">
<?php if ($addToBlock = $block->getChildBlock('addto')): ?>
<?= $addToBlock->setProduct($_item)->getChildHtml() ?>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</div>
<?= ($iterator == count($items)+1) ? '</li>' : '' ?>
<?php endforeach ?>
</ol>
</div>
</div>






I would like this to be inbetween the name and price so I add this line-



 <div><?= $block->escapeHtml($_item->getSku()) ?></div>


It doesn't work. How can I add sku to my related products?










share|improve this question




























    2















    I would like to Add the sku to the related products. I am in the template Magento_Catalog/templates/product/list/items.phtml. This is what it looks like now-



    <?php
    /**
    * Copyright © Magento, Inc. All rights reserved.
    * See COPYING.txt for license details.
    */

    // @codingStandardsIgnoreFile

    /* @var $block MagentoCatalogBlockProductAbstractProduct */
    ?>

    <?php
    switch ($type = $block->getType())

    case 'related-rule':
    if ($exist = $block->hasItems())
    $type = 'related';
    $class = $type;

    $image = 'related_products_list';
    $title = __('Accessories');
    $items = $block->getAllItems();
    $limit = $block->getPositionLimit();
    $shuffle = (int) $block->isShuffled();
    $canItemsAddToCart = $block->canItemsAddToCart();

    $showAddTo = true;
    $showCart = false;
    $templateType = null;
    $description = false;

    break;

    case 'related':
    /** @var MagentoCatalogBlockProductProductListRelated $block */
    if ($exist = $block->getItems()->getSize())
    $type = 'related';
    $class = $type;

    $image = 'related_products_list';
    $title = __('Related Products');
    $items = $block->getItems();
    $limit = 0;
    $shuffle = 0;
    $canItemsAddToCart = $block->canItemsAddToCart();

    $showAddTo = true;
    $showCart = false;
    $templateType = null;
    $description = false;

    break;

    case 'upsell-rule':
    if ($exist = $block->hasItems())
    $type = 'upsell';
    $class = $type;

    $image = 'upsell_products_list';
    $title = __('We found other products you might like!');
    $items = $block->getAllItems();
    $limit = $block->getPositionLimit();
    $shuffle = (int) $block->isShuffled();

    $showAddTo = false;
    $showCart = false;
    $templateType = null;
    $description = false;
    $canItemsAddToCart = false;

    break;

    case 'upsell':
    /** @var MagentoCatalogBlockProductProductListUpsell $block */
    if ($exist = count($block->getItemCollection()->getItems()))
    $type = 'upsell';
    $class = $type;

    $image = 'upsell_products_list';
    $title = __('We found other products you might like!');
    $items = $block->getItemCollection()->getItems();
    $limit = $block->getItemLimit('upsell');
    $shuffle = 0;

    $showAddTo = false;
    $showCart = false;
    $templateType = null;
    $description = false;
    $canItemsAddToCart = false;

    break;

    case 'crosssell-rule':
    /** @var MagentoCatalogBlockProductProductListCrosssell $block */
    if ($exist = $block->hasItems())
    $type = 'crosssell';
    $class = $type;

    $image = 'cart_cross_sell_products';
    $title = __('More Choices:');
    $items = $block->getItemCollection();

    $showAddTo = true;
    $showCart = true;
    $templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
    $description = false;
    $canItemsAddToCart = false;

    break;

    case 'crosssell':
    /** @var MagentoCatalogBlockProductProductListCrosssell $block */
    if ($exist = count($block->getItems()))
    $type = 'crosssell';
    $class = $type;

    $image = 'cart_cross_sell_products';
    $title = __('More Choices:');
    $items = $block->getItems();

    $showAddTo = true;
    $showCart = true;
    $templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
    $description = false;
    $canItemsAddToCart = false;

    break;

    case 'new':
    if ($exist = $block->getProductCollection())
    $type = 'new';
    $mode = 'grid';
    $type = $type . ' ' . $mode;

    $class = 'widget' . ' ' . $type;

    $image = 'new_products_content_widget_grid';
    $title = __('New Products');
    $items = $exist;

    $showAddTo = true;
    $showCart = true;
    $templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
    $description = ($mode == 'list') ? true : false;
    $canItemsAddToCart = false;

    break;

    default:
    $exist = null;

    ?>

    <?php if ($exist):?>

    <?php if ($type == 'related' || $type == 'upsell'): ?>
    <?php if ($type == 'related'): ?>
    <div class="block <?= /* @escapeNotVerified */ $class ?>" data-mage-init='"relatedProducts":"relatedCheckbox":".related.checkbox"' data-limit="<?= /* @escapeNotVerified */ $limit ?>" data-shuffle="<?= /* @escapeNotVerified */ $shuffle ?>">
    <?php else: ?>
    <div class="block <?= /* @escapeNotVerified */ $class ?>" data-mage-init='"upsellProducts":' data-limit="<?= /* @escapeNotVerified */ $limit ?>" data-shuffle="<?= /* @escapeNotVerified */ $shuffle ?>">
    <?php endif; ?>
    <?php else: ?>
    <div class="block <?= /* @escapeNotVerified */ $class ?>">
    <?php endif; ?>
    <div class="block-title title">
    <strong id="block-<?= /* @escapeNotVerified */ $class ?>-heading" role="heading" aria-level="2"><?= /* @escapeNotVerified */ $title ?></strong>
    </div>
    <div class="block-content content" aria-labelledby="block-<?= /* @escapeNotVerified */ $class ?>-heading">
    <?php if ($type == 'related' && $canItemsAddToCart): ?>
    <div class="block-actions">
    <?= /* @escapeNotVerified */ __('Check items to add to the cart or') ?>
    <button type="button" class="action select" role="select-all"><span id="selectall"><?= /* @escapeNotVerified */ __('select all') ?></span></button>
    </div>
    <?php endif; ?>
    <div class="products wrapper grid products-grid products-<?= /* @escapeNotVerified */ $type ?>">
    <ol class="products list items product-items">
    <?php $iterator = 1; ?>
    <?php foreach ($items as $_item): ?>
    <?php $available = ''; ?>
    <?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
    <?php if (!$_item->getRequiredOptions()): ?>
    <?php $available = 'related-available'; ?>
    <?php endif; ?>
    <?php endif; ?>
    <?php if ($type == 'related' || $type == 'upsell'): ?>
    <?= /* @escapeNotVerified */ ($iterator++ == 1) ? '<li class="item product product-item" style="display: none;">' : '</li><li class="item product product-item" style="display: none;">' ?>
    <?php else: ?>
    <?= /* @escapeNotVerified */ ($iterator++ == 1) ? '<li class="item product product-item">' : '</li><li class="item product product-item">' ?>
    <?php endif; ?>
    <div class="product-item-info <?= /* @escapeNotVerified */ $available ?>">
    <?= /* @escapeNotVerified */ '<!-- ' . $image . '-->' ?>
    <a href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>" class="product photo product-item-photo">
    <?= $block->getImage($_item, $image)->toHtml() ?>
    </a>
    <div class="product details product-item-details">
    <strong class="product name product-item-name"><a class="product-item-link" title="<?= $block->escapeHtml($_item->getName()) ?>" href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>">
    <?= $block->escapeHtml($_item->getName()) ?></a>
    </strong>

    <?= /* @escapeNotVerified */ $block->getProductPrice($_item) ?>

    <?php if ($templateType): ?>
    <?= $block->getReviewsSummaryHtml($_item, $templateType) ?>
    <?php endif; ?>

    <?php if ($canItemsAddToCart && !$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
    <?php if (!$_item->getRequiredOptions()): ?>
    <div class="field choice related">
    <input type="checkbox" class="checkbox related" id="related-checkbox<?= /* @escapeNotVerified */ $_item->getId() ?>" name="related_products[]" value="<?= /* @escapeNotVerified */ $_item->getId() ?>" />
    <label class="label" for="related-checkbox<?= /* @escapeNotVerified */ $_item->getId() ?>"><span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span></label>
    </div>
    <?php endif; ?>
    <?php endif; ?>

    <?php if ($showAddTo || $showCart): ?>
    <div class="product actions product-item-actions">
    <?php if ($showCart): ?>
    <div class="actions-primary">
    <?php if ($_item->isSaleable()): ?>
    <?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)): ?>
    <button class="action tocart primary" data-mage-init='"redirectUrl": "url": "<?= /* @escapeNotVerified */ $block->getAddToCartUrl($_item) ?>"' type="button" title="<?= /* @escapeNotVerified */ __('Add to Cart') ?>">
    <span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
    </button>
    <?php else: ?>
    <?php $postDataHelper = $this->helper('MagentoFrameworkDataHelperPostHelper');
    $postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()])
    ?>
    <button class="action tocart primary"
    data-post='<?= /* @escapeNotVerified */ $postData ?>'
    type="button" title="<?= /* @escapeNotVerified */ __('Add to Cart') ?>">
    <span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
    </button>
    <?php endif; ?>
    <?php else: ?>
    <?php if ($_item->getIsSalable()): ?>
    <div class="stock available"><span><?= /* @escapeNotVerified */ __('In stock') ?></span></div>
    <?php else: ?>
    <div class="stock unavailable"><span><?= /* @escapeNotVerified */ __('Out of stock') ?></span></div>
    <?php endif; ?>
    <?php endif; ?>
    </div>
    <?php endif; ?>

    <?php if ($showAddTo): ?>
    <div class="secondary-addto-links actions-secondary" data-role="add-to-links">
    <?php if ($addToBlock = $block->getChildBlock('addto')): ?>
    <?= $addToBlock->setProduct($_item)->getChildHtml() ?>
    <?php endif; ?>
    </div>
    <?php endif; ?>
    </div>
    <?php endif; ?>
    </div>
    </div>
    <?= ($iterator == count($items)+1) ? '</li>' : '' ?>
    <?php endforeach ?>
    </ol>
    </div>
    </div>






    I would like this to be inbetween the name and price so I add this line-



     <div><?= $block->escapeHtml($_item->getSku()) ?></div>


    It doesn't work. How can I add sku to my related products?










    share|improve this question


























      2












      2








      2








      I would like to Add the sku to the related products. I am in the template Magento_Catalog/templates/product/list/items.phtml. This is what it looks like now-



      <?php
      /**
      * Copyright © Magento, Inc. All rights reserved.
      * See COPYING.txt for license details.
      */

      // @codingStandardsIgnoreFile

      /* @var $block MagentoCatalogBlockProductAbstractProduct */
      ?>

      <?php
      switch ($type = $block->getType())

      case 'related-rule':
      if ($exist = $block->hasItems())
      $type = 'related';
      $class = $type;

      $image = 'related_products_list';
      $title = __('Accessories');
      $items = $block->getAllItems();
      $limit = $block->getPositionLimit();
      $shuffle = (int) $block->isShuffled();
      $canItemsAddToCart = $block->canItemsAddToCart();

      $showAddTo = true;
      $showCart = false;
      $templateType = null;
      $description = false;

      break;

      case 'related':
      /** @var MagentoCatalogBlockProductProductListRelated $block */
      if ($exist = $block->getItems()->getSize())
      $type = 'related';
      $class = $type;

      $image = 'related_products_list';
      $title = __('Related Products');
      $items = $block->getItems();
      $limit = 0;
      $shuffle = 0;
      $canItemsAddToCart = $block->canItemsAddToCart();

      $showAddTo = true;
      $showCart = false;
      $templateType = null;
      $description = false;

      break;

      case 'upsell-rule':
      if ($exist = $block->hasItems())
      $type = 'upsell';
      $class = $type;

      $image = 'upsell_products_list';
      $title = __('We found other products you might like!');
      $items = $block->getAllItems();
      $limit = $block->getPositionLimit();
      $shuffle = (int) $block->isShuffled();

      $showAddTo = false;
      $showCart = false;
      $templateType = null;
      $description = false;
      $canItemsAddToCart = false;

      break;

      case 'upsell':
      /** @var MagentoCatalogBlockProductProductListUpsell $block */
      if ($exist = count($block->getItemCollection()->getItems()))
      $type = 'upsell';
      $class = $type;

      $image = 'upsell_products_list';
      $title = __('We found other products you might like!');
      $items = $block->getItemCollection()->getItems();
      $limit = $block->getItemLimit('upsell');
      $shuffle = 0;

      $showAddTo = false;
      $showCart = false;
      $templateType = null;
      $description = false;
      $canItemsAddToCart = false;

      break;

      case 'crosssell-rule':
      /** @var MagentoCatalogBlockProductProductListCrosssell $block */
      if ($exist = $block->hasItems())
      $type = 'crosssell';
      $class = $type;

      $image = 'cart_cross_sell_products';
      $title = __('More Choices:');
      $items = $block->getItemCollection();

      $showAddTo = true;
      $showCart = true;
      $templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
      $description = false;
      $canItemsAddToCart = false;

      break;

      case 'crosssell':
      /** @var MagentoCatalogBlockProductProductListCrosssell $block */
      if ($exist = count($block->getItems()))
      $type = 'crosssell';
      $class = $type;

      $image = 'cart_cross_sell_products';
      $title = __('More Choices:');
      $items = $block->getItems();

      $showAddTo = true;
      $showCart = true;
      $templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
      $description = false;
      $canItemsAddToCart = false;

      break;

      case 'new':
      if ($exist = $block->getProductCollection())
      $type = 'new';
      $mode = 'grid';
      $type = $type . ' ' . $mode;

      $class = 'widget' . ' ' . $type;

      $image = 'new_products_content_widget_grid';
      $title = __('New Products');
      $items = $exist;

      $showAddTo = true;
      $showCart = true;
      $templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
      $description = ($mode == 'list') ? true : false;
      $canItemsAddToCart = false;

      break;

      default:
      $exist = null;

      ?>

      <?php if ($exist):?>

      <?php if ($type == 'related' || $type == 'upsell'): ?>
      <?php if ($type == 'related'): ?>
      <div class="block <?= /* @escapeNotVerified */ $class ?>" data-mage-init='"relatedProducts":"relatedCheckbox":".related.checkbox"' data-limit="<?= /* @escapeNotVerified */ $limit ?>" data-shuffle="<?= /* @escapeNotVerified */ $shuffle ?>">
      <?php else: ?>
      <div class="block <?= /* @escapeNotVerified */ $class ?>" data-mage-init='"upsellProducts":' data-limit="<?= /* @escapeNotVerified */ $limit ?>" data-shuffle="<?= /* @escapeNotVerified */ $shuffle ?>">
      <?php endif; ?>
      <?php else: ?>
      <div class="block <?= /* @escapeNotVerified */ $class ?>">
      <?php endif; ?>
      <div class="block-title title">
      <strong id="block-<?= /* @escapeNotVerified */ $class ?>-heading" role="heading" aria-level="2"><?= /* @escapeNotVerified */ $title ?></strong>
      </div>
      <div class="block-content content" aria-labelledby="block-<?= /* @escapeNotVerified */ $class ?>-heading">
      <?php if ($type == 'related' && $canItemsAddToCart): ?>
      <div class="block-actions">
      <?= /* @escapeNotVerified */ __('Check items to add to the cart or') ?>
      <button type="button" class="action select" role="select-all"><span id="selectall"><?= /* @escapeNotVerified */ __('select all') ?></span></button>
      </div>
      <?php endif; ?>
      <div class="products wrapper grid products-grid products-<?= /* @escapeNotVerified */ $type ?>">
      <ol class="products list items product-items">
      <?php $iterator = 1; ?>
      <?php foreach ($items as $_item): ?>
      <?php $available = ''; ?>
      <?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
      <?php if (!$_item->getRequiredOptions()): ?>
      <?php $available = 'related-available'; ?>
      <?php endif; ?>
      <?php endif; ?>
      <?php if ($type == 'related' || $type == 'upsell'): ?>
      <?= /* @escapeNotVerified */ ($iterator++ == 1) ? '<li class="item product product-item" style="display: none;">' : '</li><li class="item product product-item" style="display: none;">' ?>
      <?php else: ?>
      <?= /* @escapeNotVerified */ ($iterator++ == 1) ? '<li class="item product product-item">' : '</li><li class="item product product-item">' ?>
      <?php endif; ?>
      <div class="product-item-info <?= /* @escapeNotVerified */ $available ?>">
      <?= /* @escapeNotVerified */ '<!-- ' . $image . '-->' ?>
      <a href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>" class="product photo product-item-photo">
      <?= $block->getImage($_item, $image)->toHtml() ?>
      </a>
      <div class="product details product-item-details">
      <strong class="product name product-item-name"><a class="product-item-link" title="<?= $block->escapeHtml($_item->getName()) ?>" href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>">
      <?= $block->escapeHtml($_item->getName()) ?></a>
      </strong>

      <?= /* @escapeNotVerified */ $block->getProductPrice($_item) ?>

      <?php if ($templateType): ?>
      <?= $block->getReviewsSummaryHtml($_item, $templateType) ?>
      <?php endif; ?>

      <?php if ($canItemsAddToCart && !$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
      <?php if (!$_item->getRequiredOptions()): ?>
      <div class="field choice related">
      <input type="checkbox" class="checkbox related" id="related-checkbox<?= /* @escapeNotVerified */ $_item->getId() ?>" name="related_products[]" value="<?= /* @escapeNotVerified */ $_item->getId() ?>" />
      <label class="label" for="related-checkbox<?= /* @escapeNotVerified */ $_item->getId() ?>"><span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span></label>
      </div>
      <?php endif; ?>
      <?php endif; ?>

      <?php if ($showAddTo || $showCart): ?>
      <div class="product actions product-item-actions">
      <?php if ($showCart): ?>
      <div class="actions-primary">
      <?php if ($_item->isSaleable()): ?>
      <?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)): ?>
      <button class="action tocart primary" data-mage-init='"redirectUrl": "url": "<?= /* @escapeNotVerified */ $block->getAddToCartUrl($_item) ?>"' type="button" title="<?= /* @escapeNotVerified */ __('Add to Cart') ?>">
      <span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
      </button>
      <?php else: ?>
      <?php $postDataHelper = $this->helper('MagentoFrameworkDataHelperPostHelper');
      $postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()])
      ?>
      <button class="action tocart primary"
      data-post='<?= /* @escapeNotVerified */ $postData ?>'
      type="button" title="<?= /* @escapeNotVerified */ __('Add to Cart') ?>">
      <span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
      </button>
      <?php endif; ?>
      <?php else: ?>
      <?php if ($_item->getIsSalable()): ?>
      <div class="stock available"><span><?= /* @escapeNotVerified */ __('In stock') ?></span></div>
      <?php else: ?>
      <div class="stock unavailable"><span><?= /* @escapeNotVerified */ __('Out of stock') ?></span></div>
      <?php endif; ?>
      <?php endif; ?>
      </div>
      <?php endif; ?>

      <?php if ($showAddTo): ?>
      <div class="secondary-addto-links actions-secondary" data-role="add-to-links">
      <?php if ($addToBlock = $block->getChildBlock('addto')): ?>
      <?= $addToBlock->setProduct($_item)->getChildHtml() ?>
      <?php endif; ?>
      </div>
      <?php endif; ?>
      </div>
      <?php endif; ?>
      </div>
      </div>
      <?= ($iterator == count($items)+1) ? '</li>' : '' ?>
      <?php endforeach ?>
      </ol>
      </div>
      </div>






      I would like this to be inbetween the name and price so I add this line-



       <div><?= $block->escapeHtml($_item->getSku()) ?></div>


      It doesn't work. How can I add sku to my related products?










      share|improve this question
















      I would like to Add the sku to the related products. I am in the template Magento_Catalog/templates/product/list/items.phtml. This is what it looks like now-



      <?php
      /**
      * Copyright © Magento, Inc. All rights reserved.
      * See COPYING.txt for license details.
      */

      // @codingStandardsIgnoreFile

      /* @var $block MagentoCatalogBlockProductAbstractProduct */
      ?>

      <?php
      switch ($type = $block->getType())

      case 'related-rule':
      if ($exist = $block->hasItems())
      $type = 'related';
      $class = $type;

      $image = 'related_products_list';
      $title = __('Accessories');
      $items = $block->getAllItems();
      $limit = $block->getPositionLimit();
      $shuffle = (int) $block->isShuffled();
      $canItemsAddToCart = $block->canItemsAddToCart();

      $showAddTo = true;
      $showCart = false;
      $templateType = null;
      $description = false;

      break;

      case 'related':
      /** @var MagentoCatalogBlockProductProductListRelated $block */
      if ($exist = $block->getItems()->getSize())
      $type = 'related';
      $class = $type;

      $image = 'related_products_list';
      $title = __('Related Products');
      $items = $block->getItems();
      $limit = 0;
      $shuffle = 0;
      $canItemsAddToCart = $block->canItemsAddToCart();

      $showAddTo = true;
      $showCart = false;
      $templateType = null;
      $description = false;

      break;

      case 'upsell-rule':
      if ($exist = $block->hasItems())
      $type = 'upsell';
      $class = $type;

      $image = 'upsell_products_list';
      $title = __('We found other products you might like!');
      $items = $block->getAllItems();
      $limit = $block->getPositionLimit();
      $shuffle = (int) $block->isShuffled();

      $showAddTo = false;
      $showCart = false;
      $templateType = null;
      $description = false;
      $canItemsAddToCart = false;

      break;

      case 'upsell':
      /** @var MagentoCatalogBlockProductProductListUpsell $block */
      if ($exist = count($block->getItemCollection()->getItems()))
      $type = 'upsell';
      $class = $type;

      $image = 'upsell_products_list';
      $title = __('We found other products you might like!');
      $items = $block->getItemCollection()->getItems();
      $limit = $block->getItemLimit('upsell');
      $shuffle = 0;

      $showAddTo = false;
      $showCart = false;
      $templateType = null;
      $description = false;
      $canItemsAddToCart = false;

      break;

      case 'crosssell-rule':
      /** @var MagentoCatalogBlockProductProductListCrosssell $block */
      if ($exist = $block->hasItems())
      $type = 'crosssell';
      $class = $type;

      $image = 'cart_cross_sell_products';
      $title = __('More Choices:');
      $items = $block->getItemCollection();

      $showAddTo = true;
      $showCart = true;
      $templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
      $description = false;
      $canItemsAddToCart = false;

      break;

      case 'crosssell':
      /** @var MagentoCatalogBlockProductProductListCrosssell $block */
      if ($exist = count($block->getItems()))
      $type = 'crosssell';
      $class = $type;

      $image = 'cart_cross_sell_products';
      $title = __('More Choices:');
      $items = $block->getItems();

      $showAddTo = true;
      $showCart = true;
      $templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
      $description = false;
      $canItemsAddToCart = false;

      break;

      case 'new':
      if ($exist = $block->getProductCollection())
      $type = 'new';
      $mode = 'grid';
      $type = $type . ' ' . $mode;

      $class = 'widget' . ' ' . $type;

      $image = 'new_products_content_widget_grid';
      $title = __('New Products');
      $items = $exist;

      $showAddTo = true;
      $showCart = true;
      $templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
      $description = ($mode == 'list') ? true : false;
      $canItemsAddToCart = false;

      break;

      default:
      $exist = null;

      ?>

      <?php if ($exist):?>

      <?php if ($type == 'related' || $type == 'upsell'): ?>
      <?php if ($type == 'related'): ?>
      <div class="block <?= /* @escapeNotVerified */ $class ?>" data-mage-init='"relatedProducts":"relatedCheckbox":".related.checkbox"' data-limit="<?= /* @escapeNotVerified */ $limit ?>" data-shuffle="<?= /* @escapeNotVerified */ $shuffle ?>">
      <?php else: ?>
      <div class="block <?= /* @escapeNotVerified */ $class ?>" data-mage-init='"upsellProducts":' data-limit="<?= /* @escapeNotVerified */ $limit ?>" data-shuffle="<?= /* @escapeNotVerified */ $shuffle ?>">
      <?php endif; ?>
      <?php else: ?>
      <div class="block <?= /* @escapeNotVerified */ $class ?>">
      <?php endif; ?>
      <div class="block-title title">
      <strong id="block-<?= /* @escapeNotVerified */ $class ?>-heading" role="heading" aria-level="2"><?= /* @escapeNotVerified */ $title ?></strong>
      </div>
      <div class="block-content content" aria-labelledby="block-<?= /* @escapeNotVerified */ $class ?>-heading">
      <?php if ($type == 'related' && $canItemsAddToCart): ?>
      <div class="block-actions">
      <?= /* @escapeNotVerified */ __('Check items to add to the cart or') ?>
      <button type="button" class="action select" role="select-all"><span id="selectall"><?= /* @escapeNotVerified */ __('select all') ?></span></button>
      </div>
      <?php endif; ?>
      <div class="products wrapper grid products-grid products-<?= /* @escapeNotVerified */ $type ?>">
      <ol class="products list items product-items">
      <?php $iterator = 1; ?>
      <?php foreach ($items as $_item): ?>
      <?php $available = ''; ?>
      <?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
      <?php if (!$_item->getRequiredOptions()): ?>
      <?php $available = 'related-available'; ?>
      <?php endif; ?>
      <?php endif; ?>
      <?php if ($type == 'related' || $type == 'upsell'): ?>
      <?= /* @escapeNotVerified */ ($iterator++ == 1) ? '<li class="item product product-item" style="display: none;">' : '</li><li class="item product product-item" style="display: none;">' ?>
      <?php else: ?>
      <?= /* @escapeNotVerified */ ($iterator++ == 1) ? '<li class="item product product-item">' : '</li><li class="item product product-item">' ?>
      <?php endif; ?>
      <div class="product-item-info <?= /* @escapeNotVerified */ $available ?>">
      <?= /* @escapeNotVerified */ '<!-- ' . $image . '-->' ?>
      <a href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>" class="product photo product-item-photo">
      <?= $block->getImage($_item, $image)->toHtml() ?>
      </a>
      <div class="product details product-item-details">
      <strong class="product name product-item-name"><a class="product-item-link" title="<?= $block->escapeHtml($_item->getName()) ?>" href="<?= /* @escapeNotVerified */ $block->getProductUrl($_item) ?>">
      <?= $block->escapeHtml($_item->getName()) ?></a>
      </strong>

      <?= /* @escapeNotVerified */ $block->getProductPrice($_item) ?>

      <?php if ($templateType): ?>
      <?= $block->getReviewsSummaryHtml($_item, $templateType) ?>
      <?php endif; ?>

      <?php if ($canItemsAddToCart && !$_item->isComposite() && $_item->isSaleable() && $type == 'related'): ?>
      <?php if (!$_item->getRequiredOptions()): ?>
      <div class="field choice related">
      <input type="checkbox" class="checkbox related" id="related-checkbox<?= /* @escapeNotVerified */ $_item->getId() ?>" name="related_products[]" value="<?= /* @escapeNotVerified */ $_item->getId() ?>" />
      <label class="label" for="related-checkbox<?= /* @escapeNotVerified */ $_item->getId() ?>"><span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span></label>
      </div>
      <?php endif; ?>
      <?php endif; ?>

      <?php if ($showAddTo || $showCart): ?>
      <div class="product actions product-item-actions">
      <?php if ($showCart): ?>
      <div class="actions-primary">
      <?php if ($_item->isSaleable()): ?>
      <?php if ($_item->getTypeInstance()->hasRequiredOptions($_item)): ?>
      <button class="action tocart primary" data-mage-init='"redirectUrl": "url": "<?= /* @escapeNotVerified */ $block->getAddToCartUrl($_item) ?>"' type="button" title="<?= /* @escapeNotVerified */ __('Add to Cart') ?>">
      <span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
      </button>
      <?php else: ?>
      <?php $postDataHelper = $this->helper('MagentoFrameworkDataHelperPostHelper');
      $postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()])
      ?>
      <button class="action tocart primary"
      data-post='<?= /* @escapeNotVerified */ $postData ?>'
      type="button" title="<?= /* @escapeNotVerified */ __('Add to Cart') ?>">
      <span><?= /* @escapeNotVerified */ __('Add to Cart') ?></span>
      </button>
      <?php endif; ?>
      <?php else: ?>
      <?php if ($_item->getIsSalable()): ?>
      <div class="stock available"><span><?= /* @escapeNotVerified */ __('In stock') ?></span></div>
      <?php else: ?>
      <div class="stock unavailable"><span><?= /* @escapeNotVerified */ __('Out of stock') ?></span></div>
      <?php endif; ?>
      <?php endif; ?>
      </div>
      <?php endif; ?>

      <?php if ($showAddTo): ?>
      <div class="secondary-addto-links actions-secondary" data-role="add-to-links">
      <?php if ($addToBlock = $block->getChildBlock('addto')): ?>
      <?= $addToBlock->setProduct($_item)->getChildHtml() ?>
      <?php endif; ?>
      </div>
      <?php endif; ?>
      </div>
      <?php endif; ?>
      </div>
      </div>
      <?= ($iterator == count($items)+1) ? '</li>' : '' ?>
      <?php endforeach ?>
      </ol>
      </div>
      </div>






      I would like this to be inbetween the name and price so I add this line-



       <div><?= $block->escapeHtml($_item->getSku()) ?></div>


      It doesn't work. How can I add sku to my related products?







      magento2.2.6 related-products






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 12 mins ago









      Rakesh Donga

      1,535316




      1,535316










      asked Feb 20 at 21:15









      tjjentjjen

      318115




      318115




















          1 Answer
          1






          active

          oldest

          votes


















          1














          <?= $block->escapeHtml($_item->getSku()) ?> is correct I had a cache issue. I will leave this up for anyone else needing to add an attribute to related products.





          share|improve this answer






















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "479"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f262727%2fhow-can-i-add-an-attribute-to-related-products%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            <?= $block->escapeHtml($_item->getSku()) ?> is correct I had a cache issue. I will leave this up for anyone else needing to add an attribute to related products.





            share|improve this answer



























              1














              <?= $block->escapeHtml($_item->getSku()) ?> is correct I had a cache issue. I will leave this up for anyone else needing to add an attribute to related products.





              share|improve this answer

























                1












                1








                1







                <?= $block->escapeHtml($_item->getSku()) ?> is correct I had a cache issue. I will leave this up for anyone else needing to add an attribute to related products.





                share|improve this answer













                <?= $block->escapeHtml($_item->getSku()) ?> is correct I had a cache issue. I will leave this up for anyone else needing to add an attribute to related products.






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 20 at 21:43









                tjjentjjen

                318115




                318115



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f262727%2fhow-can-i-add-an-attribute-to-related-products%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Magento 2 - Add success message with knockout Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Success / Error message on ajax request$.widget is not a function when loading a homepage after add custom jQuery on custom themeHow can bind jQuery to current document in Magento 2 When template load by ajaxRedirect page using plugin in Magento 2Magento 2 - Update quantity and totals of cart page without page reload?Magento 2: Quote data not loaded on knockout checkoutMagento 2 : I need to change add to cart success message after adding product into cart through pluginMagento 2.2.5 How to add additional products to cart from new checkout step?Magento 2 Add error/success message with knockoutCan't validate Post Code on checkout page

                    Fil:Tokke komm.svg

                    Where did Arya get these scars? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Favourite questions and answers from the 1st quarter of 2019Why did Arya refuse to end it?Has the pronunciation of Arya Stark's name changed?Has Arya forgiven people?Why did Arya Stark lose her vision?Why can Arya still use the faces?Has the Narrow Sea become narrower?Does Arya Stark know how to make poisons outside of the House of Black and White?Why did Nymeria leave Arya?Why did Arya not kill the Lannister soldiers she encountered in the Riverlands?What is the current canonical age of Sansa, Bran and Arya Stark?