Category Archives: Magento

Magento collection set page size


Magento collection set page size : You can set page size in magento as below.


Magento collection set page size Syntax

$collection = Mage::getModel('module_name/model_name')
->getCollection()
->setPageSize('1');

The above filter will select only one record from the collection.

Magento set order in collection


Magento set order in collection : You can set order in magento table as below.


Magento set order in collection Syntax

$collection = Mage::getModel('module_name/model_name')
->getCollection()
->setOrder('id DESC');

The above filter will sort magento collection by id in descending order.

Magento Load product by id


Magento Load product by id : You Can Load Magento product by Id As Below


Magento Load product by id Syntax


         $product = Mage::getModel('catalog/product')->load($id);  
         

Which will load product by id passed.

Magento create custom log file


Magento create custom log file : Magento provide to create logs functionality. Default logs are stored in system.log file which is created in folder var/log/ststem.log. Magento’s defualt warnings and error messages are stored in the same file. We are going to explain the both default and custom error log messages. You can create log in magento simply as below :


Magento create custom log file Example

Magento Create Custom log file Example

Mage::log('Your Error Message here.....', null, 'logfilename.log');

The log method accepts three parameters as input parameter to create custom log file. First parameter is message to be logged, second parameter is null and third is file name you want to create and log the message. This file will be create in the same folder system.log file exists.

Note : This file will be created in folder “var/log”.


Magento Default Log File – system.log

Let’s have look over magento default logging file system.log. All the logs created by system are stored in this file.


Magento Create Log in – system.log

Here is simple syntax you can use to create your log in system.log file-

Magento system.log file Example

Mage::log('Your Data here....');

This will append the above message in magento system.log file.

Tip : If you working with system exceptions logging, prefer to create logs in system.log file.

Magento call model method in controller


Magento call model method in controller


Magento call model method in controller

You can call the model method in controller simply as

Mage::getModel('module_name/modelname')->yourMethodName();

Magento get single row collection


Magento get single row collection : You Can Get Single row collection as below.


Magento get single row collection Syntax

$collection  = Mage::getModel('module/model')->getCollection();
              $collection->addFieldToFilter('column_name', $value)->getFirstItem();
              $data = $collection->getData();
              print_r($data);

Which will load single row.

Magento 2 get Collection First Item

Magento new column added not saving value in table


Magento new column added not saving value in table.


Magento new column added not saving value in table

Quick Fix : Clear Cache – clear the /var/cache folder

This issue basically occurs when you add a new column and try to save the value in that column. The Value is not saved in table column because the tables are cached in magento even if you have disabled the magento cache because of zend framework table cache.

Magento Load template in template


Magento Load template in template : You can load template in template directly
as following.


Magento Load template in template

Mage::app()->getLayout()
                        ->createBlock('core/template')
                        ->setData('yourData',$yourData)
                        ->setTemplate('yourTemplatePath/yourTemplateName.pthml')->toHtml();

You can get the data passed to your template “yourTemplateName.pthml” data as below:

$data = $this->getData('yourData');

Magento get config variable value


Magento get config variable value: Magento config variable values can be get as below.


Magento get config variable value Syntax

Use the following syntax to get magento admin configuration value.

$configValue = Mage::getStoreConfig('section_name/group_name/field_name');

or

$configValue = Mage::getStoreConfig('section_name/group_name/field_name',Mage::app()->getStore());

This will fetch the current store’s config value.

Note : section_name,group_name and field_name are names defined in your config.xml