1

I want when i click on the anchor(a) tag means image it redirect me the product detail page what should i do for it?

Here is my code.

 <?php
    include 'app/Mage.php';
    Mage::app();
    $resource = Mage::getSingleton('core/resource');
    $readConnection = $resource->getConnection('core_read');
    $writeConnection = $resource->getConnection('core_write');

$hint = "hp";
$sql = "SELECT p.entity_id, pv.value as name, pt.value as description, pm.value as imagesPath
                FROM catalog_product_entity as p
                INNER JOIN catalog_product_entity_varchar as pv on pv.entity_id = p.entity_id and pv.attribute_id = 71
                INNER JOIN catalog_product_entity_text as pt on pt.entity_id = p.entity_id and pt.attribute_id = 72
                LEFT JOIN catalog_product_entity_media_gallery as pm on pm.entity_id = p.entity_id and pm.attribute_id = 88
                 WHERE pv.value like '%".$hint."%'";

               foreach($readConnection->fetchAll($sql) as $orders)
                  {

                     $id1 = $orders["entity_id"];
                     $result = $orders['name'];
                     $result1 = $orders['description'];
                     $result2 = $orders['imagesPath'];

                     echo "<ul><li>$result</li>
                     <li>$result1</li> </ul>";

                    ?>
                    <a href="#?id=<?php echo $id1 ?>"><img src="media/catalog/product<?php echo $result2; ?>" width="50px" height="50px;" /></a>

                     <?php

                  }

 ?>
4
  • why you are using sql query? – Gopal Patel Dec 9 '16 at 9:49
  • so what should i use? – Afzal Khan Dec 9 '16 at 10:01
  • basically you want retrieve all product which contain 'hp' in their name? – Gopal Patel Dec 9 '16 at 10:13
  • yes youare right but in search box. – Afzal Khan Dec 10 '16 at 6:10
2

just use below code

$productCollection = Mage::getModel('catalog/product')
    ->getCollection() 
    ->addAttributeToFilter('name', array('like' => '%hp%'))
    ->addAttributeToSelect('*');

foreach ($productCollection as $product) {
    //echo $product->getSku();
    echo "<ul><li>".$product->getName()."</li>
            <li>".$product->getDescription()."</li> </ul>";
    echo '<a href="'.Mage::getStoreConfig(Mage_Core_Model_Url::XML_PATH_SECURE_URL).'index.php/'.$product->getUrlPath().'"> <img src="'.Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product/'.$product->getImage().'"></a>';
}

Above code is tested

For product image you can use below code

Mage::getModel('catalog/product_media_config')
        ->getMediaUrl( $product->getImage() );
8
  • Oh thank you so much dear its working perfect Thank You – Afzal Khan Dec 10 '16 at 6:14
  • but it is not picking the image . – Afzal Khan Dec 10 '16 at 7:30
  • @AfzalKhan find updated answer – Gopal Patel Dec 10 '16 at 7:35
  • not working :( .. – Afzal Khan Dec 10 '16 at 8:01
  • @AfzalKhan its working at my side... well will you share output for one product – Gopal Patel Dec 10 '16 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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