Sunday, February 8, 2015

get magento product details as json

<!--?php
/* Returns a JSON-List with all options for a certain product. 
* Get-Parameter: productId
*/

$productId = intval($_GET['productId']);

if($productId != "")  {
  require_once './../app/Mage.php';
  Mage::app();
  $product = Mage::getModel('catalog/product')--->load($productId);

  //Get all options for product
  $options = array();

  foreach ($product->getOptions() as $o) {
    $values = $o->getValues();
    $valuesArray = array();
    foreach ($values as $v) {
      $valuesArray[$v['option_type_id']] = array("option_type_id" => $v['option_type_id'], "option_id" => $v['option_id'], "sku" => $v['sku'], "default_title" => $v['default_title'], "price" => $v['price'], "price_type" => $v['price_type']);
    } 
    $option = array("id" => $o->getId(), "type" => $o->getType(), "title" => $o->getTitle(), "values" => $valuesArray);
    $options[$o->getId()]= $option;
  }

  //Get prices for product
  $tier_price = $product->getData('tier_price');
  $price = $product->getPrice ();
  $priceArray[1] = array("price" => round((float) $price,2), "price_qty" => 1);

  foreach($tier_price as $entry){
    $priceArray[intval($entry["price_qty"])] = array("price" => round((float)$entry["price"],2), "price_qty" => intval($entry["price_qty"]));

  }

  $output['price'] = $priceArray;
  $output['options'] = $options;

  $json = json_encode($output);
  echo $json;
}
else {
  echo "Get-Parameter productId is missing";
}
?>

http://www.birchler-media.ch/ajax-get-magento-product-details-as-json/

No comments: