Retrieving product information in Magento

本文介绍了如何使用Magento的Mage::getModel方法获取产品信息,并应用于模板中,包括加载产品、显示产品名称、价格、URL等关键信息。

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<wbr></wbr>‘catalog/product’<wbr></wbr>relatesto app/code/core/Mage/Catalog/Model/Product.php<wbr></wbr>file.If you open that file you will see it’s actually a classnamed<wbr></wbr>Mage_Catalog_Model_Product.That same class is extending the<wbr></wbr>Mage_Catalog_Model_Abstract<wbr></wbr>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<wbr></wbr>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<wbr></wbr>custom.After you created the variable containing the model “catalog/product”you can then call all of the methods of<wbr></wbr>Mage_Catalog_Model_Product<wbr></wbr>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<wbr></wbr>$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.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值