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.
本文介绍了如何使用Magento的Mage::getModel方法获取产品信息,并应用于模板中,包括加载产品、显示产品名称、价格、URL等关键信息。
2万+

被折叠的 条评论
为什么被折叠?



