1
votes

I use these lines below to get the CMS page data,

// Get current cms page from Mage:getSingleton
$cms_id = Mage::getSingleton('cms/page')->getIdentifier();
$cms_title = Mage::getSingleton('cms/page')->getTitle();
$cms_content = Mage::getSingleton('cms/page')->getContent();
var_dump($cms_content);

What about if I want to get a catalog product data? I want to get the product's name for instance,

$_helper = $this->helper('catalog/output');
$product_id = Mage::getSingleton('catalog/product')->getIdentifier();
$product = Mage::getModel('catalog/product')->load($product_id);
echo $_helper->productAttribute($_product, $_product->getName(), 'name');

result,

Fatal error: Call to a member function getName() on a non-object in C:\wamp\www\mywebiste.com\app\design\frontend\mywebsite\default\template\page\html\banner.phtml on line 6

Any ideas?

2

2 Answers

4
votes

To get catalog product data of particular category/product you can use below code

    $id = 3; // need to pass id of particular category
    $store_id = 1; // need to pass store id if you have multiple store

    $currentCategory = Mage::getModel('catalog/category')->setStoreId($store_id)->load($id);
    $collection = $currentCategory->getProductCollection();
    $collection->load();

    foreach ($collection as $product):

    $product = Mage::getModel('catalog/product')->load($product->getId());// need to pass product id if want to load particular product

    $product_id = $product->getId();
    $product_name = $product->getName();
    $product_price = $product->getPrice();
    $product_img = Mage::getModel('catalog/product_media_config')->getMediaUrl( $product->getImage());

    endforeach;
0
votes

getIdentifier is not a member function of catalog/product class you can check

    $product_id = Mage::getSingleton('catalog/product');
    var_dump(method_exists($product_id, 'getIdentifier'));

So its not returning a ProductId or loading a Model, in result when method getName() called with empty object it throws a Fatal error