2014-06-27

Determine page type in magento

For category page (in layout xml file):

 
  
   category
  
 

And in your block class you can get value of page type:
$this->setPageType()
For other pages you can do the same - just replace catalog_category_view with your action.

2014-06-23

Add column in magento install/upgrade script

For adding a new column to table in magento installer you have to add to your install/upgrade script:
$installer = $this;
$installer->startSetup();
// ...
$installer->getConnection()->addColumn(
    $this->getTable('sales/quote_payment'),
    'billing_code', // column name
    'VARCHAR(255) NOT NULL' // type definition
);
// ...
$installer->endSetup();

2014-06-09

Rewrite controllers in magento

For rewriting controller in magento you have to add to your module config.xml:

 
  
   
    
     SliRx_Test
    
   
  
 

where Mage_Checkout - module with a controller which you are going to rewrite.
SliRx_Test - your module which contains the controller for rewrite.

The above config means that magento will search the controller in your module first and after that, if doesn't find it, in Mage_Checkout.
Then you have to create the file SliRx/Test/controllers/CartController.php:
require_once(Mage::getModuleDir('controllers', 'Mage_Checkout') . DS . 'CartController.php');

class SliRx_Test_CartController extends Mage_Checkout_CartController
{
    public function indexAction()
    {
        echo "it's cart!";
    }
}
But magento doesn't know about Mage_Checkout_CartController and you must include the file with this class by yourself. That's why before declaring SliRx_Test_CartController calls require_once;

Debug layout updates in magento

For each request magento sees all layout updates (located at design layout folder) and find the appropriate handles. To get list of handles which appropriate to the current request you have to write in controller:
$this->loadLayout();
echo '<pre>';
print_r($this->getLayout()->getUpdate()->getHandles());
echo '</pre>';
exit;
The result will be something like that:
Array
(
    [0] => default
    [1] => STORE_default
    [2] => THEME_frontend_default_default
    [3] => catalog_category_view
    [4] => customer_logged_out
)
Then magento merges all layout updates for handles in the above array to one xml file. To show this file you have to write in controller (at the beginning of method):
header('Content-type: text/xml');
$this->loadLayout();
echo $this->getLayout()->getNode()->asXML();
exit;

2014-06-08

Possible values for addAttributeToFilter

Collections in Magento have the method addAttributeToFilter. It filters entities.
This method takes an attribute code and a condition.
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('sku', 'test-sku');
$products->load();
Where 'test-sku' - value of condition.
If value of condition is integer or string - exact value will be filtered with 'eq' condition).
If value of condition is array - one of the following structures is expected:
array("from" => $fromValue, "to" => $toValue)
array("eq" => $equalValue)
array("neq" => $notEqualValue)
array("like" => $likeValue)
array("in" => array($inValues))
array("nin" => array($notInValues))
array("notnull" => $valueIsNotNull)
array("null" => $valueIsNull)
array("moreq" => $moreOrEqualValue)
array("gt" => $greaterValue)
array("lt" => $lessValue)
array("gteq" => $greaterOrEqualValue)
array("lteq" => $lessOrEqualValue)
array("finset" => $valueInSet)
array("regexp" => $regularExpression)
array("seq" => $stringValue)
array("sneq" => $stringValue)

2014-06-07

Rewrite class/method in Magento

To rewrite class method you have to add rewrite config to etc/config.xml:

 
  
   
    
     SliRx_Test_Model_Category
    
   
  
 

Where tags "catalog" and "category" are assigned to Mage::getModel('catalog/category')
Than create corresponding class at app/code/local/SliRx/Test/Model/Category.php:
class SliRx_Test_Model_Category extends Mage_Catalog_Model_Category
{
    public function getChildren()
    {
        return $this->getResource()->getChildren($this, false);
    }
}

2014-06-05

Insert tabs in magento 1.9 responsive theme

For creating tabs you need to write some html:
Title 1
Content html 1
Title 2
Content html 2
Where ajax-tabs - your own class.
And include styles in your scss file:
@include bp(min-width, $bp-medium + 1) {
    .ajax-tabs {
        @include tabs;
    }
}

@include bp(max-width, $bp-medium) {
    .ajax-tabs {
        @include accordion;
        @include accordionCollapse;
    }
}