SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
MAGENTO BEST PRACTICES
ALESSANDRO RONCHI
MANCHESTER, NOV. 7th
2015
ABOUT ME: @aleron75
aleron75/mageres
#m
agebp
3/16#magebp rules
● Use the best available tools
● Use Magento runtime
● Use alternatives to Mage::log()
● Extend native autoloading
● Write effective and solid MVC components
● Write scalable code
● Never touch the core
#m
agebp
4/16#magebp example
● Use the best available tools
● Use Magento runtime
● Use alternatives to Mage::log()
● Extend native autoloading
● Write effective and solid MVC components
● Write scalable code
● Never touch the core
#m
agebp
5/16#magebp example
● Use the best tools & habits
– to test code
– to profile code
● Use Magento runtime
– to validate code easily and rapidly
● Write scalable code
– to avoid performance traps
#m
agebp
6/16Based on a real story...
What you will be shown is real code
that run on production.
This code was written to display
a category navigation menu.
No animals were harmed in the making of
this code; a website was.
#m
agebp
7/16
First chunk
seek for category subcategories
$rootCatID = Mage::app()­>getStore()­>getRootCategoryId();
$cat = Mage::getModel('catalog/category')­>load($rootCatID);
$subCats = $cat­>getChildren();
foreach (explode(',', $subCats) as $subCatid) {
  $category = Mage::getModel('catalog/category')
    ­>load($subCatid);
  $subcategories = $category­>getChildren();
}
foreach (explode(',', $subcategories) as $subCategoryid) {
  $subcategory = Mage::getModel('catalog/category')
    ­>load($subCategoryid);
  // Second chunk...
}
#m
agebp
8/16
First chunk
seek for category subcategories
$rootCatID = Mage::app()­>getStore()­>getRootCategoryId();
$cat = Mage::getModel('catalog/category')­>load($rootCatID);
$subCats = $cat­>getChildren();
foreach (explode(',', $subCats) as $subCatid) {
  $category = Mage::getModel('catalog/category')
    ­>load($subCatid);
  $subcategories = $category­>getChildren();
}
foreach (explode(',', $subcategories) as $subCategoryid) {
  $subcategory = Mage::getModel('catalog/category')
    ­>load($subCategoryid);
  // Second chunk...
}
Assuming there is only one sub
category will break menu as soon as
a new one is added: and it happened!
Loading in loops
doesn't scale
#m
agebp
9/16
Second chunk
seek for product categories
foreach (explode(',', $subcategories) as $subCategoryid) {
  $subcategory = Mage::getModel('catalog/category')­>load($subCategoryid);
  $category = Mage::getModel('catalog/category')
    ­>load($subcategory­>getId());
  $subcatProducts = Mage::getModel('catalog/product')
    ­>getCollection()
    ­>addCategoryFilter($subcategory)­>load();
  foreach($subcatProducts as $product) {
    foreach($product­>getCategoryIds() as $categoryId) {
      $myCategory = Mage::getModel('catalog/category')­>load($categoryId);
      $catUrl = $myCategory­>getUrl();
      $prodUrlKey = end($explode("/", $product­>getProductUrl()));
    }
  }
}
#m
agebp
10/16
Second chunk
seek for product categories
foreach (explode(',', $subcategories) as $subCategoryid) {
  $subcategory = Mage::getModel('catalog/category')­>load($subCategoryid);
  $category = Mage::getModel('catalog/category')
    ­>load($subcategory­>getId());
  $subcatProducts = Mage::getModel('catalog/product')
    ­>getCollection()
    ­>addCategoryFilter($subcategory)­>load();
  foreach($subcatProducts as $product) {
    foreach($product­>getCategoryIds() as $categoryId) {
      $myCategory = Mage::getModel('catalog/category')­>load($categoryId);
      $catUrl = $myCategory­>getUrl();
      $prodUrlKey = end($explode("/", $product­>getProductUrl()));
    }
  }
}
$category is not used and
adds another load in a loop!
WHY?
#m
agebp
11/16
There is a name for this...
Code
Running
Ashamedly on
Production
Sadly enough a customer paid for that:
it's unethical other than unprofessional.
This is not technical debt.
This is building with sand.
#m
agebp
12/16
Refactoring first chunk
seek for category subcategories
$configuredCatId = 
Mage::getStoreConfig('my/own/path');
$category = Mage::getModel('catalog/category')
  ­>getCollection()
  ­>addIdFilter($configuredCatId)
  ­>setPageSize(1)
  ­>getFirstItem();
