0

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);
}

?> 

1 Answer 1

0

You Need to update this code and if you want to filter with new attribute code than add new attribute code into $attrArray variable.

<?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'];
$attrArray = $arrayName = array('color','brand');
if ($product_search) {
    $productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name','brand','color')->addAttributeToSelect('price')->addAttributeToSelect('thumbnail')->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left');
    $searchArray = array(
        array(
            'attribute' => 'name',
            'like' => '%' . $product_search . '%'
        )
    );
    foreach ($attrArray as $key => $code) {
        $options = getAttrOptions($code);
        $optionId=null; 
        if($options){
            foreach ($options as $key => $value) {
                if(strtolower($value['label']) == strtolower($product_search)){
                    $searchArray[] = array(
                            'attribute' => $code,
                            'eq' => $value['value'] 
                        )
                    ;
                }
            }
        }
    }
    $productCollection->addAttributeToFilter($searchArray);

    $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,
            'color' => $_product->getColor(),
            'price' => $_product->getPrice(),
            'qty' => $_product->getQty()
        );
    }

    echo json_encode($respones);
}
function getAttrOptions($code){
    $productModel = Mage::getModel('catalog/product');
    $attr = $productModel->getResource()->getAttribute($code);
    if ($attr && $attr->usesSource()) {
        return $attr->getSource()->getAllOptions();
    }
}
?> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.