I am trying to programatically retrieve a list of products using name attribute. I want to allow users to search by certain attribute values for example color and brand using ajax.
Here is my code
<?php
require_once 'app/Mage.php';
umask(0);
ini_set('display_errors', 1);
ini_set('max_execution_time', 3600);
Mage::app();
Mage::getSingleton('core/session', array(
'name' => 'frontend'
));
$product_search = $_REQUEST['term'];
if ($product_search) {
$productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name')->addAttributeToFilter(array(
array(
'attribute' => 'name',
'like' => '%' . $product_search . '%'
)
))->addAttributeToSelect('price')->addAttributeToSelect('thumbnail')->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left');
$imageHelper = Mage::helper('catalog/image');
foreach ($productCollection as $_product) {
$thumbail_src = "";
$url = $_product->getProductUrl();
$data = ' <div class="autocomplete-image"> <img src="' . $imageHelper->init($_product, 'thumbnail')->resize(50, 75) . '"></div> <div class="top">' . $_product->getName() . '</div><li><i class="fa fa-inr" aria-hidden="true"></i><span class="price">' . $_product->getPrice() . '</span></li> ';
$respones[] = array(
'id' => $_product->getId(),
'name' => $_product->getName(),
'data' => $data,
'price' => $_product->getPrice(),
'qty' => $_product->getQty()
);
}
echo json_encode($respones);
}
?>