$subcategories = $category­>getChildrenCategories();
foreach ($subcategories as $subcategory) {
  // Second chunk...
}
#m
agebp
13/16
Refactoring second chunk
seek for product categories
foreach ($subcategories as $subcategory) {
  $subcatProducts = $subcategory­>getProductCollection();
  foreach($subcatProducts as $product) {
    $productCategories = $product­>getCategoryCollection();
    foreach($productCategories as $myCategory) {
      $catUrl = $myCategory­>getUrl();
      $prodUrl = $product­>getProductUrl();
      $prodUrlKey = substr(strrchr($prodUrl, '/'), 1);
    }
  }
}
#m
agebp
14/16
Comparison
(using XHProf)
Before refactoring
After refactoring
10x faster!
#m
agebp
15/16
Refactoring
(using tests)
● Testing gives us the confidence to change
code without breaking functionality
● Testing doesn't require complex tools
– start from manual tests
– automate to scale
– start from common tools: diff
– use more sophisticated tools when needed
THANK YOU!
https://github.com/aleron75https://github.com/aleron75
https://twitter.com/aleron75https://twitter.com/aleron75
http://leanpub.com/magebp/c/mt15ukhttp://leanpub.com/magebp/c/mt15uk

Weitere ähnliche Inhalte

Ähnlich wie A true story about Magento best practices

Ähnlich wie A true story about Magento best practices (20)

Android reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skypeAndroid reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skype
 
Real World AngularJS recipes: beyond TodoMVC - Carlo Bonamico, Sonia Pini - C...
Real World AngularJS recipes: beyond TodoMVC - Carlo Bonamico, Sonia Pini - C...Real World AngularJS recipes: beyond TodoMVC - Carlo Bonamico, Sonia Pini - C...
Real World AngularJS recipes: beyond TodoMVC - Carlo Bonamico, Sonia Pini - C...
 
Real World AngularJS recipes: beyond TodoMVC
Real World AngularJS recipes: beyond TodoMVCReal World AngularJS recipes: beyond TodoMVC
Real World AngularJS recipes: beyond TodoMVC
 
Learn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for DevelopersLearn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for Developers
 
Extension Submission to Marketplace
Extension Submission to MarketplaceExtension Submission to Marketplace
Extension Submission to Marketplace
 
Testing Angular 2 Applications - Rich Web 2016
Testing Angular 2 Applications - Rich Web 2016Testing Angular 2 Applications - Rich Web 2016
Testing Angular 2 Applications - Rich Web 2016
 
Writing testable Code (MageTitans Mini 2016)
Writing testable Code (MageTitans Mini 2016)Writing testable Code (MageTitans Mini 2016)
Writing testable Code (MageTitans Mini 2016)
 
Final (3).pptx
Final (3).pptxFinal (3).pptx
Final (3).pptx
 
Getting Started with Scripts #HeroConf London 2015
Getting Started with Scripts #HeroConf London 2015Getting Started with Scripts #HeroConf London 2015
Getting Started with Scripts #HeroConf London 2015
 
Complete Website Development Guide by AMit P Kumar
Complete Website Development Guide by AMit P KumarComplete Website Development Guide by AMit P Kumar
Complete Website Development Guide by AMit P Kumar
 
Stapling and patching the web of now - ForwardJS3, San Francisco
Stapling and patching the web of now - ForwardJS3, San FranciscoStapling and patching the web of now - ForwardJS3, San Francisco
Stapling and patching the web of now - ForwardJS3, San Francisco
 
Angular.js for beginners
Angular.js for beginners Angular.js for beginners
Angular.js for beginners
 
Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
 
Testing Angular Applications - Jfokus 2017
Testing Angular Applications - Jfokus 2017Testing Angular Applications - Jfokus 2017
Testing Angular Applications - Jfokus 2017
 
INTERFACE by apidays 2023 - API Design Governance, Nauman Ali, Stoplight
INTERFACE by apidays 2023 - API Design Governance, Nauman Ali, StoplightINTERFACE by apidays 2023 - API Design Governance, Nauman Ali, Stoplight
INTERFACE by apidays 2023 - API Design Governance, Nauman Ali, Stoplight
 
