SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Downloaden Sie, um offline zu lesen
EXTENDING MAGENTO
LAYERED NAVIGATION
MAGE TITANS ITALIA - MILAN, FEBRUARY 5TH 2016
Nadia Sala
Extending Magento layered navigation by Nadia Sala
ABOUT ME
PHP DEVELOPER
MAGENTO BACKEND DEVELOPER
@nadiasala
nadia.sala@bitbull.it
THE STORY
"Once upon a time there was an e-commerce website that
exposed a catalog with many descriptive information."
You can imagine a fashion catalog, so you mind about:
Product type, Occasion, Color,
Gender, Designer, Price, Size,
...and more
SCENARIO
Some information is useful for browsing catalog
Some information is useful for filtering catalog
Some information is useful for both
COMMON APPROACH
Some information is useful for browsing catalog
I can create  categories and include them in the navigation
menu
COMMON APPROACH
Some information is useful for filtering catalog
I can configure some attributes to be used as filters in
layered navigation
COMMON APPROACH
Some information is useful for both
I have to create attributes in addition to categories and
duplicate the same data
DRAWBACKS
I have to keep attribute options in sync with category
children
I have to maintain proper correspondence between
product attribute values and category assignment
Filter redundancy
THE SOLUTION
Extending Magento layered navigation
to use category branches such as filters!
LET'S CODE !
Create custom module
Add system configuration
Create new catalog layer filter
block
Create new catalog layer filter
model
Extend catalog layer view
block
Define module layout update
ADD SYSTEM CONFIGURATION
<!--?xml version="1.0"?-->
<config>
<tabs>
<bitbulltab translate="label" module="bitbull_categorylayered">
<label>BITBULL</label>
<sort_order>999</sort_order>
</bitbulltab>
</tabs>
<sections>
<bitbull_categorylayered translate="label" module="bitbull_categorylayered">
<label>Category Layered Navigation</label>
<tab>bitbulltab</tab>
<frontend_type>text</frontend_type>
<sort_order>100</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<configuration translate="label" module="bitbull_categorylayered">
<label>Configuration</label>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<categories translate="label">
<label>Category List</label>
<comment>List of categories to use in layered navigation</comment>
<frontend_model>bitbull_categorylayered/config_categories</frontend_model>
<backend_model>adminhtml/system_config_backend_serialized_array</backend_model>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
EXTEND MAGE_CATALOG_BLOCK_LAYER_VIEW / 1
Child blocks initialization, one for each configured category
class Bitbull_CategoryLayered_Block_Catalog_Layer_View extends Mage_Catalog_Block_Layer_View{
/**
* Prepare child blocks
* @return Mage_Catalog_Block_Layer_View
*/
protected function _prepareLayout()
{
/** @var Mage_Catalog_Model_Category $current_category */
$current_category = Mage::helper('catalog')->getCategory();
foreach ($this->_layeredCategories as $layeredCatConfig) {
$categoryId = $layeredCatConfig['category'];
// check if current category navigation is in path of categorylayered
if ($current_category->getId() && in_array($categoryId, $current_category->getPathIds())) {
continue;
}
// create categoryLayered filter block
$blockAttributes = array(
'categoryId' => $layeredCatConfig['category'],
'requestParam' => $layeredCatConfig['filter']
);
$blockName = 'categorylayered_'.$categoryId;
$categoryBlock = $this->getLayout()
->createBlock($this->_categoryLayeredBlockName, $blockName, $blockAttributes)
->setLayer($this->getLayer())
EXTEND MAGE_CATALOG_BLOCK_LAYER_VIEW / 2
For each configured category, add block to filters list
managing position
class Bitbull_CategoryLayered_Block_Catalog_Layer_View extends Mage_Catalog_Block_Layer_View
{
/**
* Get all layer filters
* @return array
*/
public function getFilters()
{
$filters = parent::getFilters();
foreach ($this->_layeredCategories as $layeredCatConfig) {
$categoryId = $layeredCatConfig['category'];
$position = $layeredCatConfig['position'];
if (($categoryFilter = $this->_getLayeredCategoryFilter($categoryId))) {
$filters = array_merge(
array_slice(
$filters,
0,
$position
),
array($categoryFilter),
array_slice(
$filters,
$position,
count($filters)
)
);
}
CREATE NEW CATALOG LAYER FILTER MODEL / 1
Retrieve subcategories as filter items.
class Bitbull_CategoryLayered_Model_Catalog_Layer_Filter_CategoryLayered extends Mage_Catalog_Model_Layer_Filter_Abstract
{
/**
* Get data array for building filter items
* @return array
*/
protected function _getItemsData()
{
$key = $this->getLayer()->getStateKey().'_'.$this->_requestVar;
$data = $this->getLayer()->getAggregator()->getCacheData($key);
if ($data === null) {
// load children for root category or current selected category for this filter
$categories = $this->_appliedCategory instanceof Mage_Catalog_Model_Category ?
$this->_appliedCategory->getChildrenCategories() :
$this->_rootCategory->getChildrenCategories();
$this->getLayer()->getProductCollection()
->addCountToCategories($categories);
$data = array();
foreach ($categories as $category) {
/** @var $category Mage_Catalog_Model_Categeory */
if ($category->getIsActive() && $category->getProductCount()) {
$data[] = array(
'label' => Mage::helper('core')->escapeHtml($category->getName()),
'value' => $category->getId(),
'count' => $category->getProductCount(),
CREATE NEW CATALOG LAYER FILTER MODEL / 2
Apply filter logic adding a join on
catalog/category_product_index table
class Bitbull_CategoryLayered_Model_Catalog_Layer_Filter_CategoryLayered extends Mage_Catalog_Model_Layer_Filter_Abstract
/**
* Apply category filter to layer
* @param Zend_Controller_Request_Abstract $request
* @param Mage_Core_Block_Abstract $filterBlock
* @return Bitbull_CategoryLayered_Model_Catalog_Layer_Filter_CategoryLayered
*/
public function apply(Zend_Controller_Request_Abstract $request, $filterBlock)
{
$filter = (int) $request->getParam($this->getRequestVar());
if (!$filter) {
return $this;
}
// load data for applied category
$this->_appliedCategory = Mage::getModel('catalog/category')
->setStoreId(Mage::app()->getStore()->getId())
->load($filter);
if ($this->_appliedCategory->getId()) {
// create join and conditions for additional category filter
$tableAlias = 'category_layered_'.$this->_rootCategory->getId();
$conditions = array();
$conditions['category_id'] = $filter;
$conditions['store_id'] = Mage::app()->getStore()->getId();
if (!$this->_appliedCategory->getIsAnchor()) {
$conditions['is_parent'] = 1;
     Frontend Demo Backend Demo
CONCLUSION
By extending Magento layered navigation as shown we can:
avoid data replication and maintenance
filter deeply until selected category has subcategories
avoid filter redundancy
Without touching the core
THANK YOU !
NAMASTÉ
ANY QUESTION ?
@nadiasala
nadia.sala@bitbull.it
https://github.com/bitbull-team/magento-module-category-layered

Weitere ähnliche Inhalte

Was ist angesagt?

Google Rich Snippets: Magento Extension by Amasty
Google Rich Snippets: Magento Extension by AmastyGoogle Rich Snippets: Magento Extension by Amasty
Google Rich Snippets: Magento Extension by Amasty
Amasty
 

Was ist angesagt? (20)

A/B Testing Magento Extension by Amasty | User Guide
A/B Testing Magento Extension by Amasty | User GuideA/B Testing Magento Extension by Amasty | User Guide
A/B Testing Magento Extension by Amasty | User Guide
 
Special Promotions: Magento Extension by Amasty. user Guide.
Special Promotions: Magento Extension by Amasty. user Guide.Special Promotions: Magento Extension by Amasty. user Guide.
Special Promotions: Magento Extension by Amasty. user Guide.
 
User Guide for Product Labels Magento extension by Amasty
User Guide for Product Labels Magento extension by AmastyUser Guide for Product Labels Magento extension by Amasty
User Guide for Product Labels Magento extension by Amasty
 
The Ultimate Guide to Magento SEO
The Ultimate Guide to Magento SEOThe Ultimate Guide to Magento SEO
The Ultimate Guide to Magento SEO
 
Follow up email_for_magento_2_user_guide
Follow up email_for_magento_2_user_guideFollow up email_for_magento_2_user_guide
Follow up email_for_magento_2_user_guide
 
Color Swatches Pro: Magento Extension by Amasty. User Guide.
Color Swatches Pro: Magento Extension by Amasty. User Guide.Color Swatches Pro: Magento Extension by Amasty. User Guide.
Color Swatches Pro: Magento Extension by Amasty. User Guide.
 
Product Label: Magento Extension by Amasty. User Guide.
Product Label: Magento Extension by Amasty. User Guide.Product Label: Magento Extension by Amasty. User Guide.
Product Label: Magento Extension by Amasty. User Guide.
 
Order Memos and Attachments: Magento Extension by Amasty. User Guide
Order Memos and Attachments: Magento Extension by Amasty. User GuideOrder Memos and Attachments: Magento Extension by Amasty. User Guide
Order Memos and Attachments: Magento Extension by Amasty. User Guide
 
Customer Group Catalog: Magento Extension by Amasty. User Guide
Customer Group Catalog: Magento Extension by Amasty. User GuideCustomer Group Catalog: Magento Extension by Amasty. User Guide
Customer Group Catalog: Magento Extension by Amasty. User Guide
 
Magento RMA System by Amasty. User Guide.
Magento RMA System by Amasty. User Guide.Magento RMA System by Amasty. User Guide.
Magento RMA System by Amasty. User Guide.
 
Advanced Customer Segments Magento Extension by Amasty | User Guide
Advanced Customer Segments Magento Extension by Amasty | User GuideAdvanced Customer Segments Magento Extension by Amasty | User Guide
Advanced Customer Segments Magento Extension by Amasty | User Guide
 
Customer Activity Log: Magento Extension by Amasty. User Guide.
Customer Activity Log: Magento Extension by Amasty. User Guide.Customer Activity Log: Magento Extension by Amasty. User Guide.
Customer Activity Log: Magento Extension by Amasty. User Guide.
 
User Guide: Advanced product options Magento extension
User Guide: Advanced product options Magento extensionUser Guide: Advanced product options Magento extension
User Guide: Advanced product options Magento extension
 
Efficient Order Export: Magento Extension by Amasty. User Guide.
Efficient Order Export: Magento Extension by Amasty. User Guide.Efficient Order Export: Magento Extension by Amasty. User Guide.
Efficient Order Export: Magento Extension by Amasty. User Guide.
 
Cloud Backup by Amasty. User Guide/
Cloud Backup by Amasty. User Guide/Cloud Backup by Amasty. User Guide/
Cloud Backup by Amasty. User Guide/
 
Flexible Menu: Magento Extension by Amasty. User Guide.
Flexible Menu: Magento Extension by Amasty. User Guide.Flexible Menu: Magento Extension by Amasty. User Guide.
Flexible Menu: Magento Extension by Amasty. User Guide.
 
Google Rich Snippets: Magento Extension by Amasty
Google Rich Snippets: Magento Extension by AmastyGoogle Rich Snippets: Magento Extension by Amasty
Google Rich Snippets: Magento Extension by Amasty
 
Shipping Table Rates for Magento 2 by Amasty | User Guide
Shipping Table Rates for Magento 2 by Amasty | User GuideShipping Table Rates for Magento 2 by Amasty | User Guide
Shipping Table Rates for Magento 2 by Amasty | User Guide
 
Advanced Reports Magento Extension by Amasty | User Guide
Advanced Reports Magento Extension by Amasty | User GuideAdvanced Reports Magento Extension by Amasty | User Guide
Advanced Reports Magento Extension by Amasty | User Guide
 
Photo Gallery Pro Magento Extension by Amasty | User Guide
Photo Gallery Pro Magento Extension by Amasty | User GuidePhoto Gallery Pro Magento Extension by Amasty | User Guide
Photo Gallery Pro Magento Extension by Amasty | User Guide
 

Andere mochten auch

THE RISE OF AN ALTROISTIC SOCIETY
THE RISE OF AN ALTROISTIC SOCIETYTHE RISE OF AN ALTROISTIC SOCIETY
THE RISE OF AN ALTROISTIC SOCIETY
Sharon Gal Or
 
The Taiwanese Turtle And The Betel Nuts
The Taiwanese Turtle And The Betel NutsThe Taiwanese Turtle And The Betel Nuts
The Taiwanese Turtle And The Betel Nuts
Sharon Gal Or
 
學生憂鬱與自殺之防治931116
學生憂鬱與自殺之防治931116學生憂鬱與自殺之防治931116
學生憂鬱與自殺之防治931116
Zack Ou
 

Andere mochten auch (19)

Resume2016-a
Resume2016-aResume2016-a
Resume2016-a
 
THE RISE OF AN ALTROISTIC SOCIETY
THE RISE OF AN ALTROISTIC SOCIETYTHE RISE OF AN ALTROISTIC SOCIETY
THE RISE OF AN ALTROISTIC SOCIETY
 
Power point vs prezi
Power point vs preziPower point vs prezi
Power point vs prezi
 
Accomplishments
AccomplishmentsAccomplishments
Accomplishments
 
Infografía personal
Infografía personalInfografía personal
Infografía personal
 
什麼是人類學?
什麼是人類學?什麼是人類學?
什麼是人類學?
 
The Taiwanese Turtle And The Betel Nuts
The Taiwanese Turtle And The Betel NutsThe Taiwanese Turtle And The Betel Nuts
The Taiwanese Turtle And The Betel Nuts
 
Character profiles
Character profiles Character profiles
Character profiles
 
Twmba 2
Twmba 2Twmba 2
Twmba 2
 
Bim vs openbim pecha kucha
Bim vs openbim pecha kuchaBim vs openbim pecha kucha
Bim vs openbim pecha kucha
 
Smartphone intrinsecamente seguro: modelo Innovation 2.0
Smartphone intrinsecamente seguro: modelo Innovation 2.0Smartphone intrinsecamente seguro: modelo Innovation 2.0
Smartphone intrinsecamente seguro: modelo Innovation 2.0
 
buildingSMART-konf-2014-tønsbergprosjektet-konseptmodell-140424
buildingSMART-konf-2014-tønsbergprosjektet-konseptmodell-140424buildingSMART-konf-2014-tønsbergprosjektet-konseptmodell-140424
buildingSMART-konf-2014-tønsbergprosjektet-konseptmodell-140424
 
學生憂鬱與自殺之防治931116
學生憂鬱與自殺之防治931116學生憂鬱與自殺之防治931116
學生憂鬱與自殺之防治931116
 
Caw 2 2 experience and skills inventory
Caw 2 2 experience and skills inventoryCaw 2 2 experience and skills inventory
Caw 2 2 experience and skills inventory
 
為什麼醫師需要納入勞基法?
為什麼醫師需要納入勞基法?為什麼醫師需要納入勞基法?
為什麼醫師需要納入勞基法?
 
抄,是最好的獲利模式
抄,是最好的獲利模式抄,是最好的獲利模式
抄,是最好的獲利模式
 
Find Your Inner Superhero
Find Your Inner SuperheroFind Your Inner Superhero
Find Your Inner Superhero
 
Tipos de Luminarias
Tipos de LuminariasTipos de Luminarias
Tipos de Luminarias
 
淺談測試Part2
淺談測試Part2淺談測試Part2
淺談測試Part2
 

Ähnlich wie Extending Magento Layered Navigation

Can someone help me figure out how to add a drop down menu that will a.pdf
Can someone help me figure out how to add a drop down menu that will a.pdfCan someone help me figure out how to add a drop down menu that will a.pdf
Can someone help me figure out how to add a drop down menu that will a.pdf
aksachdevahosymills
 
Gail villanueva add muscle to your wordpress site
Gail villanueva   add muscle to your wordpress siteGail villanueva   add muscle to your wordpress site
Gail villanueva add muscle to your wordpress site
references
 

Ähnlich wie Extending Magento Layered Navigation (20)

Magento Product Types Demystified
Magento Product Types DemystifiedMagento Product Types Demystified
Magento Product Types Demystified
 
Magento Indexes
Magento IndexesMagento Indexes
Magento Indexes
 
Shopify Partner Social
Shopify Partner SocialShopify Partner Social
Shopify Partner Social
 
Introduction to Web Components
Introduction to Web ComponentsIntroduction to Web Components
Introduction to Web Components
 
Taxonomy Everywhere
Taxonomy EverywhereTaxonomy Everywhere
Taxonomy Everywhere
 
Can someone help me figure out how to add a drop down menu that will a.pdf
Can someone help me figure out how to add a drop down menu that will a.pdfCan someone help me figure out how to add a drop down menu that will a.pdf
Can someone help me figure out how to add a drop down menu that will a.pdf
 
May the core be with you - JandBeyond 2014
May the core be with you - JandBeyond 2014May the core be with you - JandBeyond 2014
May the core be with you - JandBeyond 2014
 
Gail villanueva add muscle to your wordpress site
Gail villanueva   add muscle to your wordpress siteGail villanueva   add muscle to your wordpress site
Gail villanueva add muscle to your wordpress site
 
WordPress for developers - phpday 2011
WordPress for developers -  phpday 2011WordPress for developers -  phpday 2011
WordPress for developers - phpday 2011
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
 
Advanced Skinning with DotNetNuke 5
Advanced Skinning with DotNetNuke 5Advanced Skinning with DotNetNuke 5
Advanced Skinning with DotNetNuke 5
 
Advanced Skinning With DotNetNuke
Advanced Skinning With DotNetNukeAdvanced Skinning With DotNetNuke
Advanced Skinning With DotNetNuke
 
Tips and Tricks for LiveWhale Development
Tips and Tricks for LiveWhale DevelopmentTips and Tricks for LiveWhale Development
Tips and Tricks for LiveWhale Development
 
WordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp ChicagoWordPress Accessibility: WordCamp Chicago
WordPress Accessibility: WordCamp Chicago
 
APIs for catalogs
APIs for catalogsAPIs for catalogs
APIs for catalogs
 
Implement rich snippets in your webshop
Implement rich snippets in your webshopImplement rich snippets in your webshop
Implement rich snippets in your webshop
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 
Django at the Disco
Django at the DiscoDjango at the Disco
Django at the Disco
 

Kürzlich hochgeladen

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Kürzlich hochgeladen (20)

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 

Extending Magento Layered Navigation

  • 1. EXTENDING MAGENTO LAYERED NAVIGATION MAGE TITANS ITALIA - MILAN, FEBRUARY 5TH 2016 Nadia Sala Extending Magento layered navigation by Nadia Sala
  • 2. ABOUT ME PHP DEVELOPER MAGENTO BACKEND DEVELOPER @nadiasala nadia.sala@bitbull.it
  • 3. THE STORY "Once upon a time there was an e-commerce website that exposed a catalog with many descriptive information."
  • 4. You can imagine a fashion catalog, so you mind about: Product type, Occasion, Color, Gender, Designer, Price, Size, ...and more
  • 5. SCENARIO Some information is useful for browsing catalog Some information is useful for filtering catalog Some information is useful for both
  • 6. COMMON APPROACH Some information is useful for browsing catalog I can create  categories and include them in the navigation menu
  • 7. COMMON APPROACH Some information is useful for filtering catalog I can configure some attributes to be used as filters in layered navigation
  • 8. COMMON APPROACH Some information is useful for both I have to create attributes in addition to categories and duplicate the same data
  • 9. DRAWBACKS I have to keep attribute options in sync with category children I have to maintain proper correspondence between product attribute values and category assignment Filter redundancy
  • 10.
  • 11.
  • 12. THE SOLUTION Extending Magento layered navigation to use category branches such as filters!
  • 13. LET'S CODE ! Create custom module Add system configuration Create new catalog layer filter block Create new catalog layer filter model Extend catalog layer view block Define module layout update
  • 14. ADD SYSTEM CONFIGURATION <!--?xml version="1.0"?--> <config> <tabs> <bitbulltab translate="label" module="bitbull_categorylayered"> <label>BITBULL</label> <sort_order>999</sort_order> </bitbulltab> </tabs> <sections> <bitbull_categorylayered translate="label" module="bitbull_categorylayered"> <label>Category Layered Navigation</label> <tab>bitbulltab</tab> <frontend_type>text</frontend_type> <sort_order>100</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <groups> <configuration translate="label" module="bitbull_categorylayered"> <label>Configuration</label> <sort_order>10</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <fields> <categories translate="label"> <label>Category List</label> <comment>List of categories to use in layered navigation</comment> <frontend_model>bitbull_categorylayered/config_categories</frontend_model> <backend_model>adminhtml/system_config_backend_serialized_array</backend_model> <sort_order>20</sort_order> <show_in_default>1</show_in_default>
  • 15. EXTEND MAGE_CATALOG_BLOCK_LAYER_VIEW / 1 Child blocks initialization, one for each configured category class Bitbull_CategoryLayered_Block_Catalog_Layer_View extends Mage_Catalog_Block_Layer_View{ /** * Prepare child blocks * @return Mage_Catalog_Block_Layer_View */ protected function _prepareLayout() { /** @var Mage_Catalog_Model_Category $current_category */ $current_category = Mage::helper('catalog')->getCategory(); foreach ($this->_layeredCategories as $layeredCatConfig) { $categoryId = $layeredCatConfig['category']; // check if current category navigation is in path of categorylayered if ($current_category->getId() && in_array($categoryId, $current_category->getPathIds())) { continue; } // create categoryLayered filter block $blockAttributes = array( 'categoryId' => $layeredCatConfig['category'], 'requestParam' => $layeredCatConfig['filter'] ); $blockName = 'categorylayered_'.$categoryId; $categoryBlock = $this->getLayout() ->createBlock($this->_categoryLayeredBlockName, $blockName, $blockAttributes) ->setLayer($this->getLayer())
  • 16. EXTEND MAGE_CATALOG_BLOCK_LAYER_VIEW / 2 For each configured category, add block to filters list managing position class Bitbull_CategoryLayered_Block_Catalog_Layer_View extends Mage_Catalog_Block_Layer_View { /** * Get all layer filters * @return array */ public function getFilters() { $filters = parent::getFilters(); foreach ($this->_layeredCategories as $layeredCatConfig) { $categoryId = $layeredCatConfig['category']; $position = $layeredCatConfig['position']; if (($categoryFilter = $this->_getLayeredCategoryFilter($categoryId))) { $filters = array_merge( array_slice( $filters, 0, $position ), array($categoryFilter), array_slice( $filters, $position, count($filters) ) ); }
  • 17. CREATE NEW CATALOG LAYER FILTER MODEL / 1 Retrieve subcategories as filter items. class Bitbull_CategoryLayered_Model_Catalog_Layer_Filter_CategoryLayered extends Mage_Catalog_Model_Layer_Filter_Abstract { /** * Get data array for building filter items * @return array */ protected function _getItemsData() { $key = $this->getLayer()->getStateKey().'_'.$this->_requestVar; $data = $this->getLayer()->getAggregator()->getCacheData($key); if ($data === null) { // load children for root category or current selected category for this filter $categories = $this->_appliedCategory instanceof Mage_Catalog_Model_Category ? $this->_appliedCategory->getChildrenCategories() : $this->_rootCategory->getChildrenCategories(); $this->getLayer()->getProductCollection() ->addCountToCategories($categories); $data = array(); foreach ($categories as $category) { /** @var $category Mage_Catalog_Model_Categeory */ if ($category->getIsActive() && $category->getProductCount()) { $data[] = array( 'label' => Mage::helper('core')->escapeHtml($category->getName()), 'value' => $category->getId(), 'count' => $category->getProductCount(),
  • 18. CREATE NEW CATALOG LAYER FILTER MODEL / 2 Apply filter logic adding a join on catalog/category_product_index table class Bitbull_CategoryLayered_Model_Catalog_Layer_Filter_CategoryLayered extends Mage_Catalog_Model_Layer_Filter_Abstract /** * Apply category filter to layer * @param Zend_Controller_Request_Abstract $request * @param Mage_Core_Block_Abstract $filterBlock * @return Bitbull_CategoryLayered_Model_Catalog_Layer_Filter_CategoryLayered */ public function apply(Zend_Controller_Request_Abstract $request, $filterBlock) { $filter = (int) $request->getParam($this->getRequestVar()); if (!$filter) { return $this; } // load data for applied category $this->_appliedCategory = Mage::getModel('catalog/category') ->setStoreId(Mage::app()->getStore()->getId()) ->load($filter); if ($this->_appliedCategory->getId()) { // create join and conditions for additional category filter $tableAlias = 'category_layered_'.$this->_rootCategory->getId(); $conditions = array(); $conditions['category_id'] = $filter; $conditions['store_id'] = Mage::app()->getStore()->getId(); if (!$this->_appliedCategory->getIsAnchor()) { $conditions['is_parent'] = 1;
  • 20. CONCLUSION By extending Magento layered navigation as shown we can: avoid data replication and maintenance filter deeply until selected category has subcategories avoid filter redundancy Without touching the core
  • 21. THANK YOU ! NAMASTÉ ANY QUESTION ? @nadiasala nadia.sala@bitbull.it https://github.com/bitbull-team/magento-module-category-layered