你现在的位置:首页 > PHP网站建设知识库 > magento > 正文

获取magento商品ID和商品信息的代码

我们可以通过下面两个方法来获取magento的商品信息:

方法一:通过ID里获取商品详情

<?php
$model = Mage::getModel('catalog/product') //getting product model
 
$_product = $model->load($productid); //getting product object for particular product id
 
echo $_product->getShortDescription(); //product's short description
echo $_product->getDescription(); // product's long description
echo $_product->getName(); //product name
echo $_product->getPrice(); //product's regular Price
echo $_product->getSpecialPrice(); //product's special Price
echo $_product->getProductUrl(); //product url
echo $_product->getImageUrl(); //product's image url
echo $_product->getSmallImageUrl(); //product's small image url
echo $_product->getThumbnailUrl(); //product's thumbnail image url  
 
?>
方法二:通过商品名获取商品ID

<?php
$product_name = 'Test Product'; //product name
$model = Mage::getModel('catalog/product') //getting product model
$collection = $model->getCollection(); //products collection
foreach ($collection as $product) //loop for getting products
{                  
 
    $model->load($product->getId());
    $pname = $model->getName();
    if(strcmp($pname,$product_name)==0)
    {
    $id = $product->getId();
    }
}
echo 'Required ID->'.$id; //id of product
?>