-1

Can anyone explain why my code gives zero results?

I am passing an attribute id from external js file to Magento. I am successfully getting the data, but the problem is, an empty page is occurring while getting the product details for corresponding attribute value.

My code:

require_once 'app/Mage.php';

Mage::app();
$id = Mage::app()->getRequest()->getParam('id');
echo $id;
$products = Mage::getModel('catalog/product')->getCollection();
        $products->addAttributeToSelect('menu')
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('price')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('sku')
                ->addAttributeToSelect('description')
                ->addAttributeToSelect('short_description');
        $products->addFieldToFilter(
            array(
            array('attribute'=>'menu', 'eq'=> $id)     
        )
        );
        foreach ($products as $product) {
            $details[] = array('Name'=>$product->getData('name'),
        'Price' => $product->getData('price'),
        'Image' => $product->getData('image'),
        'Sku' => $product->getData('sku'),
        'Description' =>$product->getData('description'),
        'short_description' => $product->get_data('short_description'),
        'id' => $product->get_data('attribute_set_id'),
        );
        }
        print_r($details); 

The output is an empty array.

If I directly pass the value, it gives an output:

$products = Mage::getModel('catalog/product')->getCollection();
        $products->addAttributeToSelect('menu')
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('price')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('sku')
                ->addAttributeToSelect('description')
                ->addAttributeToSelect('short_description');
        $products->addFieldToFilter(
            array(
            array('attribute'=>'menu', 'eq'=> 243)     
        )
        );

Am I retrieving the GET parameter correctly?

  • did u print this collection $products and check if it rendered or not.? – Manashvi Birla Jun 25 '15 at 7:10
  • @Manashvibirla yes i did...it is not rendering data... – Astro Jun 25 '15 at 7:12
  • print this collection after applying the filters – Manashvi Birla Jun 25 '15 at 7:13
  • 1
    use trim($id)..'eq'=> trim($id) .May be spacing issue – Amit Bera Jun 25 '15 at 7:16
  • @AmitBera but $id is not a string – Astro Jun 25 '15 at 7:18
1

Get the id value by following code

$id = (int) Mage::app()->getRequest()->getParam('id');

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.