How can I add an attribute to related products?Display new attribute in related ProductsAdd to cart button in...

Tabular environment - text vertically positions itself by bottom of tikz picture in adjacent cell

Exempt portion of equation line from aligning?

Ultrafilters as a double dual

Too soon for a plot twist?

Giving a career talk in my old university, how prominently should I tell students my salary?

Can I challenge the interviewer to give me a proper technical feedback?

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

Why do we say 'Pairwise Disjoint', rather than 'Disjoint'?

Was this cameo in Captain Marvel computer generated?

Interpretation of linear regression interaction term plot

Who has more? Ireland or Iceland?

Boss Telling direct supervisor I snitched

Should we avoid writing fiction about historical events without extensive research?

direct sum of representation of product groups

What would be the most expensive material to an intergalactic society?

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

Insult for someone who "doesn't know anything"

What does it take to become a wilderness skills guide as a business?

Why is there an extra space when I type "ls" on the Desktop?

What is better: yes / no radio, or simple checkbox?

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

Are small insurances worth it?

How does learning spells work when leveling a multiclass character?

How do you use environments that have the same name within a single latex document?



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 6 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

                    “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

                    How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

                    變成蝙蝠會怎樣? 參考資料 外部連結 导航菜单Thomas Nagel, "What is it like to be a...