0

I have added a custom block in each product item, in category list page:

catalog_category_view.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

        <referenceContainer name="content">
            <block class="Magento\Catalog\Block\Category\View" name="category.products" template="Magento_Catalog::category/products.phtml">
                <block class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
                    <block class="Magento\Catalog\Block\Product\ProductList\Item\Container" name="category.product.addto" as="addto">

                        <block class="Mymodules\Auction\Block\Product\CustomList\AuctionList" name="auction.product.customlist" as="auction-product-customlist" template="Mymodules_Auction::product/list/auction_list.phtml" />

                    </block>
                </block>
            </block>
        </referenceContainer>

    </body>
</page>

Mymodules/Auction/Block/Product/CustomList/AuctionList.php

<?php
namespace Mymodules\Auction\Block\Product\CustomList;

class AuctionList extends \Magento\Framework\View\Element\Template
{

    protected $_coreRegistry;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,       
        \Magento\Framework\Registry $coreRegistry,
        array $data = []
    )
    {       
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context, $data);
    }

    public function getCurrentProduct()
    {       
        return $this->_coreRegistry->registry('current_product');
    }
}

How can I get current product object?

Function getCurrentProduct() above is not working.

Is it possible to pass current product object as an argument in catalog_category_view.xml?

  • Hi, what do you want to display in this block? Because you can simply override lst.phtml file from Magento_Catalog and than in foreach loop you will have access to current product. – SebastianT May 18 at 8:29
  • I want to load custom module data, it's not possible just by working on list.pthml file – zekia May 18 at 9:37
  • 1
    So if you call your block in list.phtml file, you can try to load product by Id. this should help: magento.stackexchange.com/questions/94851/… – SebastianT May 18 at 13:24

Your Answer

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

Browse other questions tagged or ask your own question.