Tuesday, March 17, 2015

Get Product Information Query

Attribute Code SQL Equivalent Example
eq = $collections->addAttributeToFilter('price', array('eq' => 10.00));
neq != $collections->addAttributeToFilter('price', array('neq' => 10.00));
like LIKE $collections->addAttributeToFilter('name', array('like' => '%VJTemplates%'));
nlike NOT LIKE $collections->addAttributeToFilter('name', array('nlike' => '%VJTemplates%'));
in IN () $collections->addAttributeToFilter('id', array('in' => array(1,2,3,4)));
nin NOT IN () $collections->addAttributeToFilter('id', array('nin' => array(1,2,3,4)));
is IS
notnull IS NOT NULL
$collections->addAttributeToFilter('description', 'notnull');
null IS NULL $collections->addAttributeToFilter('description', 'null');
moreq >= $collections->addAttributeToFilter('price', array('moreq' => 10.00));
gt > $collections->addAttributeToFilter('price', array('gt' => 10.00));
lt < $collections->addAttributeToFilter('price', array('lt' => 10.00));
gteq >= $collections->addAttributeToFilter('price', array('gteq' => 10.00));
lteq <= $collections->addAttributeToFilter('price', array('lteq' => 10.00));
finset FIND_IN_SET() $collections->addAttributeToFilter('custom', array('finset' => '1'));
from >= $collection->addAttributeToFilter('created_at', array(
'from' => '10 July 2013',
'to' => '11 July 2013',
to <= 'date' => true
date ));
datetime $collection->addAttributeToFilter('created_at', array(
'from' => '2013-01-01 00:00:00',
'to' => '2013-12-31 00:00:00',
'datetime' => true
));

Get a list of attributes of a product:

$attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
echo '<pre>' . print_r($attributes, TRUE) . '</pre>';

AND Operation:

$collections = Mage::getModel('catalog/product')
       ->getCollection()
       ->addAttributeToSelect(array('id', 'sku', 'name', 'price'))
       ->addAttributeToFilter('name', array('eq' => 'test car'))
       ->addAttributeToFilter('price', array('eq' => 10.00))
       ->load();

OR Operation:

$collections = Mage::getModel('catalog/product')
       ->getCollection()
       ->addAttributeToSelect(array('id', 'sku', 'name', 'price'))
       ->addAttributeToFilter(
         array(
           array('attribute' => 'name', 'eq' => 'test car'),
           array('attribute' => 'sku', 'regexp' => '^ABC'),
         )
        )
       ->load();

Looping through the collection:

foreach ($collection as $prodObj) {
  echo $prodObj->getId() . ' ';
  echo $prodObj->getSku() . ' ';
  echo $prodObj->getName() . ' ';
  echo $prodObj->getPrice() . ' ';
}

Reference:

http://www.vjtemplates.com/blog/magento/addattributetoselect-addattributetofilter-addfieldtofilter

No comments: