In Magento eCommerce while working with catalog model, There arise the need to fetch product details from product id.
We can get all product details if we have product id.
But sometimes we only have product name, so we need to get product id for getting product details.
I am listing here both the method.
1) Product details from Product 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 ?>
2) Product ID from Product Name
This is little bit complex. (If anybody has better way please post here)
<?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 ?>
getId();
$_product = $model->load($productid);
echo $_product->getName() // get Product Name
echo $_product->getAttributeText(‘manufacturer’) // get Brand name
?>
Huge help, thanks a lot!
Glad it was helpful
Hello,
Thank you for you code, it’s really helpfull!
But I have a question: How can I get all id product and store them in an array ?
Because I want to update my Magento database to set the stock quantity receives from a web service.
Thank you so much for your help!
Hello, can you services me by giving answers to this question – how do i take my ecommerce internationally?
Sorry, I did not understand the question
How to set Add to cart button.
I have tried:
isSaleable()): ?>
<button type="button" title="__(‘Add to Cart’) ?>” class=”button btn-cart” onclick=”setLocation(‘getAddToCartUrl($_product) ?>’)”>__(‘Add to Cart’) ?>
__(‘Out of stock’) ?>
But not working
The code snippet is not correct, please refer the add to cart button found in default magento design, path goes like this
app/design/frontend/base/default/template/catalog/product/view
thnx!!! such a grt example
I am glad it was helpful
thx..
Great example.thanks!
glad it was helpful
It was helpful….
i want to add only product, so if i add the following code in any page, will that work?
getName(); //product name
?>
yupe
I am using soap api to access magento data for a third party application but when i try to add coupon code in shoping cart then it is not working and throes an error: Coupon code is not valid.
can anybody help me how to solve this issue.
my code is..
try {
$resultCartCouponAdd = $soap->call(
$connect,
‘cart_coupon.add’,
array(
‘CartId’,
‘testcoupon’,
‘default_store’
)
);}
catch (Exception $e)
{echo ‘…ERROR: ‘, $e->getMessage(); }
how to coupan code not valid error in moblie app and website in coupan code working.
Not understanding the question
This is the way I did always. But I believe this is not the best method.
What if there is a large amounts of products in the database? Select all of them is a big resources.
You can use filters, Please check these posts Filters in Query in Magento , Product Collection in Magento
Hi,
Which Magento API will give the base cost and classification of a product ? (catalog_product.info is not providing the info about base cost and calssification of the product).?
Thanks in advance..
product name you want to find and get their product id example;
$pname = array('My Product Name', 'His Product Name');
$pcollection = Mage::getSingleton('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('name', array('in' => $pname));
now you have a collection of products whos’ name are ‘My Product Name’ and ‘His Product Name’
many more options for the addFieldToFilter at http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections/
scroll to middle of page and see all the options
Hi
I am trying make a new API in magento 1.7 version.I craeted a three XML file and one php file.
XML file are the following
@shameem356 – Please check this out Creating a Custom API or Extending the Core API
Hi,
I have done as per the Magento instruction,. but I am getting the error as Invalid path ? can I have help from you to solve this path problem ?
@sharmeem356 – Can you share the files with me ?
Hi,
How to check a particular product is belongs to which store ?
@shameem356 – $products_model = Mage::getModel(‘catalog/product’)->load(2)->getStoreId(); //where 2 is product id
Product ID from Product Name :
$_product = Mage::getModel(‘catalog/product’)->loadByAttribute(‘name’, ‘Anashria Womens Premier Leather Sandal’);
echo $_product->entity_id;
Thank you Jamal. 🙂
this is a nice tutorial
thanks
Product Details by item id
$custom = Mage::getModel(‘catalog/product’)->load($_item->getProductId());
echo $custom->getShortDescription()
$custom = Mage::getModel(‘catalog/product’)->load($_item->getProductId());
echo $custom->getShortDescription()
I get php error :
Call to a member function load() on a non-object in /var/www/myshop/app/design/frontend/base/default/template/checkout/cart/item/default.phtml on line 43
what could be the problem here ?
grtz
gert
i got it, i copy/pasted the code, and it replaced the “‘” with another type of quote :s
Very helpful info here. Product ID is easy to use with this post!
well thanks i have got it but can you also tell me how i will get the company address and company name and also product starting date and ending date i mean the day product will expire ?
is it like this –
$_product->getProductCompany();
??????
@man_in_black – Are company address and company name custom attributes ??. You can check all the data by print_r($_product->getData()).
Good question…would like to know this to!