I want listing a (inactive) category product in the right column in all page of my magento site. To make this i used a snippet of code that i found in stackoverflow site.
Below there is the snippet of code I have adapted to my esigence(sorry for ugly code but I'm not developer):
<?php $h3h3=Mage::getModel('catalog/category')->load(15); // YOU NEED TO CHANGE 5 TO THE ID OF YOUR CATEGORY
$h3h3=$h3h3->getProductCollection();
if(!$h3h3->count()):
// do nothing ?>
<?php else: ?>
<div class="box base-mini ricette">
<div class="head">
<h4>Ricette consigliate</h4>
</div>
<?php foreach($h3h3->getAllIds() as $lol)
{ $_product=Mage::getModel('catalog/product')->load($lol); ?>
<a href="<?php echo $_product->getProductUrl() ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'image')->resize(240, 162); ?>" width="240" height="162" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a>
<h5><a href="<?php echo $_product->getProductUrl() ?>"><?php print $_product->getName();
echo $this->getPriceHtml($_product, true);?>
</a></h5>
</div>
<?php break; } ?>
<?php endif; ?>
<div style="margin-bottom:10px">
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('generic_right_block')->toHtml() ?>
</div>
The problem of this code is that $_product->getProductUrl() gives me a wrong url of product. Besides this url change depending on the page, for example if I'am on the Home page the url of my product is : my.domain.com/index.php/myproduct.html. Else if i'am on the category view, but not the category of my product, the url is : my.domain.com/index.php/catalog/product/view/id/51/s/myproduct/category/3/
In each of these case the page of product correctly opens but not with the custom template assigned at the category ( and its children ) of my product. Instead of my custom layout assigned only to this category ( and its children ) is loaded the default layout of all generic category of magento site.
Instead with this correct url the custom layout of my category ( and its children ) is correctly loaded : my.domain.com/index.php/mycategory/myproduct.html
The problem is the same in the related product phtml standard script of Magento. But i've simply resolved with this snippet of code that re-build the url
$_categories = $_item->getCategoryIds();
$_category = Mage::getModel('catalog/category')->load(9);
$url = substr($this->getUrl($_category->getUrlPath($_item)),0,-6).'/'.basename($_item->getProductUrl());
I try to apply this solution to my script but without a result. Can anyone help me? Thank you!