Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

How to override ResultController of CatalogSearch action in magento


If you want to override your ResultController action of CatalogSearch module in your local than follow the following easy steps.

Step1: Create xml file in app/etc/modules

HK_Compare.xml
<?xml version="1.0"?>
<config>
    <modules>
        <HK_Compare>
            <active>true</active>
            <codePool>local</codePool>
        </HK_Compare>
    </modules>
</config>

Step2: Create controller file in app/code/local/HK/Compare/controllers

ResultController.php

Add following code in this .php file

include_once("Mage/CatalogSearch/controllers/ResultController.php");
class HK_Compare_ResultController extends Mage_Core_Controller_Front_Action
{
       public function indexAction()
       {
               echo "Module is comming from local"; //make sure changes working
       }
}

Step3: Create config.xml in app/code/local/HK/Compare/etc
<?xml version="1.0"?>
<config>
    <modules>
        <HK_Compare>
            <version>0.1.0</version>
        </HK_Compare>
    </modules>
    <frontend>
        <routers>
            <catalogsearch>
                <args>
                    <modules>
                        <company_modulename before="Mage_CatalogSearch">HK_Compare</company_modulename>
                    </modules>
                </args>
            </catalogsearch>
        </routers>
        <layout>
            <updates>
                <compare>
                    <file>compare.xml</file>
                </compare>
            </updates>
        </layout>
    </frontend>    
</config>

After this clear your magento cache and test your new changes. Hit like or leave comment if post is helpful for you.

PHP Sessions Not working for all pages


If you are getting problem like your php session not working in all pages of your site, than don't worry here you can easily solve your problem if your problem match to my problem i have previously in my site and i solve.

So suppose in your login page you started your session as...

session_start();
$_SESSION['islogin'] = 'true';

but your 'islogin' session is not working for all the pages except few pages. Than may be there is problem in your url. Please check there is a 'www' in your url. If not than add 'www' in your url and try again. If you want to add 'www' in your url using htaccess file than try following.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

Add these three lines of code in your .htaccess file.

May be this changes solve your problem.

How to create sales order invoice programmatically in magento


By default mangeto generate sales order invoice at the time when order payment is goes to paid.
But many times in magento some payment methods do not change order status to paid. So in this situation if you want to generate your order Invoice programmatically you can use following simple solution.

/*Create an invoice if not created yet*/
 $paymentMethod = $order->getPayment()->getMethodInstance()->getCode();

/*If you want to create only for few payment methods*/
if($paymentMethod=='YOUR PAYMENT METHOD''){
          if($order->hasInvoices()==0)
         {
               $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
               if (!$invoice->getTotalQty())
              {                          
                      $helper->log('Cannot create an invoice without products. '.$order->getRealOrderId());
                     $invoice-                 >setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
                     $invoice->register();
                     $transactionSave = Mage::getModel('core/resource_transaction')
                     ->addObject($invoice)
                     ->addObject($invoice->getOrder());
                     $transactionSave->save();
              }
         }
}

That's it.

How to Create Retina Graphics for your iphone or ipad


Retina Images serves different images based on the device being used by the viewer.

If your images not looking sharper in iphone or ipad Retina you need to make some change as following in your css. 

Suppose you have a logo.png(100px*50px) for normal browser view. You need to create double size of images for Retina. Like...

Your logo.png becomes (200px*100px) for Retina.

'<div class="logo"></div>'

Now add following css changes on top of your css file.

@media only screen and (min--moz-device-pixel-ratio: 2),
only screen and (-o-min-device-pixel-ratio: 2/1), 
only screen and (-webkit-min-device-pixel-ratio: 2), 
only screen and (min-device-pixel-ratio: 2){
     .logo{
           background: url("../images/nbitcoin@2x.png") no-repeat;
           background-size: 100px 50px;
}


Magento Redirect to Mobile theme programmatically


In my previous post i described how to redirect mobile theme in mobile browser.  Here you can see how it's work. But some time if you have created different cms page for different store it is not working in that redirection. In that redirection it will always call main store cms page.

If you getting such kind of problem you can now solve using my following solution. There is only few lines of code. Follow the step for redirect your main theme to mobile theme in mobile browser.

Step1: Add following code in your head.phtml file of your main theme(not in mobile theme).

<?php if($_SESSION['store'] =='') { ?>
 <script type="text/javascript">
 if (screen.width <= 720) {
      loadXMLDoc();
}
 function loadXMLDoc()
{
      var xmlhttp;
      if (window.XMLHttpRequest)
     {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
      }
       else
       {// code for IE6, IE5
             xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
  xmlhttp.onreadystatechange=function()
 {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
           location.reload(true);
       }
   }
  xmlhttp.open("GET","http://www.yourdomain.com/mobileajax.php",true);
 xmlhttp.send();
}
</script>
<?php }?>

Step2: Create one mobileajax.php file in your root and add following code in it.

<?php
     session_start();
     $_SESSION['store'] ='mobile';
?>


Step3: Now add following code in your index.php file after 'umask(0);' this line of code.

session_start();
if(isset($_SESSION['store']))
{
    if($_SESSION['store'] =='mobile'){
        $_GET['___store'] ='mobile' ;
    }
}else{
    $_SESSION['store']="";

}


Now, clear your cache in test in your mobile and desktop browser.


 

Copyright @ 2017 HKBlog.