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

Php 如何在Magento 1.8中获取目录产品数据?,php,magento,magento-1.7,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();

我使用下面这些行获取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\app\design\frontend\mywebsite\default\template\page\html\banner.phtml 第6行

有什么想法吗?

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

    $product_id = Mage::getSingleton('catalog/product');
    var_dump(method_exists($product_id, 'getIdentifier'));
因此,它不会返回ProductId或加载模型,结果是当使用空对象调用方法getName时,它会抛出一个致命错误

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

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

因此,它不会返回ProductId或加载模型,结果是,当使用空对象调用方法getName时,它会抛出一个致命错误,以获取您可以在下面代码中使用的特定类别/产品的目录产品数据

    $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;

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

    $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;

谢谢你的回答!:谢谢你的回答D