Magento: Sample apache virtualhost for your website
Sample apache virtualhost to point to your magento directory and run your local website with specified URL.
1 2 3 4 5 6 7 8 | <VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/var/www/magento/" ServerName loca.lho.st ServerAlias loca.lho.st ErrorLog "logs/error_log" CustomLog "logs/access_log" common </VirtualHost> |
Add entry to /etc/hosts too:
1 | 127.0.0.1 loca.lho.st |
Restart apache (service httpd restart OR service apache2 restart) and point your browser location to:
1 | http://loca.lho.st |
and you will see the website running from your /var/www/magento directory.
Magento: Clear all caches from command line
Magento clear all caches from command line, programatically from ssh. Clearing the caches is a must when you are making any configuration changes in your Magento website. Although you can always clear the cache from admin panel, sometimes for faster cleaning or unable to log into admin panel reason, it’s good to have a script which will clear all the caches in Magento.
Create a file in your Magento root and name it clearCache.php with the below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php
echo "Start Cleaning all caches at ... " . date("Y-m-d H:i:s") . "\n\n";
ini_set("display_errors", 1);
require 'app/Mage.php';
Mage::app('admin')->setUseSessionInUrl(false);
Mage::getConfig()->init();
$types = Mage::app()->getCacheInstance()->getTypes();
try {
echo "Cleaning data cache... \n";
flush();
foreach ($types as $type => $data) {
echo "Removing $type ... ";
echo Mage::app()->getCacheInstance()->clean($data["tags"]) ? "Cache cleared!" : "There is some error!";
echo "\n";
}
} catch (exception $e) {
die("[ERROR:" . $e->getMessage() . "]");
}
echo "\n";
try {
echo "Cleaning stored cache... ";
flush();
echo Mage::app()->getCacheInstance()->clean() ? "Cache cleared!" : "There is some error!";
echo "\n\n";
} catch (exception $e) {
die("[ERROR:" . $e->getMessage() . "]");
}
?> |
Make sure all the double quotes comes good in copy pasting.
You can now run this script by the command “php -f clearCache.php” from your magento root location in terminal and this will start clearing all the caches for you! Once done, it will confirm by the message “Cache cleared!” or giving error message if it fails.
Magento: Enable logs on API calls
There is no setting to enable/disable API calls in Magento, but you can do so by modifying a file which handles all the API calls and logging the requests in a custom log file. This is useful when you know that there are many API calls being requested by third party applications to your website but not sure what is actually being called, how many times, and at what day and time.
With that said, open the file: app/code/core/Mage/Api/Model/Server/Handler/Abstract.php
and find the function “call” which handles all the API calls. Scroll down the function and find the if block which says:
1 2 3 4 5 6 7 8 9 10 11 | if (method_exists($model, $method)) {
if (isset($methodInfo->arguments) && ((string)$methodInfo->arguments) == 'array') {
return $model->$method((is_array($args) ? $args : array($args)));
} elseif (!is_array($args)) {
return $model->$method($args);
} else {
return call_user_func_array(array(&$model, $method), $args);
}
} else {
throw new Mage_Api_Exception('resource_path_not_callable');
} |
and replace it with:
1 2 3 4 5 6 7 8 9 10 11 12 13 | if (method_exists($model, $method)) {
Mage::log($method, null, 'api.log'); //logs the method
Mage::log($args, null, 'api.log'); //logs the arguments
if (isset($methodInfo->arguments) && ((string)$methodInfo->arguments) == 'array') {
return $model->$method((is_array($args) ? $args : array($args)));
} elseif (!is_array($args)) {
return $model->$method($args);
} else {
return call_user_func_array(array(&$model, $method), $args);
}
} else {
throw new Mage_Api_Exception('resource_path_not_callable');
} |
Now you should have your var/log/api.log file filled with the “method” and “arguments” requested by third party systems on your server. This should help you in knowing what was requested and at what time in your new log file.
Magento continue shopping link to last added product’s category page
In Magento checkout cart, link Continue shopping button to last added product’s category page. Below code will check last added product’s id in cart and gets the last category assigned to the product.
Put below code at the start of the checkout/cart.phtml file
1 2 3 4 5 6 | $lastProductIdAddedToCart = Mage::getSingleton('checkout/session')->getLastAddedProductId();
if($lastProductIdAddedToCart) {
$productCategoryIdsArray = Mage::getModel('catalog/product')->load($lastProductIdAddedToCart)->getCategoryIds();
//print_r($productCategoryIdsArray);
$continueShoppingCategoryUrl = Mage::getModel('catalog/category')->load(end($productCategoryIdsArray))->getUrl();
} |
Replace continue shopping button with below code, in the checkout/cart.phtml file
1 2 3 | <?php if($this->getContinueShoppingUrl()): ?> <button type="button" title="<?php echo $this->__('Continue Shopping') ?>" class="button btn-continue" onclick="setLocation('<?php echo (isset($continueShoppingCategoryUrl)) ? $continueShoppingCategoryUrl : $this->getContinueShoppingUrl(); ?>')"><span><span><?php echo $this->__('Continue Shopping') ?></span></span></button> <?php endif; ?> |
Magento display categories and sub-categories
Magento display categories and sub-categories. Below code will show all the parent and child categories along with show/hide functionalities by jQuery.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <div class="block block-layered-nav"> <div class="block-title"> <strong><span><?php echo $this->__('Shop By Category') ?></span></strong> </div> <div class="block-content"> <?php $productid = Mage::registry('current_product')->getId(); $product = Mage::getSingleton('catalog/product')->load($productid); $parentIds = $product->getCategoryIds(); $parentId = $parentIds[0]; $_categories = Mage::getBlockSingleton('catalog/navigation'); foreach ($_categories->getStoreCategories() as $_category) { $category = Mage::getModel('catalog/category'); $category->load($_category->getId()); $subcategories = explode(',', $category->getChildren()); if(!in_array($_category->getId(),$parentIds)) { $hide="display:none"; $inactive="inactive"; } else { $hide=""; $inactive="active"; } ?> <div style="padding:5px 5px 0px 10px"> <div class="cat parent <?php echo $inactive;?>" style="font-size:15px"><?php echo $_category->getName() ?></div> <div class="child" style="<?php echo $hide;?>"> <?php foreach ($subcategories as $subcategoryId) { $category->load($subcategoryId); if($category->getChildren() == '') { if(in_array($subcategoryId,$parentIds)) { $bold = "font-weight:bold"; } else { $bold = ""; } echo '<div class="subcat" style="'.$bold.'"><a href="' . $category->getURL() . '">' . $category->getName() . '</a></div>'; } else {?> <div class="subcat"> <div class="parent active"><?php echo $category->getName() ?></div> <div class="child"> <?php $subsubcategories = explode(',', $category->getChildren()); foreach($subsubcategories as $subsubcatid) { if(in_array($subsubcatid, $parentIds)) { $bold = "font-weight:bold"; } else { $bold = ""; } $category->load($subsubcatid); echo '<div style="padding-left:10px;'.$bold.'"><a href="' . $category->getURL() . '">' . $category->getName() . '</a></div>'; } ?> </div> </div> <?php } } ?> </div> </div> <?php } ?> </div> </div> <script type="text/javascript"> jQuery('.block-content .cat').click(function(){ var t = jQuery(this); if(jQuery(this).next().css('display')=='none') { jQuery('.col-left .block-content .child').hide(); t.next().show(); t.next().find('div.child').show("slow"); } else { t.next().hide(); t.next().find('div.child').hide("slow"); } }); </script> |
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)