Mouad Dida
Mouad Dida

Reputation: 1

how to get product image in autocomplete list in magento

Well my question is How I can get a product image just near the product title in my autocomplete list in magento.

I did many searches and most of answer are by adding this line of code:

    $imageUrl = Mage::helper('catalog/image')->init($item, 'image'); 

but it doesnt work and give me as url of all product :

catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/image.jpg

the function getSuggestData():

public function getSuggestData()
{

if (!$this->_suggestData) {
    $collection = $this->helper('catalogsearch')->getSuggestCollection();
    $query = $this->helper('catalogsearch')->getQueryText();
    $counter = 0;
    $data = array();
    foreach ($collection as $item) {
    $imageUrl = Mage::helper('catalog/image')->init($item, 'image');          
      $_data = array(
            'title' => $item->getQueryText(),
            'row_class' => (++$counter)%2?'odd':'even',
            'num_of_results' => $item->getNumResults(),
            'url' => $imageUrl
        );

        if ($item->getQueryText() == $query) {
            array_unshift($data, $_data);
        }
        else {
            $data[] = $_data;
        }
    }



    $this->_suggestData = $data;
}
return $this->_suggestData;
}

Thanks for help

Upvotes: 0

Views: 672

Answers (1)

In your Magento autocomplete search results, it appears that you are attempting to show the product image next to the product title. However, you are receiving a placeholder image URL (placeholder/image.jpg), which implies that either: There isn't a picture set for the product. It appears that the product picture is not being retrieved correctly by the Mage::helper('catalog/image')->init($item, 'image'). The answer The product's actual image attribute must be used to correctly retrieve the product image URL. Try making the following changes to your getSuggestData() function:

public function getSuggestData()
{
    if (!$this->_suggestData) {
        $collection = $this->helper('catalogsearch')->getSuggestCollection();
        $query = $this->helper('catalogsearch')->getQueryText();
        $counter = 0;
        $data = array();

        foreach ($collection as $item) {
            $product = Mage::getModel('catalog/product')->load($item->getId()); // Load product data
            $imageUrl = Mage::getModel('catalog/product_media_config')->getMediaUrl($product->getImage());

            // Check if product has a valid image
            if (!$product->getImage() || $product->getImage() == 'no_selection') {
                $imageUrl = Mage::getDesign()->getSkinUrl('images/catalog/product/placeholder/image.jpg'); // Default placeholder image
            }

            $_data = array(
                'title' => $item->getQueryText(),
                'row_class' => (++$counter) % 2 ? 'odd' : 'even',
                'num_of_results' => $item->getNumResults(),
                'image' => $imageUrl // Use image key instead of url for clarity
            );

            if ($item->getQueryText() == $query) {
                array_unshift($data, $_data);
            } else {
                $data[] = $_data;
            }
        }

        $this->_suggestData = $data;
    }
    return $this->_suggestData;
}

Once this function returns the correct data, you need to adjust your frontend autocomplete template (probably in autocomplete.phtml) to display the image.

<ul>
    <?php foreach ($suggestData as $item): ?>
        <li class="<?php echo $item['row_class']; ?>">
            <img src="<?php echo $item['image']; ?>" alt="<?php echo $item['title']; ?>" width="50" height="50"/>
            <span><?php echo $item['title']; ?></span>
        </li>
    <?php endforeach; ?>
</ul>

Upvotes: 0

Related Questions