Search API magento

Search API magento

Search API magento
<?php
error_reporting(0);
include("../app/Mage.php");
umask(0);
Mage::app();
$result_array = array();
$result_array_listproduct = array();
$result_array_listproduct1 = array();
$searchstring=$_POST['product_search'];


if ($searchstring=="" || $searchstring===null){
    $result_array["message"]="Please Insert Some Character";
    $result_array["status"]="2";

}else{
$product_collection = Mage::getResourceModel('catalog/product_collection')
                  ->addAttributeToSelect('*')
                  ->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'))
                  ->addAttributeToFilter('status', array('status' => 1))
                  ->load();

foreach ($product_collection as $product) {
         $ids[] = $product->getId();
     }

for($i=0;$ids[$i];$i++)
{
    $_product = Mage::getModel('catalog/product')->load($ids[$i]);

    $product_visi=$_product->getVisibility();

    if($product_visi == "4"){

    
      $result_array_listproduct["product_id"]=$product_id=$_product->getId(); // product id
      $result_array_listproduct["product_name"]=$product_name=$_product->getName(); //product name
      $result_array_listproduct["product_image"]=$product_image=$_product->getImageUrl(); //product's image url
      $result_array_listproduct["product_small_image"]=$product_small_image=$_product->getSmallImageUrl(); //product's small image url
      $result_array_listproduct["product_thumbnail_image"]=$product_thumbnail_image=$_product->getThumbnailUrl();
      $result_array_listproduct["product_all_images"]=$product_all_image=array();
        $images='';// every time images empty
        foreach ($_product->getMediaGalleryImages() as $image) {
            $images[]=$image->getUrl();
        }
        array_push($result_array_listproduct["product_all_images"],$images);//all images push in array
      $result_array_listproduct["product_regular_price"]=$product_regular_price=$_product->getPrice(); //product's regular Price
      $result_array_listproduct["product_special_price"]=$product_special_price=$_product->getSpecialPrice();
      $result_array_listproduct["sku"]=$sku=$_product->getSku();
      $result_array_listproduct["product_short_description"]=$product_short_description=htmlentities(preg_replace('/"/',' ',$_product->getShortDescription())); //product's short description
      $result_array_listproduct["product_long_description"]=$product_long_description=htmlentities(preg_replace('/"/',' ',$_product->getDescription())); // product's long description
      $result_array_listproduct["product_qty"]=$product_qty=$_product->getStockItem()->getQty();// get product Qty
      $result_array_listproduct["product_attribute"]=$product_attribute=$_product->getAttributeText('color'); // get product attribute color
      $result_array_listproduct["product_type"]=$product_type=$_product->getTypeId();
      $result_array_listproduct["product_status"]=$product_status=$product->getStatus();
      $result_array_listproduct["product_visibility"]=$product_visi=$_product->getVisibility();
      $result_array_listproduct["asociated"]= array();

      if($product_type=="configurable"){
      $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
      $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
        foreach($simple_collection as $simple_product){
          $result_array_associated["product_id"]= $simple_product->getId();
          $result_array_associated["product_name"]=$simple_product->getName();
          $result_array_associated["product_image"]=$simple_product->getImageUrl();
          $result_array_associated["product_small_image"]=$simple_product->getSmallImageUrl();
          $result_array_associated["product_thumbnail_image"]=$simple_product->getThumbnailUrl();
          $result_array_associated["product_regular_price"]=$simple_product->getPrice();
          $result_array_associated["product_special_price"]=$simple_product->getSpecialPrice();

          $result_array_associated["sku"]=$simple_product->getSku();
          $result_array_associated["product_short_description"]=htmlentities(preg_replace('/"/',' ',$simple_product->getShortDescription()));
          $result_array_associated["product_long_description"]=htmlentities(preg_replace('/"/',' ',$simple_product->getDescription()));
          $result_array_associated["product_qty"]=$simple_product->getStockItem()->getQty();
          $result_array_associated["product_attribute"]=$color=$simple_product->getAttributeText('color');
          $result_array_associated["option_id"]= $simple_product->getResource()->getAttribute(92)->getSource()->getOptionId($color);
          $result_array_associated["product_type"]=$simple_product->getTypeId();
          $result_array_associated["product_status"]=$simple_product->getStatus();
          $result_array_associated["product_visibility"]= $simple_product->getVisibility();
          array_push($result_array_listproduct["asociated"],$result_array_associated);

        }
      }
          array_push($result_array, $result_array_listproduct);
    }
}

if($product_name===null){
    $result_array_listproduct["message"]="Your Product is not avilable ";
    $result_array_listproduct["status"]="0";
    array_push($result_array, $result_array_listproduct);
    }else{

        $result_array_listproduct1["message"]="sucess";
        $result_array_listproduct1["status"]="1";
        array_push($result_array, $result_array_listproduct1); 
    }
}
echo json_encode($result_array);
?>

No comments:

Powered by Blogger.