Pages

Magento 1.9 - Find what event using in Observer.

Following code return what event used in the observer,

app\etc\modules\Gta_FindPaymentGateway.xml

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

app\code\local\Gta\FindPaymentGateway\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Gta_FindPaymentGateway>
            <version>1.0.0</version>
        </Gta_FindPaymentGateway>
    </modules>
    <global> 
        <models>
            <gta_findpaymentgateway>
                <class>Gta_FindPaymentGateway_Model</class>
            </gta_findpaymentgateway>
        </models> 
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <Gta_FindPaymentGateway_Model_Observer>   
                        <type>singleton</type>
                        <class>Gta_FindPaymentGateway_Model_Observer</class>
                        <method>paymenter</method>
                    </Gta_FindPaymentGateway_Model_Observer>
                </observers>
            </checkout_cart_product_add_after>      
        </events>
    </global> 
</config>

app\code\local\Gta\FindPaymentGateway\Model\Observer.php

<?php
    class Gta_FindPaymentGateway_Model_Observer
    {
        public function paymenter($Observer)
        {
            $order = $Observer->getEvent();
            Mage::log($order->getName(),null,'eventsss.log',true);
        }
    }
?>

Magento 1.9 - Find what are all functions overwritten magento core function?

Create file into your Magento root folder with the name of find.php and run,


 <?php  
 $foldersToCheck = array('app/code/local/', 'app/code/community/');  
 $configFiles = array();  
 foreach ($foldersToCheck as $folder){  
   $files = glob($folder.'*/*/etc/config.xml');//get all config.xml files in the specified folder  
   $configFiles = array_merge($configFiles, $files);//merge with the rest of the config files  
 }  
 $rewrites = array();//list of all rewrites  
 foreach ($configFiles as $file){  
   $dom = new DOMDocument;  
   $dom->loadXML(file_get_contents($file));  
   $xpath = new DOMXPath($dom);  
     $path = '//rewrite/*';//search for tags named 'rewrite'  
     $text = $xpath->query($path);  
     foreach ($text as $rewriteElement){  
       $type = $rewriteElement->parentNode->parentNode->parentNode->tagName;//what is overwritten (model, block, helper)  
       $parent = $rewriteElement->parentNode->parentNode->tagName;//module identifier that is being rewritten (core, catalog, sales, ...)  
       $name = $rewriteElement->tagName;//element that is rewritten (layout, product, category, order)  
       foreach ($rewriteElement->childNodes as $element){  
         $rewrites[$type][$parent.'/'.$name][] = $element->textContent;//class that rewrites it  
       }  
     }  
 }  
 echo "<pre>";print_r($rewrites);  
 ?>  

PHP - Find duplicate values in array

 <?php  
 $one = array(1,2,3,4,1,2,3,4,5,6,7);  
 $duplicates = findto($one);  
 function findto($one)  
 {  
   return array_diff_assoc($one, array_unique($one));  
 }  
 echo print_r($duplicates);  

Magento 1.9 - Add Discount Percentage Below Price

Copy price.phtml from :

app/design/frontend/base/default/template/catalog/product/price.phtml

Paste price.phtml to :

app/design/frontend/default/theme/template/catalog/product/price.phtml

Find :

<?php endif; /* if ($_finalPrice == $_price): */ ?>

Add above it:

    <!-- start custom code  -->

     <?php // Discount percents output start 
     $_finalPrice=$_product->getFinalPrice(); 
     $_price=$_product->getPrice();?>

    <?php if($_finalPrice < $_price): ?>
    <?php $_savePercent = 100 - round(($_finalPrice / $_price)*100); ?>
    <?php echo "Your Savings ".$_savePercent; ?>%
    <?php endif; ?>
    <?php // Discount percent output end ?>


    <!-- end custom code  -->





How to fix Magento 1.9 Email sending problem

In the latest Magento versions, all emails are being sent via CRON job.
Every email will be queued and sent will depend on the set interval.


By default, Magento has already set CRON jobs at 
System -> Configuration -> Advanced -> System -> Advanced -> Cron


If the CRON job is not working properly, please follow the following steps to send instant emails.


Copy Template.php file from 
app -> code -> core -> Mage -> Core -> Model -> Email


Create folder structure like 
app -> code -> local ->  Mage -> Core -> Model -> Email


Paste Template.php file in an Email folder