Testing stage. being ahead business with cucumber
Testing stage. being ahead business with cucumberTesting stage. being ahead business with cucumber
Testing stage. being ahead business with cucumber
 
Mobile security recipes for xamarin
Mobile security recipes for xamarinMobile security recipes for xamarin
Mobile security recipes for xamarin
 
How can JAVA Performance tuning speed up applications.pdf
How can JAVA Performance tuning speed up applications.pdfHow can JAVA Performance tuning speed up applications.pdf
How can JAVA Performance tuning speed up applications.pdf
 
Wagento Magento 2 developer - Brent W Peterson
Wagento Magento 2 developer - Brent W PetersonWagento Magento 2 developer - Brent W Peterson
Wagento Magento 2 developer - Brent W Peterson
 
API Design Best Practices by Igor Miniailo
API Design Best Practices by Igor MiniailoAPI Design Best Practices by Igor Miniailo
API Design Best Practices by Igor Miniailo
 

Mehr von Alessandro Ronchi

Mehr von Alessandro Ronchi (12)

Meet Magento IT 2021 - Principles & Advantages of Hexagonal Architecture on M...
Meet Magento IT 2021 - Principles & Advantages of Hexagonal Architecture on M...Meet Magento IT 2021 - Principles & Advantages of Hexagonal Architecture on M...
Meet Magento IT 2021 - Principles & Advantages of Hexagonal Architecture on M...
 
Awesome architectures in Magento 2.3
Awesome architectures in Magento 2.3Awesome architectures in Magento 2.3
Awesome architectures in Magento 2.3
 
Some lessons learned contributing to #MagentoMSI
Some lessons learned contributing to #MagentoMSISome lessons learned contributing to #MagentoMSI
Some lessons learned contributing to #MagentoMSI
 
The lessons I learned contributing to #MagentoMSI
The lessons I learned contributing to #MagentoMSIThe lessons I learned contributing to #MagentoMSI
The lessons I learned contributing to #MagentoMSI
 
How I ended up contributing to Magento core
How I ended up contributing to Magento coreHow I ended up contributing to Magento core
How I ended up contributing to Magento core
 
How I ended up touching Magento core
How I ended up touching Magento coreHow I ended up touching Magento core
How I ended up touching Magento core
 
B2B & Magento: a long journey tale
B2B & Magento: a long journey taleB2B & Magento: a long journey tale
B2B & Magento: a long journey tale
 
Need to estimate? Let's play poker!
Need to estimate? Let's play poker!Need to estimate? Let's play poker!
Need to estimate? Let's play poker!
 
Why I did one step backward to go forward
Why I did one step backward to go forwardWhy I did one step backward to go forward
Why I did one step backward to go forward
 
Mageday::2014 - Workshop
Mageday::2014 - WorkshopMageday::2014 - Workshop
Mageday::2014 - Workshop
 
Mageday::2014
Mageday::2014Mageday::2014
Mageday::2014
 
Mageploy presentato al Mage::day() 2013
Mageploy presentato al Mage::day() 2013Mageploy presentato al Mage::day() 2013
Mageploy presentato al Mage::day() 2013
 

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
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
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)

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
 
%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
 
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
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%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
 
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
 
%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
 
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...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
%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
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
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
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
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
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 

