Skip to main content

Posts

5 worst Magento practices

I hope this article will help many to save their eCommerce problem and to optimize their code. Magento is a wonderful platform to kick start your e-commerce endeavors. But the worst part is its development flow. I will introduce you the 5 worst Magento development practices followed by detailed example. 1.  Collections – query results limitation To improve code performance and scalability remember to apply limitation on the collection’s query results — it’s much better to do  $collection->getSize()  instead of  $collection->count()  or  count($collection)  because that ways, all of the items will be loaded from the database and than iterated. 2.  SQL queries inside a loop Bad practice is to load Magento models inside a loop. Running SQL query is very expensive operation and doing it in a loop tends to make it even worse. Instead of doing that we could use data collections to load models and then process it.  To be careful when ...

Display all categories and sub categories on left sidebar in Magento

You can display list of all categories and subcategories in left sidebar including product view page using the following code in Magento. You can display subcategories upto second level using this code. You can create a new file for this code and you can add it in your theme’s catalog XML file like this: <block type="catalog/navigation" name="catalog.leftnav" template="catalog/navigation/my_left_nav.phtml" /> It should be placed inside the reference name “ left “, to show it in the left sidebar. I hope you know all these stuffs. Let’s get to the point. Just copy and paste the code in the  my_left_nav.phtml,  which should be in your theme’s  catalog/navigation folder.   <!-- List all categories and their second level subcategories --> <div class="block block-list block-categories"> <div id="block-categories" class="block-title active"> <strong><span>Categories </span...

exact match search in with any name or meta key words etc

Magento/code/core/Mage/Catalogsearch/Block/Result copy this file into local folder and find this function   protected function _getProductCollection() and edit like below I am doing search like exact match with name and meta key words  protected function _getProductCollection()     {         if (is_null($this->_productCollection)) {         $serchQuery = str_replace('+', " ", Mage::app()->getRequest()->getParam('q'));         $collection = Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect('*')                 ->addAttributeToFilter(array(                     array('attribute'=>'name','like' => '%' . $serchQuery . '%'),                     array('attribute'=>'meta_keyword','like' => '%' . $serchQuery . '%'), ...

update order customer if mismatch after migration like any issue

Using csv we can update customer ids use this code in controller  if (($handle = fopen("/chroot/home/*****/*********/html/app/code/Test/Testmodule/Controller/Index/uniqueorder.csv", "r")) !== FALSE) {             while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {                 $email = $data[2]; //                 echo $url;die("data");                 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();                 $orderCollection = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\CollectionFactory');                 $collection = $orderCollection->create()                         ->addAttributeToSelect('*')     ...

Magento 2 Useful snippets for frontend developers

1 - Infos About Store Get Current store Id : inject \Magento\Store\Model\StoreManagerInterface $storeManage   $this->storeManager = $storeManager;   and get the id with :   $id = $this->storeManager->getStore()->getId();   get base Url :   $baseUrl = $this->storeManager->getStore()->getBaseUrl();     you can inject \Magento\Framework\UrlInterface and use use constants of class as parameters (UrlInterface::URL_TYPE_LINK) :       const URL_TYPE_LINK = 'link';     const URL_TYPE_DIRECT_LINK = 'direct_link';     const URL_TYPE_WEB = 'web';     const URL_TYPE_MEDIA = 'media';     const URL_TYPE_STATIC = 'static';     const URL_TYPE_JS = 'js';   get base currency code  or (getBaseCurrency() , getDefaultCurrencyCode(), getDefaultCurrency()):   $baseCurrency = $this->storeManager->getStore()->getBas...