Reputation: 4421
I am trying to output products from some category on an arbitrary page in a fashion similar to that of list.phtml's grid format.
I have the following snippet:
$category = Mage::getModel('catalog/category');
$category->load(17);
$_productCollection = $category->getProductCollection()
->addAttributeToSelect('name');
$_helper = Mage::helper('catalog/output');
That gives me a product collection which I then iterate over with:
foreach ($_productCollection as $_product):
<!-- This works -->
<h2 class="product-name">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">
<?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?>
</a>
</h2>
<!-- This does not -->
<?php echo $this->getPriceHtml($_product, true) ?>
<!-- This just returns out of stock -->
<div class="actions">
<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')">
<span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
endforeach;
The above code except for the call to get the product collection at the top is just borrowed from list.phtml.
Can anyone tell me why the price and is saleable information is not available, hence why the item appears out of stock? Previously when the product name was unavailable, I had to add ->addAttributeToSelect('name')
, would I need to add something along those lines?
Upvotes: 2
Views: 2457
Reputation: 31
$productBlock=$this->getLayout()->createBlock("catalog/product");
echo $productBlock->getPriceHtml($_product,true);
Try this
Upvotes: 0
Reputation: 3077
Please try the code below in your phtml file.
$category = Mage::getModel('catalog/category')->load(3);
$_productCollection = $category->getProductCollection()->addAttributeToSelect('*');
$productBlock=$this->getLayout()->createBlock("catalog/product");
foreach($_productCollection as $_product)
{
//for get the price of product
if($_product->isSaleable()) //this will check if product is in stock
echo $productBlock->getPriceHtml($_product,true);
}
Upvotes: 4
Reputation: 3694
So you're on the right path to look into core, if you want to copy some functional that is similar to the one from the basic Magento - like product listing.
getPriceHtml
is a method defined in the abstract class Mage_Catalog_Block_Product_Abstract
. So to use it, you need to extend your block from the Mage_Catalog_Block_Product_Abstract
one.isSaleable
returned false because you didn't have some of the attributes joined to your collection.Here's how you should accomplish your goal, if you want to follow Magento's logic.
Create your own module, or just block in local/Mage/Catalog/Block/YourBlock.php
. This block should extend Mage_Catalog_Block_Product_Abstract
. After that create a method in this block getCustomProductCollection()
:
pubcli funciton getCustomProductCollection()
{
if (is_null($this->_productCollection)) {
$category = Mage::getModel('catalog/category')->load(17);
$layer = $this->getLayer();
$layer->setCurrentCategory($category);
$this->_productCollection = $layer->getProductCollection();
}
return $this->_productCollection;
}
Now in your phtml
file you'll just call for this method:
$productCollection = $this->getCustomProductCollection();
And the rest of the code will work.
Upvotes: 2