A true story about Magento best practices

  • 1. MAGENTO BEST PRACTICES ALESSANDRO RONCHI MANCHESTER, NOV. 7th 2015
  • 3. #m agebp 3/16#magebp rules ● Use the best available tools ● Use Magento runtime ● Use alternatives to Mage::log() ● Extend native autoloading ● Write effective and solid MVC components ● Write scalable code ● Never touch the core
  • 4. #m agebp 4/16#magebp example ● Use the best available tools ● Use Magento runtime ● Use alternatives to Mage::log() ● Extend native autoloading ● Write effective and solid MVC components ● Write scalable code ● Never touch the core
  • 5. #m agebp 5/16#magebp example ● Use the best tools & habits – to test code – to profile code ● Use Magento runtime – to validate code easily and rapidly ● Write scalable code – to avoid performance traps
  • 6. #m agebp 6/16Based on a real story... What you will be shown is real code that run on production. This code was written to display a category navigation menu. No animals were harmed in the making of this code; a website was.
  • 7. #m agebp 7/16 First chunk seek for category subcategories $rootCatID = Mage::app()­>getStore()­>getRootCategoryId(); $cat = Mage::getModel('catalog/category')­>load($rootCatID); $subCats = $cat­>getChildren(); foreach (explode(',', $subCats) as $subCatid) {   $category = Mage::getModel('catalog/category')     ­>load($subCatid);   $subcategories = $category­>getChildren(); } foreach (explode(',', $subcategories) as $subCategoryid) {   $subcategory = Mage::getModel('catalog/category')     ­>load($subCategoryid);   // Second chunk... }
  • 8. #m agebp 8/16 First chunk seek for category subcategories $rootCatID = Mage::app()­>getStore()­>getRootCategoryId(); $cat = Mage::getModel('catalog/category')­>load($rootCatID); $subCats = $cat­>getChildren(); foreach (explode(',', $subCats) as $subCatid) {   $category = Mage::getModel('catalog/category')     ­>load($subCatid);   $subcategories = $category­>getChildren(); } foreach (explode(',', $subcategories) as $subCategoryid) {   $subcategory = Mage::getModel('catalog/category')     ­>load($subCategoryid);   // Second chunk... } Assuming there is only one sub category will break menu as soon as a new one is added: and it happened! Loading in loops doesn't scale
  • 9. #m agebp 9/16 Second chunk seek for product categories foreach (explode(',', $subcategories) as $subCategoryid) {   $subcategory = Mage::getModel('catalog/category')­>load($subCategoryid);   $category = Mage::getModel('catalog/category')     ­>load($subcategory­>getId());   $subcatProducts = Mage::getModel('catalog/product')     ­>getCollection()     ­>addCategoryFilter($subcategory)­>load();   foreach($subcatProducts as $product) {     foreach($product­>getCategoryIds() as $categoryId) {       $myCategory = Mage::getModel('catalog/category')­>load($categoryId);       $catUrl = $myCategory­>getUrl();       $prodUrlKey = end($explode("/", $product­>getProductUrl()));     }   } }
  • 10. #m agebp 10/16 Second chunk seek for product categories foreach (explode(',', $subcategories) as $subCategoryid) {   $subcategory = Mage::getModel('catalog/category')­>load($subCategoryid);   $category = Mage::getModel('catalog/category')     ­>load($subcategory­>getId());   $subcatProducts = Mage::getModel('catalog/product')     ­>getCollection()     ­>addCategoryFilter($subcategory)­>load();   foreach($subcatProducts as $product) {     foreach($product­>getCategoryIds() as $categoryId) {       $myCategory = Mage::getModel('catalog/category')­>load($categoryId);       $catUrl = $myCategory­>getUrl();       $prodUrlKey = end($explode("/", $product­>getProductUrl()));     }   } } $category is not used and adds another load in a loop! WHY?
  • 11. #m agebp 11/16 There is a name for this... Code Running Ashamedly on Production Sadly enough a customer paid for that: it's unethical other than unprofessional. This is not technical debt. This is building with sand.
  • 12. #m agebp 12/16 Refactoring first chunk seek for category subcategories $configuredCatId =  Mage::getStoreConfig('my/own/path'); $category = Mage::getModel('catalog/category')   ­>getCollection()   ­>addIdFilter($configuredCatId)   ­>setPageSize(1)   ­>getFirstItem(); $subcategories = $category­>getChildrenCategories(); foreach ($subcategories as $subcategory) {   // Second chunk... }
  • 13. #m agebp 13/16 Refactoring second chunk seek for product categories foreach ($subcategories as $subcategory) {   $subcatProducts = $subcategory­>getProductCollection();   foreach($subcatProducts as $product) {     $productCategories = $product­>getCategoryCollection();     foreach($productCategories as $myCategory) {       $catUrl = $myCategory­>getUrl();       $prodUrl = $product­>getProductUrl();       $prodUrlKey = substr(strrchr($prodUrl, '/'), 1);     }   } }
  • 15. #m agebp 15/16 Refactoring (using tests) ● Testing gives us the confidence to change code without breaking functionality ● Testing doesn't require complex tools – start from manual tests – automate to scale – start from common tools: diff – use more sophisticated tools when needed