Monday, August 22, 2016

Unset Sorting & Disable Cache for Newest Product Category

Recently we had a requirement to always show a particular Sorting Option (Newest Products - news_from_date) for a particular category page. (New Products)


We faced two issues.

(1) Sorting is Not always set to "newest products" sorting option when pointing back from different category.

This occurs when a customer selects a different sorting option on different category and moving back to Newest Product Category, products will sort according to the previously selected sorting option.

(2) Sorting is Not reset even after clearing parameters passing in Url on Newest Product Category.

Ex:
http://site_url/newest/?dir=asc&order=position

Even after clearing the GET params passing over URL, the sorting is Not reset for "newest_from_date" option.

These issue are specific to Magento Enterprise edition.

We can simply overcome the above two issues with following development.

In app/code/local/<package>/<module_name>/etc/config.xml file:
(app/code/local/ISM/New/etc/config.xml)
 <config>   
    <frontend>
        <events>
            <core_block_abstract_to_html_before>
                <observers>
                    <ism_sorting_new_category>
                        <class>ism_new/observer</class>
                        <method>unsetSortingNewCategory</method>
                    </ism_sorting_new_category>
                </observers>
            </core_block_abstract_to_html_before>
            <controller_action_postdispatch_catalog_category_view>
                <observers>
                    <ism_disable_cache_for_new_category>
                        <class>ism_new/observer</class>
                        <method>disableCacheForNewCategory</method>
                    </ism_disable_cache_for_new_category>
                </observers>
            </controller_action_postdispatch_catalog_category_view>
        </events>
    </frontend>
</config>


In app/code/local/<package>/<module_name>/Model/Observer.php file:
(app/code/local/ISM/New/Model/Observer.php)
/** 
* Disable parameter memorizing and unset sorting order & category for New(Nieuwe) category 
* @param Varien_Event_Observer $observer 
* @return $this 
*/
public function unsetSortingNewCategory(Varien_Event_Observer $observer)
{
    $block = $observer->getBlock();
    if ($block instanceof Mage_Catalog_Block_Product_List) {
        $category = Mage::registry('current_category');
        if ($category && $category->getDefaultSortBy() == 'news_from_date') {
            $sortingOrder = Mage::app()->getRequest()->getParam('order', false);
            $sortingDirection = Mage::app()->getRequest()->getParam('dir', false);

            if (!$sortingOrder && !$sortingDirection) {
                Mage::getSingleton('catalog/session')->unsSortOrder();
                Mage::getSingleton('catalog/session')->unsSortDirection();
            }
        }
    }

    return $this;
}

/** 
* Disable Cache for New (Nieuwe) category 
* @param Varien_Event_Observer $observer 
* @return $this 
*/
public function disableCacheForNewCategory(Varien_Event_Observer $observer)
{
    $category = Mage::registry('current_category');

    if ($category && $category->getDefaultSortBy() == 'news_from_date') {
        $request = $observer->getEvent()->getControllerAction()->getRequest();
        $request->setParam('no_cache', true);
    }

    return $this;
}




Unset Sorting & Disable Cache for Newest Product Category

Recently we had a requirement to always show a particular Sorting Option (Newest Products - news_from_date) for a particular category page. (New Products)


We faced two issues.

(1) Sorting is Not always set to "newest products" sorting option when pointing back from different category.

This occurs when a customer selects a different sorting option on different category and moving back to Newest Product Category, products will sort according to the previously selected sorting option.

(2) Sorting is Not reset even after clearing parameters passing in Url on Newest Product Category.

Ex:
http://site_url/newest/?dir=asc&order=position

Even after clearing the GET params passing over URL, the sorting is Not reset for "newest_from_date" option.

These issue are specific to Magento Enterprise edition.

We can simply overcome the above two issues with following development.

In app/code/local/<package>/<module_name>/etc/config.xml file:
(app/code/local/ISM/New/etc/config.xml)
 <config>   
    <frontend>
        <events>
            <core_block_abstract_to_html_before>
                <observers>
                    <ism_sorting_new_category>
                        <class>ism_new/observer</class>
                        <method>unsetSortingNewCategory</method>
                    </ism_sorting_new_category>
                </observers>
            </core_block_abstract_to_html_before>
            <controller_action_postdispatch_catalog_category_view>
                <observers>
                    <ism_disable_cache_for_new_category>
                        <class>ism_new/observer</class>
                        <method>disableCacheForNewCategory</method>
                    </ism_disable_cache_for_new_category>
                </observers>
            </controller_action_postdispatch_catalog_category_view>
        </events>
    </frontend>
</config>


In app/code/local/<package>/<module_name>/Model/Observer.php file:
(app/code/local/ISM/New/Model/Observer.php)
/** 
* Disable parameter memorizing and unset sorting order & category for New(Nieuwe) category 
* @param Varien_Event_Observer $observer 
* @return $this 
*/
public function unsetSortingNewCategory(Varien_Event_Observer $observer)
{
    $block = $observer->getBlock();
    if ($block instanceof Mage_Catalog_Block_Product_List) {
        $category = Mage::registry('current_category');
        if ($category && $category->getDefaultSortBy() == 'news_from_date') {
            $sortingOrder = Mage::app()->getRequest()->getParam('order', false);
            $sortingDirection = Mage::app()->getRequest()->getParam('dir', false);

            if (!$sortingOrder && !$sortingDirection) {
                Mage::getSingleton('catalog/session')->unsSortOrder();
                Mage::getSingleton('catalog/session')->unsSortDirection();
            }
        }
    }

    return $this;
}