Open Template.php file and go to line number 407 or search for the following code 
 if ($this->hasQueue() && $this->getQueue() instanceof Mage_Core_Model_Email_Queue) {


Replace above code with following
 if (!($this->hasQueue()) && $this->getQueue() instanceof Mage_Core_Model_Email_Queue) {


This trick works perfectly on Magento Version 1.9.2.0

MAGENTO2 - SET CRON JOB WAMP IN WINDOWS LOCALHOST


    Magento 2, there are three cron jobs required. Because windows don’t have cron, we have to use a bat file and configure it to run as a scheduled task as shown below:

1.Find the location of php.exe – In you have Wamp installed, it’s in Drive:\wamp\bin\php\php5.6.25 e.g c:\wamp\bin\php\php5.6.25
2.Go to a folder and create a file named yourcron.bat
3.Edit this file and paste 3 lines shown below:
in the following file, I have added to cron job for re-indexing and cache


C:\wamp\bin\php\php5.6.25\php.exe C:\wamp\www\magento2\bin\magento cron:run
C:\wamp\bin\php\php5.6.25\php.exe C:\wamp\www\magento2\update\cron.php
C:\wamp\bin\php\php5.6.25\php.exe C:\wamp\www\magento2\bin\magento setup:cron:run
C:\wamp\bin\php\php5.6.25\php.exe C:\wamp\www\magento2\bin/magento indexer:reindex
C:\wamp\bin\php\php5.6.25\php.exe C:\wamp\www\magento2\bin/magento cache:flush

4. Save the file, open command prompt and enter the following command:

schtasks /create /sc minute /mo 5 /tn Magento\Cron /tr C:\wamp\bin\php\php5.6.25\yourcron.bat

5. Now click on Start –> Control Panel –> Administrative Tools –> Task Scheduler
6. Expand Task Schedule Library from the left-hand column and click Magento.
7. Select Cron from the middle pane, click Properties from the right pane under selected items.
8. Under Security Options, click the Change User or Group button.
9. Now in the current window, enter System username in the object name box and click check names. Click OK to close the User window, and OK to close the Cron properties Window, and close Task Scheduler.
10. Cron job setup is successful and it will run every 5 minutes.

Magento 1 to 2 Migration Commands for Store Setting & Data Setting

 Migration Commands

Migrating Store Setting :

php bin/magento migrate:settings --reset vendor/magento/data-migration-tool/etc/opensource-to-opensource/1.9.2.3/config.xml

Migrating Data Setting :

php bin/magento migrate:data --reset vendor/magento/data-migration-tool/etc/opensource-to-opensource/1.9.2.3/config.xml

Difference between Mage::getResourceModel() vs Mage::getModel() vs Mage::getSingleton() in Magento


Mage::getSingleton()

Mage::getSingleton() will first check if the same class instance exists or not in the memory. If the instance exists then it will return the same object from the memory. So Mage::getSingleton() is faster than Mage::getModel().

Example

$product1 = Mage::getSingleton('catalog/product');
$product2 = Mage::getSingleton('catalog/product');
$product1 and $product2 both will share same memory of OS and return only one instance each time.

Mage::getModel()

Mage::getModel() will create a new instance of an object each time even such an object exists in the configuration.

Example

$product1 = Mage::getModel('catalog/product');
$product2 = Mage::getModel('catalog/product');
$product1 and $product2 both have different instant of same object and also occupy different memory

Mage::getResourceModel()

As far as I know, all collections in Magento are resource models. They are instantiated by

Mage::getResourceModel()

or

Mage::getModel()->getCollection()

It doesn't really matter which function you use; the latter one simply calls the first one. The Magento team simply chose to make collections part of the resource, probably because collections need to query the database a lot. Usually, you will not have to call Mage::getResourceModel() for anything else than collections.

Install Magento 2.3.3 - Wamp 3 - Windows 7

Steps to be followed :

1) Install Composer

     > composer install

2) Upload the latest Magento to = > wamp/www/magento2 & Create Database.

3)  using cmd run following,

      php bin/magento setup:install --base-url=http://localhost/magento2 --db-host=localhost --db-name="magento2" --db-user="root" --db-password="" --admin-firstname="abc" --admin-lastname="abc" --admin-email=abc@gmail.com --admin-user="magento2" --admin-password="magento2123" --language="en_US" --currency="INR" --timezone="Asia/Kolkata" --use-rewrites="1"

4) If backend not working, update the file
magento2/vendor/magento/framework/View/Element/Template/File/Validator.php

find 

protected function isPathInDirectories($path, $directories)

replaced entire function with following code :


protected function isPathInDirectories($path, $directories)
{
    if (!is_array($directories)) {
        $directories = (array)$directories;
    }
    $realPath = $this->fileDriver->getRealPath($path);
    $realPath = str_replace('\\', '/', $realPath); // extra code added
    foreach ($directories as $directory) {
        if (0 === strpos($realPath, $directory)) {
            return true;
        }
    }
    return false;
}


5) Open di.xml file inside magento2/app/etc/di.xml

   replace the word "Symlink" with the "Copy" in the file and save the file.


6) Enjoy :)



Add WordPress Header & Footer for custom Development page.

Add following code to your main file or where you want header & footer


<?php
require_once(rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/wp-load.php');
get_header();
?>

--------------------
--------------------
--------------------
--------------------
--------------------
<?php
get_footer();
?>