11/22/2010

How to Get Product details Using Product Id in Magento

If you got the product id then you can get details of a product by using the following method in magento.

$obj = Mage::getModel('catalog/product');
$_product = $obj->load($product_id); // Enter your Product Id in $product_id
// get Product's name
echo $_product->getName();
//get product's short description
echo $_product->getShortDescription();
//get Product's Long Description
echo $_product->getDescription();
//get Product's Regular Price
echo $_product->getPrice();
//get Product's Special price
echo $_product->getSpecialPrice();
//get Product's Url
echo $_product->getProductUrl();
//get Product's image Url
echo $_product->getImageUrl();

10 comments:

  1. Nice post.
    Just one thing:
    How can I format the price I get using your explanation?
    The price value I get is like 119.8400.
    It has not currency symbol and has four decimals instead of two.

    Thanks

    ReplyDelete
    Replies
    1. getPrice(), true); # For Ex. 119.8400


      function format_money_price($number, $fractional=false) {
      if ($fractional) {
      $number = sprintf('%.2f', $number);
      }
      while (true) {
      $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
      if ($replaced != $number) {
      $number = $replaced;
      } else {
      break;
      }
      }
      return $number;
      }
      ?>


      Delete
    2. To format price, use:

      $helper = Mage::helper('core')->formatPrice($price,true);

      Note: here I have used $price for product price.

      Delete
  2. Nice Post!! Thanks...

    ReplyDelete
  3. I need to display add to cart button for each product. How?

    ReplyDelete
    Replies
    1. <input type="button" value="Add To Cart" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"/>

      Delete
  4. Anonymous5/19/2014

    Nice post! Its working....

    ReplyDelete
  5. Anonymous6/17/2014

    How to Get Product details Using Product Id in Magento dynamically plz help me

    ReplyDelete
  6. somebody knows how to get the credit card number and cvv after checkout, in a redirection payment api ?

    ReplyDelete
  7. can this be used to get any other attribute values ? how do you identify the attribute id/label ?

    ReplyDelete