/** 
* Disable Cache for New (Nieuwe) category 
* @param Varien_Event_Observer $observer 
* @return $this 
*/
public function disableCacheForNewCategory(Varien_Event_Observer $observer)
{
    $category = Mage::registry('current_category');

    if ($category && $category->getDefaultSortBy() == 'news_from_date') {
        $request = $observer->getEvent()->getControllerAction()->getRequest();
        $request->setParam('no_cache', true);
    }

    return $this;
}




Sunday, April 10, 2016

Set different sorting direction for a Category

In here following code can be used for 2 purposes.
  1. Set default direction of the Sorting on a particular Category
  2. Disable parameter memorizing and reset sorting when moving to different Category
NOTE:
Following options will NOT work when Full Page Cache (FPC) is enabled in Enterprise version.
This issue will address on different blog post.

Option 01:
Put the following code either in local.xml or catalog.xml

<layout version="0.1.0">
    <default>
        <catalog_category_default translate="label">
            <reference name="product_list_toolbar">
                <action method="setDefaultDirection"><string>desc</string></action>
                <action method="disableParamsMemorizing" />
            </reference>
        </catalog_category_default>
    </default>
</layout>


Option 02:
Go to Catalog => Categories and select the Category you want to set default direction. Under "Custom Design => Custom Layout Update" add the following content.
<reference name="product_list_toolbar">
    <action method="setDefaultDirection"><string>desc</string></action>
    <action method="disableParamsMemorizing" />
</reference>


Monday, February 15, 2016

Cache Types in Magento 2

Current version of Magento 2 works with following types of Caches:


  1. Configuration (config) cache:
    This cache appears when Magento gathers configuration from all its modules, merges it, and saves to the cache. Additionally, it includes store-specific settings from the file system and database. You should flush or clean this type of cache after changing configuration files.
  2. Layout (layout) cache:
    This consists of compile page layouts. This cache requires flushing layout files.
  3. Block HTML output cache (block_html):
    This one is about HTML fragmanets per each block. Clear this cache after changing the view layer.
  4. DDL cache (db_ddl):
    This is about database schema. It can be clean up automatically as well. It is posible to put any data in any segment of db_ddl cache. Cache should be flushed after custom changes to DB schema.
  5. Entity-attribute-value (eav) cache:
    This cache includes Meta-data related to EAV attributes.
    Ex:
    Store labels, search settings, attribute renderings etc.
  6. Full page cache (full_page):
    This occurs due to generate of HTML pages. It is automatically clean by Magento an can be modify by third party developers.
  7. Translations cache (translate):
    This consists of translations from all modules.
  8. Integration configuration  cache (config_integration):
    This is related to compiled integration. Clean this after adding new integration or modifying existing ones.
Web services configuration cache (config_webservice): Cache of the web API structure.

Pre-requisites:
- Go to bin folder
> cd <amagento_root>/bin

- To check the cache status:
> magento cache:status

- To enable / disable cache types
> magento cache:enable [cache_type]
> magento cache:disable [cache_type]

- To clean the cache
> magento cache:clean [type]

- To flush the cache
> magento cache:flush [type]

Cache types:
layout
block_html
collections
db_ddl
eav
full_page
translate
config_integration
config_integration_api
config_webservice



Tuesday, February 9, 2016

Add Column to existing Table

Table Name: ism_storelocator/location
Setup Script name: mysql4-upgrade-1.0.10-1.0.11.php
<?php$installer = $this;

$installer->startSetup();

$installer->getConnection()->addColumn(
    $this->getTable('ism_storelocator/location'),
    'default_url',
    'varchar(200)');

$installer->endSetup();

Monday, January 18, 2016

Adding CMS Static Block to Footer

Add following content on local.xml


<layout>
    <default>
        <reference name="head">
            <action method="addItem">
              <type>skin_js</type><name>js/ism/string-truncate.js</name>
           </action>
        </reference>
        <reference name="footer">
            <block type="cms/block" name="footer_shipping_icon" as="footer_shipping_icon">
                <action method="setBlockId">
                    <block_id>footer_shipping_icon</block_id>
                </action>
            </block>
        </reference>
    </default>
</layout>
Call the CMS/Block on PHTML file. (ex: footer.phtml)
<div class="footer-shipping-icon">
    <?php echo $this->getChildHtml('footer_shipping_icon'); ?></div>

Wednesday, January 13, 2016

Delete Product Attribute Programatically

<?phpdefine('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
Mage::app();

echo "Begining...."."\n";
try{
$model = new Mage_Eav_Model_Entity_Setup('core_setup');
// your attribute code here
$attribute_id = 'salelabel'
// Remove Product Attribute
$model->removeAttribute('catalog_product', $attribute_id);
echo "Done"."\n";
}catch(Exception $e)
{
  echo "An exception occured.".$e->getMessage();      
}
echo "Ending...."."\n";