Showing posts with label magento. Show all posts
Showing posts with label magento. Show all posts

2015-10-30

How to add external js to magento

Add to your layout:
<reference name="head">
   <block type="core/text" name="my.external.js">
      <action method="setText">
        <text>
           <![CDATA[<script type="text/javascript" src="https://domain.com/my_file.js"></script>]]>
        </text>
      </action>
   </block>
</reference>

2015-10-29

How to show global messages in own controller/block

Add to your controller:
$this->_initLayoutMessages('customer/session');
after loadLayout():
$this->loadLayout();
$this->_initLayoutMessages('customer/session');
$this->renderLayout();
Then in phtml:
<?= $this->getMessagesBlock()->toHtml() ?>

2015-10-15

How to create table in magento installation script

$installer = $this;

$installer->startSetup();

$table = $installer->getConnection()
    ->newTable($installer->getTable('wmpayment/webmoney_response'))
    ->addColumn('id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'identity' => true,
        'unsigned' => true,
        'nullable' => false,
        'primary'  => true,
    ), 'Id')
    ->addColumn('id_order', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'unsigned' => true,
        'nullable' => false
    ), 'Id order')
    ->addColumn('response', Varien_Db_Ddl_Table::TYPE_TEXT, null, array(
        'nullable' => false,
    ), 'Response in json format')
    ->addIndex('id_order', 'id_order');
$installer->getConnection()->createTable($table);

How to get magento config value

$value = Mage::getStoreConfig('path/to/config');

2015-10-06

Change magento configuration programmatically

$configModel = Mage::getModel('core/config');
$configModel->saveConfig('path/to/config', 'new value');

2015-02-23

Get helper from template (.phtml) in magento 2.0

Let's imagine that you have a helper app/code/SliRx/News/Helper/Date.php.
You can get the helper from template (.phtml file) like this:
$helper = $this->helper('SliRx\News\Helper\Date');

2015-02-22

Changing page layout in magento 2.0

You can change page layout by adding:
$layout = '2columns-left';
$this->pageConfig->setPageLayout($layout);
to your block's constructor.

2015-02-13

Magento google tag manager gtm

Google Tag Manager integration

With Google Tag Manager you can manage your Analytics events, Adwords conversion tracking, remarketing, etc. in one place. This module allows you to integrate Google Tag Manager to your site.
All you need is insert your container id to configuration of this extension.

 

You can get Google Tag Manager Integration GTM for magento here

 

Where to configure the extension?

System > Configuration > Google Tag Manager > Configuration.

Extension features:

  • Support multi-store
  • Support transactions (ecommerce tracking)
  • Support remarketing

Transaction features:

  • Support all product types
  • You can specify a name of your shop for each store
Remarketing allows you to show ads to your past site visitors and customize those ads based on the section of your site people visited. With dynamic remarketing, you can take this a step further, and show your site visitors an ad with the specific product they viewed on your site.

2014-08-07

Filter categories by store

Getting categories from specific store:
$storeId = 5;
$categoryId = Mage::app()->getStore($storeId)->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCollection()
->addFieldToFilter('path', array('like'=> "1/$categoryId/%"))

2014-08-01

Magento: get product qty/count/quantity

$qtyStock = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
where $product - loaded magento product (Mage_Catalog_Model_Product);

2014-07-22

Move category

$category = Mage::getModel('catalog/category')->load($idCategoryForMoving);
$category->move($idParentCategory, $idAfterCategory);

Getting filterable attributes in category

Getting filterable attributes from layered navigation
$category = Mage::registry('current_category');
$products = $category->getProductCollection();

$appliedFilters = Mage::getSingleton('catalog/layer')->getFilterableAttributes();
foreach ($appliedFilters as $filter) {
  if ($value = $this->getRequest()->getParam($filter->getAttributeCode())) {
      $products->addFieldToFilter($filter->getAttributeCode(), $value);
  }
}
Method getFilterableAttributes returns all available attributes for filtering. Then in foreach we check if an attribute has been selected by user, we add it to the filter.

2014-07-16

Include third party libraries

For including third party libraries from lib folder
require_once(Mage::getBaseDir('lib') . '/simple_html_dom.php');

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;
    }
}