5

I need to show related products block on configurable product page (as it is by deflault), but instead of related products of configurable product i need to show related products of simple products which are included in that configurable product.

| improve this question | | | | |
  • ok but you can display only the products under configurable products that are visible and in stock with inventory will show. what you want here ? – Charul Tyagi Sep 10 '18 at 10:40
  • i need that when i select simple product for any configurable product that simple product related product data should also be shown as related product in configurable product – Arjun Sep 10 '18 at 11:01
  • so if you select any simple product it should be visibility should be catalog,search to front-end ,in stock and have inventory too then can list down in related products – Charul Tyagi Sep 10 '18 at 11:22
  • Ok let me recorrect myself when i redirect to any configurable product page i need to see related products on that page but that related products should not be of that configurable product but of all the simple products attach to that configurable product – Arjun Sep 10 '18 at 12:25
4
+50

If you want to display related products of child items instead of the related products of the parent item in a configurable product structure, then

Create a new module and override _prepareData function the main block file

vendor/magento/module-catalog/Block/Product/ProductList/Related.php

Something like

protected function _prepareData() {
    $product = $this->_coreRegistry->registry('product');
    /* @var $product \Magento\Catalog\Model\Product */

    // GET TYPE
    $productType = $product->getTypeId();

    // IF CONFIGURABLE
    if ($productType == 'configurable') {
        $childrenProducts = $product->getTypeInstance()->getUsedProducts($product);
        if ($childrenProducts) {
            $this->_itemCollection = array();
            foreach ($childrenProducts as $singleChild) {
                $relatedProductCollection = $singleChild->getRelatedProductCollection()->addAttributeToSelect(
                                'required_options'
                        )->setPositionOrder()->addStoreFilter();

                if ($this->moduleManager->isEnabled('Magento_Checkout')) {
                    $this->_addProductAttributesAndPrices($relatedProductCollection);
                }
                $relatedProductCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
                $relatedProductCollection->load();
                foreach ($relatedProductCollection as $singleProduct) {
                    $singleProduct->setDoNotUseCategoryId(true);
                    $this->_itemCollection[] = $singleProduct;
                }
            }
        }
    } else {
        // IF OTHERS

        // main FUNCITON CODE
    }


    return $this;
}

and then override the items.phtml file

vendor/magento/module-catalog/view/frontend/templates/product/list/items.phtml

under case 'related' change the if condition

if ($exist = $block->getItems()->getSize())

to

if ($exist = count($block->getItems()))
| improve this answer | | | | |

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.