I need to display a product list from a category id given in a product-view page. After a large research i could see people saying this is not possible due type=catalog/product_list don't work in a product-view page. Is there any way to accomplish this??.
Magento – display product list in product view page
product-collectionproduct-listproduct-view
Related Solutions
You need to create a new extension that checks, before rendering the quick search (or advanced search) page if the product collection contains exactly one product.
For this let's create a new extension called StackExchange_CatalogSearch
.
You will need to following files:
app/etc/modules/StackExchange_CatalogSearch.xml
- the declaration file
<?xml version="1.0"?>
<config>
<modules>
<StackExchange_CatalogSearch>
<codePool>local</codePool>
<active>true</active>
<depends>
<Mage_CatalogSearch />
</depends>
</StackExchange_CatalogSearch>
</modules>
</config>
app/code/local/StackExchange/CatalogSearch/etc/config.xml
- the configuration file:
<?xml version="1.0"?>
<config>
<modules>
<StackExchange_CatalogSearch>
<version>1.0.0</version>
</StackExchange_CatalogSearch>
</modules>
<global>
<models>
<stackexchange_catalogsearch>
<class>StackExchange_CatalogSearch_Model</class>
</stackexchange_catalogsearch>
</models>
</global>
<frontend>
<events>
<controller_action_layout_render_before_catalogsearch_result_index><!-- for the quick search-->
<observers>
<stackexchange_catalogsearch>
<model>stackexchange_catalogsearch/observer</model>
<method>redirectToProduct</method>
</stackexchange_catalogsearch>
</observers>
</controller_action_layout_render_before_catalogsearch_result_index>
<controller_action_layout_render_before_catalogsearch_advanced_result><!-- for the advanced search-->
<observers>
<stackexchange_catalogsearch>
<model>stackexchange_catalogsearch/observer</model>
<method>redirectToProduct</method>
</stackexchange_catalogsearch>
</observers>
</controller_action_layout_render_before_catalogsearch_advanced_result>
</events>
</frontend>
</config>
app/code/local/StackExchange/CatalogSearch/Model/Observer.php
- the observer that does all the work.
<?php
class StackExchange_CatalogSearch_Model_Observer
{
//the product list block name in layout
const RESULT_BLOCK_NAME = 'search_result_list';
public function redirectToProduct($observer)
{
/** @var Mage_Catalog_Block_Product_List $block */
$block = Mage::app()->getLayout()->getBlock(self::RESULT_BLOCK_NAME);
if ($block) {
$collection = $block->getLoadedProductCollection();
if ($collection && $collection->getSize() == 1) {
/** @var Mage_Catalog_Model_Product $product */
$product = $collection->getFirstItem();
$url = $product->getProductUrl();
if ($url){
Mage::app()->getResponse()->setRedirect($url);
Mage::app()->getResponse()->sendResponse();
exit; //stop everything else
}
}
}
}
}
Clear the cache, disable compilation if enabled and give it a go.
Note: This extension redirects to the product page when the search (and advanced search) page should return only on product, even if this happens after the search or after applying a layered navigation filter.
Open:
/app/design/frontend/default/YOURTHEME/template/catalog/product/view.phtml
Add this:
<?php if ($_product->isAvailable()): ?>
<p class="availability in-stock"><?php echo $this->__('Qty Available:') ?> <span><!--<?php echo $this->__('In stock') ?>--><?= (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty()?></span></p>
<?php else: ?>
<p class="availability out-of-stock"><?php echo $this->__('Availability:') ?> <span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
I hope this is what you are looking for :)
Best Answer
You can always write your own block or inject some custom code into
catalog/product/view.phtml
to fetch a product collection of a given category and show them.So adding the
product_list
block into the product view page isn't the only option you've got.Be careful though because one wrong step and you will end with some really heavy load of SQL queries.
Cheers!