Thursday, November 21, 2013

Magento : AddAttributeToFilter( Contionals - Getting products from the table in magento commerce



AddAttributeToFilter( Contionals - Getting products from the table in magento commerce ADDATTRIBUTETOFILTER CONDITIONALS IN MAGENTO

addAttributeToFilter is a function that can be called on a product collection in Magento. In short, it adds a condition to the WHERE part of the MySQL query used to extract a product collection from the database.




1
2
3
4
$_products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image'))
   ->addAttributeToFilter('sku', array('like' => 'UX%'))
    ->load();




The above code would get a product collection, with each product having it's name, url, price and small image loaded in it's data array. The product collection would be filtered and contain only products that have an SKU starting with UX.
addAttributeToFilter Conditionals
Notice above, I used the LIKE operator There are many more operators in SQL and addAttributeToFilter will accept them all. I include them below as well as a reference for you. Hopefully this will save you some time.
Equals: eq




1
$_products->addAttributeToFilter('status', array('eq' => 1));




Not Equals - neq




1
$_products->addAttributeToFilter('sku', array('neq' => 'test-product'));




Like - like




1
$_products->addAttributeToFilter('sku', array('like' => 'UX%'));




One thing to note about like is that you can include SQL wildcard characters such as the percent sign.
Not Like - nlike




1
$_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));




In - in




1
$_products->addAttributeToFilter('id', array('in' => array(1,4,98)));




When using in, the value parameter accepts an array of values.
Not In - nin




1
$_products->addAttributeToFilter('id', array('nin' => array(1,4,98)));




NULL - null




1
$_products->addAttributeToFilter('description', 'null');




Not NULL - notnull




1
$_products->addAttributeToFilter('description', 'notnull');




Greater Than - gt




1
$_products->addAttributeToFilter('id', array('gt' => 5));




Less Than - lt




1
$_products->addAttributeToFilter('id', array('lt' => 5));






Greater Than or Equals To- gteq




1
$_products->addAttributeToFilter('id', array('gteq' => 5));




Less Than or Equals To - lteq




1
$_products->addAttributeToFilter('id', array('lteq' => 5));




addFieldToFilter()
Debugging The SQL Query
There are two ways to debug the query being executed when loading a collection in Magento.




1
2
3
4
5
6
7
// Method 1
Mage::getModel('catalog/product')->getCollection()->load(true);
// Method 2 (Quicker, Recommended)
$collection = Mage::getModel('catalog/product')->getCollection();
echo $collection->getSelect();




Both method 1 and method 2 will print out the query but both will do it in slightly different ways. Method 1 prints the query out as well as loading the products while method 2 will just convert the query object to a string (ie. will print out the SQL). The second method is definitely better as it will be executed much

By PHP with 64 comments

Thursday, October 10, 2013

Magento Error - There has been an error processing your request Exception printing is disabled by default for security reasons. Error log record number: xxxxxxxxx

When coding Magento module, mostly you will face following error:

There has been an error processing your request

Exception printing is disabled by default for security reasons.
Error log record number: xxxxxxxxx
To view more informative error, you can follow this steps:
Go to folder /errors/
Change local.xml.sample to local.xml
after this change you can see few more errors.
then Open /lib/Zend/Cache/Backend/File.php
and search
1
2
protected $_options = array(
'cache_dir' => 'null',
and change it to:
1
2
protected $_options = array(
'cache_dir' => 'tmp/',
Save the changes.
create tmp folder under root magento folder.
You are Done.

=========================

Magento install error - Exception printing is disabled

Here is a known error which can occur when installing Magento:
There has been an error processing your request
Exception printing is disabled by default for security reasons.
Error log record number: XXXXXXXXXXXXXXX
Here is the solution:
  1. Navigate to the "errors" folder.
  2. Change local.xml.sample to local.xml
  3. You should now see a new list of crazy errors all over the Magento page - this is okay.
  4. Open magento/lib/Zend/Cache/Backend/File.php and look for:
    protected $_options = array(
    'cache_dir' => 'null',
  5. Change it to:
    protected $_options = array(
    'cache_dir' => 'tmp/',
  6. Save it.
  7. Now the final step is to go create a tmp folder in the root Magento folder.
  8. That's it.

============================= ===============================


Here i shared some useful magento code, 
i think this code is useful sometime. This details learned it from 
some otherwebsite and some points i had been worked out. 
what i learned/worked, i would like to share to all. In case any issue or 
wrong code if you find it from below, please excuse.

1. How top get Session details ?

<?php
$sessionId  = Mage::getModel('core/session')->getSessionId();
$customerId = Mage::getModel('customer/session')->getCustomerId();
$vistitorId = Mage::getModel('core/session')->getVisitorId();
?>

2.How to get Store details?

<?php
$storeId     = Mage::app()->getStore()->getId();
$storeName   = Mage::app()->getStore()->getName();
$storeCode   = Mage::app()->getStore()->getCode();
$groupId     = Mage::app()->getStore()->getGroupID();
$groupName   = Mage::app()->getStore()->getGroup()->getName();
$websiteName = Mage::app()->getWebsite()->getName();
?>

3. How to remove index.php from Magento URLs?

if you are using Magento and your store urls are looking this way,
http://www.example.com/index.php/toys/toy.html (This is example URL)
So, you must be looking, how to remove that index.php from the middle, 
as that’s not good for Search Engine Indexing at all. 
So, here is the trick.

Step: 1 Open your site root folder and you can find the .htaccess file. 
Just edit that. Open it on a text editor and find this line, 
#Rewrite Base /magento/.  Just replace it with Rewrite Base /

Step: 2 Then go to your Admin Panel and enable the Web Server Rewrites. 
You can find it at System > Configuration > Web > Search Engine 
Optimization.

Step: 3 Then goto your Cache Management page 
( System > Cache Management) and 
refresh your Cache and also refresh the Web Redirects.
Thats all. Test at Frontend now.

4. Exception printing is disabled by default for security reasons  Error in Magento :

If we face any issue like as follow
Issue:
Exception printing is disabled by default for security reasons
Error log record number: *********
here has been an error processing your request
Solutions
In this case we need to check following steps.
Step 1 : Go to folder /errors/
Step 2 : Change local.xml.sample to local.xml
Step 3 : after this change you can see few more errors.
then Open magento/lib/Zend/Cache/Backend/File.php
and search 
From 
protected $_options = array(
'cache_dir' => 'null', 
To
protected $_options = array(
'cache_dir' => 'tmp/', 
Save the changes.
Step 4 : create tmp folder under root magento folder.
Ref : http://www.eukhost.com/forums/f15/exception-printing-disabled-default-
security-reasons-error-log-record-numb-11362/

5. After Submit Registration form its redirect to where? 
And basic validation also has to check below file.
app/code/core/Mage/customer/controllers/AccountController.php

After submit Registration form it will display Success or failure
message from above file.

By PHP with 41 comments

    • Popular
    • Categories
    • Archives