0

I want to show related products on product page only from my custom module. I am not sure if I can or is it possible to do it. I am doing a check by if my attribute is enable I will change the outcome of Is saleable function. I want to show related products on my phtml can anyone show me how to do this or tip

1

Try this hope it will help you.

 $_quickShopHelper =  $this->helper('Emthemes\QuickShop\Helper\Data');
switch ($type = $block->getType()) {

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

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

            $showWishlist = true;
            $showCompare = true;
            $showCart = false;
            $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
            $description = false;
        }
    break;

    case 'related':
        /** @var \Magento\Catalog\Block\Product\ProductList\Related $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();

            $showWishlist = true;
            $showCompare = true;
            $showCart = false;
            $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
            $description = false;
        }
    break;

?>

html part this is lengthy but you can modify as per your need

<?php if ($exist):?>

    <?php if ($type == 'related'): ?>
        <?php if ($type == 'related'): ?>
            <div class="block <?php /* @escapeNotVerified */ echo $class; ?>" data-mage-init='{"relatedProducts":{"relatedCheckbox":".related.checkbox"}}' data-limit="<?php /* @escapeNotVerified */ echo $limit; ?>" data-shuffle="<?php /* @escapeNotVerified */ echo $shuffle; ?>">
        <?php else: ?>
            <div class="block <?php /* @escapeNotVerified */ echo $class; ?>" data-mage-init='{"upsellProducts":{}}' data-limit="<?php /* @escapeNotVerified */ echo $limit; ?>" data-shuffle="<?php /* @escapeNotVerified */ echo $shuffle; ?>">
        <?php endif; ?>
    <?php else: ?>
        <div class="block <?php /* @escapeNotVerified */ echo $class; ?>">
    <?php endif; ?>
    <div class="block-title title">
        <strong id="block-<?php /* @escapeNotVerified */ echo $class?>-heading" role="heading" aria-level="2"><?php /* @escapeNotVerified */ if($title=="Related Products") { echo "Similar Products"; } else { echo $title; } ?></strong>
    </div>
    <div class="block-content content" aria-labelledby="block-<?php /* @escapeNotVerified */ echo $class?>-heading">
        <?php if ($type == 'relateda' && $canItemsAddToCart): ?>
        <div class="block-actions">
            <?php /* @escapeNotVerified */ echo __('Check items to add to the cart or') ?>
            <button type="button" class="action select" role="select-all"><span><?php /* @escapeNotVerified */ echo __('select all') ?></span></button>
        </div>
        <?php endif; ?>
        <div class="products wrapper grid products-grid products-<?php /* @escapeNotVerified */ echo $type; ?>">       
            <ul class="products list items product-items owl-carousel">
                <?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'): ?>
                        <li class="item product product-item" style="display: none;">
                <?php else: ?>
                        <li class="item product product-item">
                <?php endif; ?>
                <div class="product-item-info <?php /* @escapeNotVerified */ echo $available; ?>">
                    <div class="product-shop-top">
                    <?php /* @escapeNotVerified */ echo '<!-- ' . $image . '-->' ?>
                    <a href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>" class="product photo product-item-photo">
                        <?php echo $block->getImage($_item, $image)->toHtml(); ?>
                    </a>
                    <div class="em-element-display-hover bottom">
                        <div class="em-btn-addto">
                            <?php if ($showWishlist || $showCompare || $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": "<?php /* @escapeNotVerified */ echo $block->getAddToCartUrl($_item) ?>"}}' type="button" title="<?php /* @escapeNotVerified */ echo __('Buy Now') ?>">
                                                        <span><?php /* @escapeNotVerified */ echo __('Buy Now') ?></span>
                                                    </button>-->
                                                <?php else: ?>
                                                    <?php $postDataHelper = $this->helper('Magento\Framework\Data\Helper\PostHelper');
                                                    $postData = $postDataHelper->getPostData($block->getAddToCartUrl($_item), ['product' => $_item->getEntityId()])
                                                    ?>
                                                    <!-- <button class="action tocart primary" data-mage-init='{"redirectUrl": {"url": "<?php /* @escapeNotVerified */ echo $block->getAddToCartUrl($_item) ?>"}}' type="button" title="<?php /* @escapeNotVerified */ echo __('Buy Now') ?>">
                                                        <span><?php /* @escapeNotVerified */ echo __('Buy Now') ?></span>
                                                    </button> -->
                                                <?php endif; ?>
                                            <?php else: ?>
                                                <?php if ($_item->getIsSalable()): ?>
                                                    <div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
                                                <?php else: ?>
                                                    <div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
                                                <?php endif; ?>
                                            <?php endif; ?>
                                        </div>
                                    <?php endif; ?>

                                    <?php if ($showWishlist || $showCompare): ?>
                                        <div class="secondary-addto-links actions-secondary" data-role="add-to-links">
                                            <?php if ($this->helper('Magento\Wishlist\Helper\Data')->isAllow() && $showWishlist): ?>
                                                <a href="#" data-post='<?php /* @escapeNotVerified */ echo $block->getAddToWishlistParams($_item); ?>'  class="action towishlist" data-action="add-to-wishlist" title="<?php /* @escapeNotVerified */ echo __('Add to Wish List') ?>">
                                                    <span><?php /* @escapeNotVerified */ echo __('Add to Wish List') ?></span>
                                                </a>
                                            <?php endif; ?>                                          
                                        </div>
                                    <?php endif; ?>
                                    <?php if($_quickShopHelper->getConfig('quickshop/general/active')): ?>
                            <?php echo $_quickShopHelper->getQuickShopButton($_item); ?>
                        <?php endif; ?> 
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    </div>
                    <div class="product details product-item-details">
                        <strong class="product name product-item-name"><a class="product-item-link" title="<?php echo $block->escapeHtml($_item->getName()) ?>" href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($_item) ?>">
                            <?php echo $block->escapeHtml($_item->getName()) ?></a>
                        </strong>

                        <?php /* @escapeNotVerified */ echo $block->getProductPrice($_item); ?>

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

                            <?php if (!$_item->isComposite() && $_item->isSaleable() && $type == 'relateda'): ?>
                                <?php if (!$_item->getRequiredOptions()): ?>
                                    <div class="field choice related">
                                        <input type="checkbox" class="checkbox related" id="related-checkbox<?php /* @escapeNotVerified */ echo $_item->getId() ?>" name="related_products[]" value="<?php /* @escapeNotVerified */ echo $_item->getId() ?>" />
                                        <label class="label" for="related-checkbox<?php /* @escapeNotVerified */ echo $_item->getId() ?>"><span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span></label>
                                    </div>
                                <?php endif; ?>
                            <?php endif; ?>
                            
                        </div>
                    </div>
                </li>
                <?php endforeach ?>
            </ul>
        </div>
    </div>
</div>
<?php endif;?>
| improve this answer | |
  • 1
    What will be the html part for this I am struggling on that now I can’t get it to show, I have put this case but I can’t put the html, as in how to load the title image and price for related products – Jack Brooks Oct 20 at 6:26
  • Okey I will help for html part – jykmhar123 Oct 20 at 6:29
  • Thank you @jykmhar – Jack Brooks Oct 20 at 6:33
  • 1
    Try this reference – jykmhar123 Oct 20 at 6:37
  • 1
    I will give this a try – Jack Brooks Oct 20 at 6:38
0

It should be pretty simple. You can even achieve this just by overwriting the template.

Just copy the content of magento/module-catalog/view/frontend/templates/product/list/items.phtml in to your theme YourTheme/Magento_Catalog/templates/product/list/items.phtml and then change the logic for this $block->getItems():

case 'related':
    /** @var \Magento\Catalog\Block\Product\ProductList\Related $block */
    if ($exist = $block->getItems()->getSize()) { //change the logic
        $type = 'related';
        $class = $type;

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

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

Hope this helps.

| improve this answer | |
  • Hi @Adarsh what will be the html part to show it in frontend, would I just move whatever is in the item.phtml to my module. I don’t want to move it into theme I want a separate module that’s why – Jack Brooks Oct 19 at 22:22
  • Yes, you should copy the entire content of that file and change the logic as you want. You may have noticed that file is used for other types too. – Adarsh Khatri Oct 19 at 22:27

Your Answer

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

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