如何在Magento 1.8中获取目录产品数据?

时间:2014-04-24 10:04:24

标签: php magento magento-1.7

我使用下面的这些行来获取CMS页面数据,

// 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);

如果我想获取目录产品数据怎么样?我想获得产品的名称,例如

$_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');

结果,

  

致命错误:在非对象中调用成员函数getName()   C:\ WAMP \ WWW \ mywebiste.com \程序\设计\前台\ mywebsite \ DEFAULT \模板\页面\ HTML \ banner.phtml   在第6行

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

要获取特定类别/产品的目录产品数据,您可以在代码

下使用
    $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;

答案 1 :(得分:0)

getIdentifier不是catalog/product类的成员函数 你可以检查

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

所以它没有返回ProductId或加载Model,结果当方法getName()调用空对象时会抛出致命错误