Retrieving product information in Magento

If you are developing Magento template sooner or later you willneed to obtain some product information and rearrange it to fityour template. One of the most useful function while working withMagento is Mage::getModel($modelClass).

Here is how you would use it to retrieve Magento productinformationMage::getModel(‘catalog/product’);.

This ‘catalog/product’ relatesto app/code/core/Mage/Catalog/Model/Product.php file.If you open that file you will see it’s actually a classnamed Mage_Catalog_Model_Product.That same class is extending the Mage_Catalog_Model_Abstract classmeaning it inherits all of it’s methods.

For the last few weeks I’ve been using NetBeans 6.5beta, regularlydownloading night builds of it. Let’s suppose you open the/template/catalog/category/view.phtml file(ar any other from template folder) and you write down the“Mage_Catalog_Model_Product” + “Ctrl + Space” NetBeans will giveyou a code completion with all of the available methods that goalong with the class.

All you need to do is something like

$cProduct = Mage::getModel(‘catalog/product’);

Don’t let the name variable $cProduct confuse you. I’m using theletter “c”as a sort of personal standard, stands for custom.After you created the variable containing the model “catalog/product”you can then call all of the methods of Mage_Catalog_Model_Product onit.

Here is how:

$cProduct->load(152);

echo ‘<p>Product name:<strong>’ .$cProduct->getName() .‘</strong></p>’;
echo ‘<p>Product price:<strong>’ .$cProduct->getPrice() .‘</strong></p>’;
echo ‘<p>Product url:<strong>’ .$cProduct->getProductUrl() .‘</strong></p>’;
echo ‘<p>Product Category:<strong>’ .$cProduct->getCategory() .‘</strong></p>’;
echo ‘<p>Product image url:<strong>’ .$cProduct->getImageUrl() .‘</strong></p>’;
echo ‘<p>Product status (this one isboolean): <strong>’ .$cProduct->getStatus() .‘</strong></p>’;
echo ‘<p>Product weight:<strong>’ .$cProduct->getWeight() .‘</strong></p>’;

Most important line in the above code is the $cProduct->load(152);.This is where you tell the $cProduct variable to point to productwith id 152 (it was 152 on my working sample, it could be any othernumber). You can see the products id value if you log on to theAdmin interrface and go to Catalog > Manage products> And look into the table listing all theproducts.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值