Skip to content

How to get related product collection in block

I use this way to get related product collection

/**
 * Prepare and return product collection
 *
 * @return MagentoCatalogModelResourceModelProductCollection
 */
public function createCollection()
{
    /** @var $collection MagentoCatalogModelResourceModelProductCollection */
    $myBlock = MagentoFrameworkAppObjectManager::getInstance()->get('AntonStudioStockUpdateBlockRelatedProducts');

    $currentProduct = $myBlock->getCurrentProduct();
    $relatedProducts = $currentProduct->getRelatedProductCollection();
    /**
     * Prevent retrieval of duplicate records. This may occur when multiselect product attribute matches
     * several allowed values from condition simultaneously
     */
    $relatedProducts->distinct(true);

    return $relatedProducts;
}

and used it in template

<?php if ( $exist = ( $block->getProductCollection() && $block->getProductCollection()->getSize() ) ): ?>
    <?php
    $type = 'widget-product-grid';

    $mode = 'grid';

    $image = 'new_products_content_widget_grid';
    $items = $block->getProductCollection()->getItems();

    $showWishlist = true;
    $showCompare  = true;
    $showCart     = true;
    $templateType = MagentoCatalogBlockProductReviewRendererInterface::DEFAULT_VIEW;
    $description  = false;

    $objectManager = MagentoFrameworkAppObjectManager::getInstance();

    $_helper      = $this->helper( 'MagentoCatalogHelperOutput' );
    $_imagehelper = $this->helper( 'MagentoCatalogHelperImage' );
    ?>

    <div class="block widget block-products-list <?= /* @noEscape */
    $mode ?>">
        <?php if ( $block->getTitle() ): ?>
            <div class="block-title">
                <strong><?= $block->escapeHtml( __( $block->getTitle() ) ) ?></strong>
            </div>
        <?php endif ?>
        <div class="block-content">
            <?= /* @noEscape */
            '<!-- ' . $image . '-->' ?>
            <div class="home-products-<?= /* @noEscape */
            $mode ?> <?= /* @noEscape */
            $mode ?>">
                <ol class="home-product-items <?= /* @noEscape */
                $type ?>">
                    <?php $iterator = 1; ?>
                    <?php foreach ( $items as $_item ): ?>
                        <?php
                        $_product = $objectManager->get( 'MagentoCatalogModelProduct' )->load( $_item->getId() );
                        $all_data = $_product->getData();

                        $imageUrl = $_imagehelper->init( $_item, 'product_thumbnail_image' )->resize(300, 300)->getUrl();
                        ?>

                        <?= /* @noEscape */
                    ( $iterator ++ == 1 ) ? '<li class="product-item">' : '</li><li class="product-item">' ?>

                        <div class="ProductListItem">
                            <a href="<?= $block->escapeUrl($block->getProductUrl($_item)) ?>" class="ProductListItem__imageWrap">
                                <img src="<?= $imageUrl ?>"
                                     class="ProductListItem__productImage">
                            </a>
                            <div class="ProductListItem__summaryWrap">
<!--                                <div class="ProductListItem__productSize">--><?//= isset($all_data['tile_length_mm'])?$all_data['tile_length_mm']:'' . ' * ' . isset($all_data['tile_width_mm'])?$all_data['tile_width_mm']:'' ?><!--</div>-->
                                <a href="<?= $block->escapeUrl($block->getProductUrl($_item)) ?>" class="ProductListItem__productTitle">title<?= $block->escapeHtml( $_item->getName() ) ?></a>
                            </div>
                            <div class="ProductListItem__addToCartWrap">
                                <div class="ProductListItem__price">
                                    <?= $block->getProductPriceHtml( $_item, $type ) ?>/M2
                                </div>
                                <div class="ProductListItem__addToCartIcon">
                                    <?php if ($this->helper('MagentoWishlistHelperData')->isAllow() && $showWishlist): ?>
                                        <a href="#"
                                           data-post='<?= /* @noEscape */ $block->getAddToWishlistParams($_item) ?>' class="action towishlist" data-action="add-to-wishlist" title="<?= $block->escapeHtmlAttr(__('Add to Wish List')) ?>">
                                            <span><?= $block->escapeHtml(__('Add to Wish List')) ?></span>
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                        <?= ( $iterator == count( $items ) + 1 ) ? '</li>' : '' ?>
                    <?php endforeach ?>
                </ol>
            </div>
            <?= $block->getPagerHtml() ?>
        </div>
    </div>
<?php endif; ?>

But $block->escapeHtml( $_item->getName() ) out put null