0

I have cobbled together a module for Magento 2 that is eventually going to insert a block in to the product page that pulls in other products with a similar attribute (in this case, "range").

When the module is enabled, I see it in the setup_modules table in the database, and see the CSS I've inserted in to the page from the module, however product pages return a 404 error.

If I remove the block call from the catalog_product_view.xml file that references the template, the page loads.

app/code/Company/RangeCollection/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Company_RangeCollection" setup_version="2.0.1" schema_version="0.0.1" active="true" />
</config>

app/code/Company/RangeCollection/Block/RangeCollection.php

<?php
namespace Company\RangeCollection\Block;

class RangeCollection extends \Magento\Catalog\Block\Product\AbstractProduct {

    protected $_collection;
    protected $_config;


    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Catalog\Model\Resource\Product\Collection $collection,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        array $data = []
    ) {
        $this->_collection = $collection;
        $this->_config = $scopeConfig->getValue('range_collection/settings');
        parent::__construct($context, $data);
    }

    public function getProducts($currentRegion) {
        $limit = $this->getProductLimit();
        $collection = $this->_collection;
            //->addAttributeToFilter('region', 1);
        $collection->getSelect()
            ->order('rand()')
            ->limit($limit);
        return $collection;
    }

    public function getConfig() {
        return $this->_config;
    }

    public function getProductLimit() {
        return $this->_config["limit"];
    }

}

app/code/Company/RangeCollection/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Company\RangeCollection\Block\RangeCollection" name="range.collection" as="range" after="-" template="Company_RangeCollection::list.phtml"/>
        </referenceContainer>
    </body>
</page>

app/code/Company/RangeCollection/etc/config.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Magento/Core/etc/config.xsd">
    <default>
        <range_collection>
            <settings>
                <limit>4</limit>
            </settings>
        </range_collection>
    </default>
</config>

app/code/Company/RangeCollection/view/frontend/templates/list.phtml

<?php
$_products = $this->getProducts();
$_config = $this->getConfig();
$_columnWidth = 100 / $this->getProductLimit();
?>

<h2><?php echo __('Other products in this range') ?></h2>

<ul class="products list items featured">
    <?php foreach ($_products as $_product): ?>
        <li class="item" style="width: <?php echo $_columnWidth; ?>%">
            <div class="product">
                <a href="<?php echo $_product->getProductUrl() ?>" class="product">
                    <span class="product-item-name"><?php echo $this->escapeHtml($_product->getName()) ?></span>
                </a>
                <?php echo $this->getProductPrice($_product) ?>
            </div>
        </li>
    <?php endforeach; ?>
</ul>
3
  • Have you defined registration.php file in module root Jul 28, 2016 at 9:17
  • I have, yes: <?php \Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Company_RangeCollection', DIR ); Excuse the rubbish formatting! Jul 28, 2016 at 9:18
  • Have to defined routes.xml
    – Arjun
    Jul 28, 2016 at 9:22

1 Answer 1

1

Define di.xml file at app/code/Company/RangeCollection/etc/di.xml

add below code to it:

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Product\AbstractProduct" type="Company\RangeCollection\Block\RangeCollection" /> 

</config>

Edit:

Change below line in file app/code/Company/RangeCollection/Block/RangeCollection.php :

\Magento\Catalog\Model\Resource\Product\Collection $collection,

to:

\Magento\Catalog\Model\ResourceModel\Product\Collection $collection,
3
  • Added this but still not working. I've just tried bin/magento setup:di:compile and now I'm getting: Class Magento\Catalog\Model\Resource\Product\Collection does not exist Jul 28, 2016 at 9:35
  • Updated answer check it Jul 28, 2016 at 9:43
  • It was my own mistake, I'd written Resource as opposed to ResourceModel. Compiled and now I'm seeing the block output on the page. Jul 28, 2016 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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