Wednesday, December 4, 2013

Magento : Full Page Cache


Full Page Cache (FPC) is:
As in every thing that is generated from a script is written to HTML and served next time, improving performance (by reducing load and not having to generate the page for every visit).


In order to understand Magento Full Page Cache, we need to understand what the run function in Mage_Core_Model_App

public function run($params)
    {
        $options = isset($params['options']) ? $params['options'] : array();
        $this->baseInit($options);
        Mage::register('application_params', $params);

        if ($this->_cache->processRequest()) {
            $this->getResponse()->sendResponse();
        } else {
            $this->_initModules();
            $this->loadAreaPart(Mage_Core_Model_App_Area::AREA_GLOBAL, Mage_Core_Model_App_Area::PART_EVENTS);

            if ($this->_config->isLocalConfigLoaded()) {
                $scopeCode = isset($params['scope_code']) ? $params['scope_code'] : '';
                $scopeType = isset($params['scope_type']) ? $params['scope_type'] : 'store';
                $this->_initCurrentStore($scopeCode, $scopeType);
                $this->_initRequest();
                Mage_Core_Model_Resource_Setup::applyAllDataUpdates();
            }

            $this->getFrontController()->dispatch();
        }
        return $this;
    }

Here $this->_cache->processRequest()
line checks if you have defined a caching node like this under app/etc/test.xml

<config>
<global>
<cache>
<request_processors>
<lt>Bd_Fullpagecache_Model_Processor</lt>
</request_processors>
</cache>
</global>
</config>

The next thing that magento does is to find/initialize the class you have defined and it expects that you have an extractContent function defined in your model.

Friday, November 29, 2013

Magento : Dynamically adding and changing blocks

Store-specific handle:
<STORE_#> (where '#' is the store ID — if you only have one store it is most likely '1')

Category-specific handle:
<CATEGORY_#> (where '#' is the ID number of the current category)

Product-specific handle:
<PRODUCT_#> (where '#' is the ID number of the current product)

Product type-specific handle:
<PRODUCT_TYPE_NAME> (where 'name' is the product type, example: 'simple', 'grouped', 'configurable', etc.)

Customer logged in or out:
<CUSTOMER_LOGGED_STATUS> (where 'status' is either 'in' or 'out')

If CMS page:
<cms_page>

Close a div popup after clicking outside it

Remember that jQuery library must be included.

Use following js code

jQuery(document).mouseup(function (event){
    var divcontainer = jQuery(".forclose" ); // Object of DIV
    if (divcontainer.has(event.target).length === 0){
        divcontainer.hide();
    }
});

Sunday, September 29, 2013

Bestseller products in Magento

 Using query from database

SELECT `e`.*, `bs`.* FROM `catalog_product_entity` AS `e` INNER JOIN (SELECT `sales_flat_order_item`.`product_id`, SUM(`qty_ordered`) AS `count` FROM `sales_flat_order_item` GROUP BY `product_id`) AS `bs` ON bs.product_id = e.entity_id ORDER BY `bs`.`count` DESC;

And using the collection

$storeId    = Mage::app()->getStore()->getId();
        $products = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            //->addAttributeToSelect('*')
            ->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description', 'description')) //edit to suit tastes
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->setOrder('ordered_qty', 'desc'); //best sellers on top      
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

        //$products->setPageSize(6)->setCurPage(1);



Sunday, September 8, 2013

W3Schools JavaScript Quiz

W3Schools JavaScript Quiz

1. Inside which HTML element do we put the JavaScript?
You answered:<script>
 Correct Answer!

2. What is the correct JavaScript syntax to write "Hello World"?
You answered:document.write("Hello World");
 Correct Answer!

3. Where is the correct place to insert a JavaScript?
You answered:Both the <head> section and the <body> section are correct
 Correct Answer!

4. What is the correct syntax for referring to an external script called "xxx.js"?
You answered:<script src="xxx.js">
 Correct Answer!

5. The external JavaScript file must contain the <script> tag.
You answered:False
 Correct Answer!

6. How do you write "Hello World" in an alert box?
You answered:alert("Hello World");
 Correct Answer!

7. How do you create a function in JavaScript?
You answered:function myFunction()
 Correct Answer!

8. How do you call a function named "myFunction"?
You answered:myFunction()
 Correct Answer!

9. How to write an IF statement in JavaScript?
You answered:if (i==5)
 Correct Answer!

10. How to write an IF statement for executing some code if "i" is NOT equal to 5?
You answered:if (i != 5)
 Correct Answer!

11. How does a WHILE loop start?
You answered:while (i<=10)
 Correct Answer!

12. How does a FOR loop start?
You answered:for (i = 0; i <= 5; i++)
 Correct Answer!

13. How can you add a comment in a JavaScript?
You answered://This is a comment
 Correct Answer!

14. How to insert a comment that has more than one line?
You answered:/*This comment has
more than one line*/
 Correct Answer!

15. What is the correct way to write a JavaScript array?
You answered:var txt = new Array("tim","kim","jim")
 Correct Answer!

16. How do you round the number 7.25, to the nearest integer?
You answered:Math.round(7.25)
 Correct Answer!

17. How do you find the number with the highest value of x and y?
You answered:Math.max(x,y)
 Correct Answer!

18. What is the correct JavaScript syntax for opening a new window called "w2" ?
You answered:w2=window.open("http://www.w3schools.com");
 Correct Answer!

19. JavaScript is the same as Java.
You answered:False
 Correct Answer!

20. How can you detect the client's browser name?
You answered:navigator.appName
 Correct Answer!

Saturday, September 7, 2013

W3Schools PHP Quiz

W3Schools PHP Quiz

1. What does PHP stand for?
You answered:PHP: Hypertext Preprocessor
 Correct Answer!

2. PHP server scripts are surrounded by delimiters, which?
You answered:<?php…?>
 Correct Answer!

3. How do you write "Hello World" in PHP
You answered:echo "Hello World";
 Correct Answer!

4. All variables in PHP start with which symbol?
You answered:$
 Correct Answer!

5. What is the correct way to end a PHP statement?
You answered:;
 Correct Answer!

6. The PHP syntax is most similar to:
You answered:Perl and C
 Correct Answer!

7. How do you get information from a form that is submitted using the "get" method?
You answered:$_GET[];
 Correct Answer!

8. When using the POST method, variables are displayed in the URL:
You answered:False
 Correct Answer!

9. In PHP you can use both single quotes ( ' ' ) and double quotes ( " " ) for strings:
You answered:True
 Correct Answer!

10. Include files must have the file extension ".inc"
You answered:False
 Correct Answer!

11. What is the correct way to include the file "time.inc" ?
You answered:<?php include "time.inc"; ?>
 Correct Answer!

12. What is the correct way to create a function in PHP?
You answered:function myFunction()
 Correct Answer!

13. What is the correct way to open the file "time.txt" as readable?
You answered:fopen("time.txt","r");
 Correct Answer!

14. PHP allows you to send emails directly from a script
You answered:True
 Correct Answer!

15. What is the correct way to connect to a MySQL database?
You answered:mysqli_connect(host,username,password,dbname);
 Correct Answer!

16. What is the correct way to add 1 to the $count variable?
You answered:$count++;
 Correct Answer!

17. What is a correct way to add a comment in PHP?
You answered:/*…*/
 Correct Answer!

18. PHP can be run on Microsoft Windows IIS(Internet Information Server):
You answered:True
 Correct Answer!

19. In PHP, the die() and exit() functions do the exact same thing.
You answered:True
 Correct Answer!

20. Which one of these variables has an illegal name?
You answered:$my-Var
 Correct Answer!

Thursday, September 5, 2013

W3Schools SQL Quiz

W3Schools SQL Quiz

1. What does SQL stand for?
You answered:Structured Query Language
 Correct Answer!

2. Which SQL statement is used to extract data from a database?
You answered:SELECT
 Correct Answer!

3. Which SQL statement is used to update data in a database?
You answered:UPDATE
 Correct Answer!

4. Which SQL statement is used to delete data from a database?
You answered:DELETE
 Correct Answer!

5. Which SQL statement is used to insert new data in a database?
You answered:INSERT INTO
 Correct Answer!

6. With SQL, how do you select a column named "FirstName" from a table named "Persons"?
You answered:SELECT FirstName FROM Persons
 Correct Answer!

7. With SQL, how do you select all the columns from a table named "Persons"?
You answered:SELECT * FROM Persons
 Correct Answer!

8. With SQL, how do you select all the records from a table named "Persons" where the value of the column "FirstName" is "Peter"?
You answered:SELECT * FROM Persons WHERE FirstName='Peter'
 Correct Answer!

9. With SQL, how do you select all the records from a table named "Persons" where the value of the column "FirstName" starts with an "a"?
You answered:SELECT * FROM Persons WHERE FirstName LIKE 'a%'
 Correct Answer!

10. The OR operator displays a record if ANY conditions listed are true. The AND operator displays a record if ALL of the conditions listed are true
You answered:True
 Correct Answer!

11. With SQL, how do you select all the records from a table named "Persons" where the "FirstName" is "Peter" and the "LastName" is "Jackson"?
You answered:SELECT * FROM Persons WHERE FirstName='Peter' AND LastName='Jackson'
 Correct Answer!

12. With SQL, how do you select all the records from a table named "Persons" where the "LastName" is alphabetically between (and including) "Hansen" and "Pettersen"?
You answered:SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'
 Correct Answer!

13. Which SQL statement is used to return only different values?
You answered:SELECT DISTINCT
 Correct Answer!

14. Which SQL keyword is used to sort the result-set?
You answered:ORDER BY
 Correct Answer!

15. With SQL, how can you return all the records from a table named "Persons" sorted descending by "FirstName"?
You answered:SELECT * FROM Persons ORDER BY FirstName DESC
 Correct Answer!

16. With SQL, how can you insert a new record into the "Persons" table?
You answered:INSERT INTO Persons VALUES ('Jimmy', 'Jackson')
 Correct Answer!

17. With SQL, how can you insert "Olsen" as the "LastName" in the "Persons" table?
You answered:INSERT INTO Persons (LastName) VALUES ('Olsen')
 Correct Answer!

18. How can you change "Hansen" into "Nilsen" in the "LastName" column in the Persons table?
You answered:UPDATE Persons SET LastName='Nilsen' WHERE LastName='Hansen'
 Correct Answer!

19. With SQL, how can you delete the records where the "FirstName" is "Peter" in the Persons Table?
You answered:DELETE FROM Persons WHERE FirstName = 'Peter'
 Correct Answer!

20. With SQL, how can you return the number of records in the "Persons" table?
You answered:SELECT COUNT(*) FROM Persons
 Correct Answer!

Tuesday, August 27, 2013

Magento: Store locator in Magento using Google Map API

First you need to create a custom module with single model (one model-one table).
Suppose Bd_Storelocator is a custom module and Storelocator is a model,
Then use following code in any phtml file as
<?php
$requireData = Mage::getModel('storelocator/storelocator')->getStore();
//print_r($requireData->getData());
$markers = "";
$messages = "";
foreach ($requireData as $data) {
    $markers .= '{lat:' . $data["latitude"] . ',lng:' . $data["longitude"] . ',name:' . '"' . $data["storename"] . '"},';
    $messages .= "'" . addslashes($data["storename"]) . "<br/ >" . addslashes($data["address"]) . "<br />" . $data["telephone"] . "', ";    }
$markers1 = rtrim($markers, ",");
$messages1 = rtrim($messages, ", ");
?>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
    var map;
    google.maps.event.addDomListener(window, "load", initialize);
    function initialize() {
        var map = new google.maps.Map(document.getElementById("map_canvas"), {
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            disableAutoPan: false,
            streetViewControl: false
        });
        var markers = [<?php echo $markers1; ?>];
        var widt = document.body.offsetWidth;
        document.getElementById("map_canvas").width = widt+"px";
        for (index in markers) {
            if(Number(index) != index) break;
            addMarker(markers[index],map,index);
        }
        var bounds = new google.maps.LatLngBounds();
        for (index in markers) {
            if(Number(index) != index) break;
            var data = markers[index];
            bounds.extend(new google.maps.LatLng(data.lat, data.lng));
        }
        // Don't zoom in too far on only one marker
        if (bounds.getNorthEast().equals(bounds.getSouthWest())) {
            var extendPoint1 = new google.maps.LatLng(bounds.getNorthEast().lat() + 0.01, bounds.getNorthEast().lng() + 0.01);
            var extendPoint2 = new google.maps.LatLng(bounds.getNorthEast().lat() - 0.01, bounds.getNorthEast().lng() - 0.01);
            bounds.extend(extendPoint1);
            bounds.extend(extendPoint2);
        }
        map.fitBounds(bounds);
    }
    function addMarker(data,map,index) {
        // Create the marker
        //var image = '<?php //print $base_url . '/' . $themePath . '/images/drupal.png'  ?>';
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(data.lat, data.lng),
            map: map,
            //icon: image,
            title: data.name
        });
        attachSecretMessage(marker,index,map)

    }
    function attachSecretMessage(marker,number,map) {
        var message = [<?php echo $messages1; ?>];

        var infowindow = new google.maps.InfoWindow({
            content: message[number],
            size: new google.maps.Size(50,50)
        });
        google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map,marker);
           jQuery("#showinfo").html(message[number]);

        });
    }
</script>
<div class="mapBlock">
    <div id="map_canvas" style="height: 473px;">
    </div>
</div>
<div id="showinfo">

</div>

Adding a Google Map to your website

Use following code
<!DOCTYPE html>
<html>
  <head>
    <style>
      #map_canvas {
        width: 500px;
        height: 400px;
      }
    </style>
    <script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script>
      function initialize() {
        var map_canvas = document.getElementById('map_canvas');
        var map_options = {
          center: new google.maps.LatLng(44.5403, -78.5463),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(map_canvas, map_options)
      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map_canvas"></div>
  </body>
</html>

Friday, August 16, 2013

Magento : How to cache custom data into Magento

Use following code in configuration file as
<global>
    <cache>
        <types>
            <namespace_module module="namespace_module" translate="label description">
                <label>Your modules cache label</label>
                <description>Description of your modules cache</description>
                <tags>YOUR_MODULES_CACHE_TAGS</tags>
            </namespace_module>
        </types>
    </cache>
</global>
Then create a model class file witha variable and a method as
const CACHE_TAG_NAMESPACE_MODULE = 'YOUR_MODULES_CACHE_TAGS';
$cacheGroup = 'namespace_module';
$useCache = Mage::app()->useCache($cacheGroup);
if (true === $useCache) {
    // Cache is active
    $cacheId = "unique_name";
    if ($cacheContent = Mage::app()->loadCache($cacheId)) {
    $html = $cacheContent;
    return $html;
    }else{

    try {
        $cacheContent = $html;
        $tags = array(model::CACHE_TAG_NAMESPACE_MODULE);
        $lifetime = Mage::getStoreConfig('core/cache/lifetime');
        Mage::app()->saveCache($cacheContent, $cacheId, $tags, $lifetime);
        } catch (Exception $e) {
        // Exception = no caching
        Mage::logException($e);
    }
    return $html;
    }
} else {
    // Cache is not active
    return $html;
}

Also we can following code for clean the cache
Mage::app()->removeCache($cacheId);


By default Magento uses its file system to save cache (files located in the directory var/cache/).
Another option is the database, which is slower then the files option mostly because the database is a heavily used resource for Magento instances anyway. Then there are storage schemes that use RAM (which are much faster), e.g. APC (shared memory) or memcached (a networked caching server) or Redis.

The Magento cache is organized by tags, this means you have cache entries which belongs to a cache group.

APC is an opcode cache for PHP. It is a framework for caching and optimizing PHP code. APC also serves as a user cache. APC cache is stored in memory where performance gains over file system cache are huge.
There following step to configure APC in Magento
1. Install php5-apc package (like sudo apt-get install php5-apc)
2. Edit and configure setting in apc.ini
3. Add the following code between the global tags in app/etc/local.xml.
<global>
<cache>
            <backend>apc</backend>
            <prefix>mystore_</prefix>
        </cache>
</global>

There following step to configure Memcache as a Fast Backend in Magento
1. Install Memcache as a PHP extension (apt-get install memcached )
2. And enable it in your php.ini
3. Add the following code between the global tags in app/etc/local.xml.
<cache>
<backend>memcached</backend>
<!-- apc / memcached / empty=file -->
<memcached>
<!-- memcached cache backend related config -->
<servers>
<!-- any number of server nodes can be included -->
<server>
<host><![CDATA[127.0.0.1]]></host>
<port><![CDATA[11211]]></port>
<persistent><![CDATA[1]]></persistent>
</server>
</servers>
<compression><![CDATA[0]]></compression>
<cache_dir><![CDATA[]]></cache_dir>
<hashed_directory_level><![CDATA[]]></hashed_directory_level>
<hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
<file_name_prefix><![CDATA[]]></file_name_prefix>
</memcached>
</cache>

There following step to configure Redis as a Fast Backend in Magento
1. Install redis (2.4+ required)
2. Install phpredis
3. Install the magento extension “Cm_Cache_Backend_Redis”
4. Add the following code between the global tags in app/etc/local.xml.
<cache>                                              
<backend>Zend_Cache_Backend_Redis</backend>                            <slow_backend>database</slow_backend>                                  <slow_backend_store_data>0</slow_backend_store_data>                   <auto_refresh_fast_cache>0</auto_refresh_fast_cache>
<backend_options>                                              
<server><![CDATA[127.0.0.1]]></server>
<port><![CDATA[11211]]></port>                                              
<database>database</database>                                        
<use_redisent>0</use_redisent>  <!-- 0 for phpredis, 1 for redisent -->                                                <automatic_cleaning_factor>0</automatic_cleaning_factor> <!-- optional, 20000 is the default, 0 disables auto clean -->
</backend_options>
</cache>

Monday, July 29, 2013

Magento : Install Magento 1.7.0.2 With Sample Data By SSH

mkdir myproject
cd myproject
wget http://www.magentocommerce.com/downloads/assets/1.7.0.2/magento-1.7.0.2.tar.gz
wget http://www.magentocommerce.com/downloads/assets/1.6.1.0/magento-sample-data-1.6.1.0.tar.gz
tar -zxvf magento-1.7.0.2.tar.gz
tar -zxvf magento-sample-data-1.6.1.0.tar.gz
mv magento-sample-data-1.6.1.0/media/* magento/media/
mv magento-sample-data-1.6.1.0/magento_sample_data_for_1.6.1.0.sql magento/data.sql
mv magento/* magento/.htaccess .
chmod o+w var var/.htaccess app/etc
chmod -R o+w media
mysql -h localhost -u -p myproject_db < data.sql
chmod 755 mage
./mage mage-setup.

Wednesday, July 17, 2013

Magento : Create/Collocate Magento System Configuration

First create a blank module with following files
 etc/modules/Bd_Systemconfigdemo.xml
Bd/Systemconfigdemo/etc/config.xml

Then create a system.xml file in  Bd/Systemconfigdemo/etc/system.xml with following code
<?xml version="1.0"?>
<config>
      <tabs>
        <demo_tab translate="label" module="systemconfigdemo">
            <label>Demo Tab</label>
            <sort_order>0</sort_order>
        </demo_tab>
      </tabs>
      <sections>
        <demo_section  translate="label" module="systemconfigdemo">                   
        <label>Demo Section</label>
        <tab>demo_tab</tab>
        <frontend_type>text</frontend_type>
        <sort_order>0</sort_order>
        <show_in_default>1</show_in_default>
        <show_in_website>1</show_in_website>
        <show_in_store>1</show_in_store>           
                    <groups>
                      <demo_group translate="label">
                      <label>Demo Group</label>
                      <frontend_type>text</frontend_type>
                      <sort_order>0</sort_order>
                      <show_in_default>1</show_in_default>
                      <show_in_website>1</show_in_website>
                      <show_in_store>1</show_in_store>
                       <fields>
                          <demo_field1 translate="label">
                            <label>Demo Field1</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>0</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>first</comment>
                          </demo_field1>
                          <demo_field2 translate="label">
                            <label>Demo Field2</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>0</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>second</comment>
                          </demo_field2>
                       </fields>
                       </demo_group>
                    </groups>
        </demo_section>
      </sections>
</config>


And adminhtml.xml with following code
<?xml version="1.0"?>
    <config>
        <acl>
            <resources>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <demo_section translate="title" module="systemconfigdemo">
                                            <title>Demo Section Section</title>
                                            <sort_order>0</sort_order>
                                        </demo_section>
                                    </children>
                                </config>
                            </children>
                        </system>
                    </children>
                </admin>
            </resources>
        </acl>
    </config>

Magento : Add New Order Total (fee or discount, like prepaid discount)

Here i consider that i need to give a discount when user make a order with prepaid method.

Consider, Bd_Prepaid is a custom module and prepaiddiscount_total is a variable,

First create a simple module
1. etc/modules/Bd_Prepaid
<?xml version="1.0"?>
<config>
  <modules>
    <Bd_Prepaid>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Bd_Prepaid>
  </modules>
</config>

2.Then create a config file with following code
<?xml version="1.0"?>
<config>
  <modules>
    <Bd_Prepaid>
      <version>0.1.0</version>
    </Bd_Prepaid>
  </modules>
  <global>
    <helpers>
      <prepaid>
        <class>Bd_Prepaid_Helper</class>
      </prepaid>
    </helpers>
    <models>
      <prepaid>
        <class>Bd_Prepaid_Model</class>
        <resourceModel>prepaid_mysql4</resourceModel>
      </prepaid>
    </models>
    <resources>
      <salesattribute1374123678_setup>
        <setup>
          <module>Bd_Prepaid</module>
          <class>Mage_Sales_Model_Mysql4_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </salesattribute1374123678_setup>
      <salesattribute1374123678_write>
        <connection>
          <use>core_write</use>
        </connection>
      </salesattribute1374123678_write>
      <salesattribute1374123678_read>
        <connection>
          <use>core_read</use>
        </connection>
      </salesattribute1374123678_read>
    </resources>
    <events>
    <checkout_type_onepage_save_order_after> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_type_onepage_save_order_after_prepaiddiscount_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>prepaid/newordertotalobserver</class> <!-- observers class alias -->
            <method>savePrepaiddiscountTotal</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_type_onepage_save_order_after_prepaiddiscount_handler>
        </observers>
      </checkout_type_onepage_save_order_after>       
    <checkout_type_multishipping_create_orders_single> <!-- identifier of the event we want to catch -->
        <observers>       
          <checkout_type_multishipping_create_orders_single_prepaiddiscount_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>prepaid/newordertotalobserver</class> <!-- observers class alias -->
            <method>savePrepaiddiscountTotalForMultishipping</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_type_multishipping_create_orders_single_prepaiddiscount_handler>       
        </observers>
      </checkout_type_multishipping_create_orders_single>
    </events>   
     <sales>
        <quote>
            <totals>               
                <prepaiddiscount_total>
                    <class>prepaid/quote_address_total_prepaiddiscount</class>
                    <after>subtotal,freeshipping,tax_subtotal,shipping</after>
                    <before>grand_total</before>
                </prepaiddiscount_total>
            </totals>
        </quote>
            <order_invoice>
                <totals>               
                <prepaiddiscount_total>
                    <class>prepaid/order_invoice_total_prepaiddiscount</class>
                    <after>subtotal,freeshipping,tax_subtotal,shipping</after>
                    <before>grand_total</before>
                </prepaiddiscount_total>
                </totals>
            </order_invoice>
            <order_creditmemo>
                <totals>               
                <prepaiddiscount_total>
                    <class>prepaid/order_invoice_total_prepaiddiscount</class>
                    <after>subtotal,freeshipping,tax_subtotal,shipping</after>
                    <before>grand_total</before>
                </prepaiddiscount_total>
                </totals>
            </order_creditmemo>
    </sales>
  </global>
</config>

3.Then create amysql file to create a order attribute
<?php
$installer = $this;
$installer->startSetup();

$installer->addAttribute("quote_address", "prepaiddiscount_total", array("type"=>"varchar"));
$installer->addAttribute("order", "prepaiddiscount_total", array("type"=>"varchar"));
$installer->endSetup();
    
4.Then create a observer as used in config file
Like Bd/Prepaid/Model/Newordertotalobserver.php
<?php
class Bd_Prepaid_Model_Newordertotalobserver
{
     public function savePrepaiddiscountTotal(Varien_Event_Observer $observer)
    {
         $order = $observer -> getEvent() -> getOrder();
         $quote = $observer -> getEvent() -> getQuote();
         $shippingAddress = $quote -> getShippingAddress();
         if($shippingAddress && $shippingAddress -> getData('prepaiddiscount_total')){
             $order -> setData('prepaiddiscount_total', $shippingAddress -> getData('prepaiddiscount_total'));
             }
        else{
             $billingAddress = $quote -> getBillingAddress();
             $order -> setData('prepaiddiscount_total', $billingAddress -> getData('prepaiddiscount_total'));
             }
         $order -> save();
     }
   
     public function savePrepaiddiscountTotalForMultishipping(Varien_Event_Observer $observer)
    {
         $order = $observer -> getEvent() -> getOrder();
         $address = $observer -> getEvent() -> getAddress();
         $order -> setData('prepaiddiscount_total', $shippingAddress -> getData('prepaiddiscount_total'));
         $order -> save();
     }
}

5.Then create a class file to collect the total (Bd/Prepaid/Model/Quote/Address/Total/Prepaiddiscount.php), which inherited from Mage_Sales_Model_Quote_Address_Total_Abstract with following code
<?php
class Bd_Prepaid_Model_Quote_Address_Total_Prepaiddiscount
extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
     public function __construct()
    {
         $this -> setCode('prepaiddiscount_total');
         }
    /**
     * Collect totals information about prepaiddiscount
     *
     * @param Mage_Sales_Model_Quote_Address $address
     * @return Mage_Sales_Model_Quote_Address_Total_Shipping
     */
     public function collect(Mage_Sales_Model_Quote_Address $address)
    {
         parent :: collect($address);
         $items = $this->_getAddressItems($address);
         if (!count($items)) {
            return $this;
         }
         $quote= $address->getQuote();

         //amount definition

         $prepaiddiscountAmount = 0.01;

         //amount definition

         $prepaiddiscountAmount = $quote -> getStore() -> roundPrice($prepaiddiscountAmount);
         $this -> _setAmount($prepaiddiscountAmount) -> _setBaseAmount($prepaiddiscountAmount);
         $address->setData('prepaiddiscount_total',$prepaiddiscountAmount);

         return $this;
     }
   
    /**
     * Add prepaiddiscount totals information to address object
     *
     * @param Mage_Sales_Model_Quote_Address $address
     * @return Mage_Sales_Model_Quote_Address_Total_Shipping
     */
     public function fetch(Mage_Sales_Model_Quote_Address $address)
    {
         parent :: fetch($address);
         $amount = $address -> getTotalAmount($this -> getCode());
         if ($amount != 0){
             $address -> addTotal(array(
                     'code' => $this -> getCode(),
                     'title' => $this -> getLabel(),
                     'value' => $amount
                    ));
         }
       
         return $this;
     }
   
    /**
     * Get label
     *
     * @return string
     */
     public function getLabel()
    {
         return Mage :: helper('prepaid') -> __('Prepaid Discount');
    }
}

6.Then create a file to add this variable in invoice and an other file to add in memo
as
Bd/Prepaid/Model/Order/Invoice/Total/Prepaiddiscount.php
<?php
class Bd_Prepaid_Model_Order_Invoice_Total_Prepaiddiscount
extends Mage_Sales_Model_Order_Invoice_Total_Abstract
{
    public function collect(Mage_Sales_Model_Order_Invoice $invoice)
    {
        $order=$invoice->getOrder();
        $PrepaiddiscountTotal = $order->getPrepaiddiscountTotal();
        if ($PrepaiddiscountTotal&&count($order->getInvoiceCollection())==0) {
            $invoice->setGrandTotal($invoice->getGrandTotal()+$orderPrepaiddiscountTotal);
            $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal()+$orderPrepaiddiscountTotal);
        }
        return $this;
    }
}

Bd/Prepaid/Model/Order/Creditmemo/Total/Prepaiddiscount.php
<?php
class Bd_Prepaid_Model_Order_Creditmemo_Total_Prepaiddiscount
extends Mage_Sales_Model_Order_Creditmemo_Total_Abstract
{
    public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
    {

        return $this;

        $order = $creditmemo->getOrder();
        $orderPrepaiddiscountTotal        = $order->getPrepaiddiscountTotal();

        if ($orderPrepaiddiscountTotal) {
            $creditmemo->setGrandTotal($creditmemo->getGrandTotal()+$orderPrepaiddiscountTotal);
            $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal()+$orderPrepaiddiscountTotal);
        }

        return $this;
    }
}

7. At last add following code to app\code\core\Mage\Sales\Block\Order\Totals.php
${code}Total=$source->getprepaiddiscountTotal();

if($source->getOrderId()) {
    ${code}Total=$source->getOrder()->getprepaiddiscountTotal();
}

$this->_totals['prepaiddiscount_total'] = new Varien_Object(array(
    'code'  => 'prepaiddiscount_total',
    'field' => 'prepaiddiscount_total',
    'value' => $prepaiddiscountTotal,
    'label' => $this->__('Prepaid Discount')
));


And following code to app\code\core\Mage\Adminhtml\Block\Sales\Totals.php
$prepaiddiscountTotal=$this->getSource()->getprepaiddiscountTotal();

if($this->getSource()->getOrderId()) {
    $prepaiddiscountTotal=$this->getSource()->getOrder()->getprepaiddiscountTotal();
}
$this->_totals['prepaiddiscount_total'] = new Varien_Object(array(
        'code'      => 'prepaiddiscount_total',
        'value'     => $prepaiddiscountTotal,
        'base_value'=> $prepaiddiscountTotal,
        'label'     => $this->__('Prepaid Discount')
));



 

Tuesday, July 2, 2013

Magento : Make 'Continue Shopping' button redirect to the last-added-to-cart product's category

Edit cart.phtml and replace following code

<?php if($this->getContinueShoppingUrl()): ?>
    <button type="button" title="<?php echo $this->__('Continue Shopping') ?>" class="button btn-continue" onclick="setLocation('<?php echo $this->getContinueShoppingUrl(); ?>')"><span><span><?php echo $this->__('Continue Shopping') ?></span></span></button>
<?php endif; ?>

with

<?php
    $lastProductAddedToCartId = Mage::getSingleton('checkout/session')->getLastAddedProductId();
    if($lastProductAddedToCartId) {
        $productCategoryIdsArray = Mage::getModel('catalog/product')->load($lastProductAddedToCartId)->getCategoryIds();
        $continueShoppingCategoryUrl = Mage::getModel('catalog/category')->load($productCategoryIdsArray[0])->getUrl();
    }
?>
<?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; ?>

Thursday, June 20, 2013

Magento : Create CSV file for any custom data in magento

It can be done by using Varien_File_Csv class
Exp:

$file_path = '/your_dir_path/sample.csv';
$mage_csv = new Varien_File_Csv(); //mage CSV 
$products_ids = array(1,2,3); //product ids or make array of any data
$products_model = Mage::getModel('catalog/product');
$products_row = array();
foreach ($products_ids as $pid)
{
   $prod = $products_model->load($pid);
   $data = array();
   $data['sku'] = $prod->getSku();
   $data['name'] = $prod->getName();
   $data['price'] = $prod->getPrice();    
   $products_row[] = $data;              
}
$mage_csv->saveData($file_path, $products_row);

Sunday, June 16, 2013

Magento : Order State and Status in Magento

In Magento, there are two columns in sales_flat_order table.
These are state and status, They both are different. State is used by magento to tell if the order is new, processing, complete, holded, closed, canceled, etc.

while Statuses are the one that YOU would be defining at the backend in System -> Order Statuses. Magento displays order STATUSES and not STATES in the backend order detail page to let you know which status is assigned as per your mapping.

Remember, multiple statuses can be mapped with one state, while vice versa is not possible.

Also you may know more, reading file /app/code/core/Mage/Sales/etc/config.xml

Sunday, April 14, 2013

Magento : Import Gmail Contact

1. Define a link in any phtml file as
<?php
    $client_id=  '';                  
    $redirect_uri='test/test/gooledata';
    $scope = "https://www.google.com/m8/feeds/"; //google scope to access
    $state = "profile"; //optional                   
    $loginUrl = sprintf("https://accounts.google.com/o/oauth2/auth?scope=%s&state=%s&redirect_uri=%s&response_type=code&client_id=%s",$scope,$state,$redirect_uri,$client_id);
?>
Import E-mail Addresses From:<br />
<a href="javascript:poptastic('<?php echo $loginUrl; ?>');">Import</a>



<script>
function poptastic(url) {
      var newWindow = window.open(url, 'name', 'height=600,width=450,top=100,left=500');
      if (window.focus) {
        newWindow.focus();
      }
     
 }
function showGmailData(){
       //location.reload();
       jQuery("#gmailloader").css('display', 'block');
            jQuery.ajax({
                    type: "POST",
                    data : jQuery("#form-validate").serialize(),
                    url: '/invitation/index/showgmaildata',
                    success: function(data) {
                    jQuery("#gmailloader").css('display', 'none');
                    result = jQuery.parseJSON(data);
                    if(!result.success) {
                    }
                    else    {

                    }
              }
            });
 }
</script>

2. Define Redirect action in controller
public function googledataAction(){        
              
        $authcode= $this->getRequest()->getParam('code');
        $importcontacts = array();
        if(isset($authcode) && $authcode != ''){
           $importcontacts = Mage::helper('test')->importGmailContacts($authcode);
           //print_r($importcontacts);
           //Mage::getSingleton('core/session')->setImportcontacts($importcontacts);
           echo '<html><body>Please wait....<script type="text/javascript">window.opener.showGmailData(); window.close();</script></body></html>';
           die();
          
        }       
    }

3. Define importGmailContacts() method in helper
public function importGmailContacts($authcode){       
            try{
            $clientid='';
            $clientsecret='';
            $redirecturi='test/test/googledata';
            $fields=array(
            'code'=>  urlencode($authcode),
            'client_id'=>  urlencode($clientid),
            'client_secret'=>  urlencode($clientsecret),
            'redirect_uri'=>  urlencode($redirecturi),
            'grant_type'=>  urlencode('authorization_code')
            );
            $fields_string='';
            foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
            $fields_string=rtrim($fields_string,'&');
            //open connection
            $ch = curl_init();
            //set the url, number of POST vars, POST data
            curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
            curl_setopt($ch,CURLOPT_POST,5);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
            // Set so curl_exec returns the result instead of outputting it.
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            //to trust any ssl certificates
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            //execute post
            $result = curl_exec($ch);
            //close connection
            curl_close($ch);
            //extracting access_token from response string
            $response   =  json_decode($result);
            $accesstoken= $response->access_token;
            if( $accesstoken!='')
           
            $xmlresponse=  file_get_contents('https://www.google.com/m8/feeds/contacts/default/full?max-results=100&oauth_token='. $accesstoken);
            //reading xml using SimpleXML
            $xml=  new SimpleXMLElement($xmlresponse);
            $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
            $result = $xml->xpath('//gd:email');
            foreach ($result as $title) {
                $importcontact[] = (string)$title->attributes()->address;
            }
            Mage::getSingleton('core/session')->setImportcontacts($importcontact);
            //return $importcontact;
            }  catch (Exception $e){ echo $e->getMessage();}
    }

4.Then use according to requirement in following action
public function showGmailDataAction(){
        $result = array(
                'success' => false,
                'message' => false,
                'count' => false,
            );
$importcontacts = Mage::getSingleton('core/session')->getImportcontacts();
$this->getResponse()->setBody(Zend_Json::encode($result));
        return;
    }
               

Magento : Custom router in Magento

Suppose, we need to create a custom url like 'abc' for test/test/index

1. Define an event in config.xml
<events>
            <controller_front_init_routers>
                <observers>
                    <test>
                        <class>Bd_Test_Controller_Router</class>
                        <method>initControllerRouters</method>
                    </test>
                </observers>
            </controller_front_init_routers>
 </events>

2. Then define router class in Controller folder
<?php

class Bd_Test_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
{
    /**
     * Initialize Controller Router
     *
     * @param Varien_Event_Observer $observer
     */
    public function initControllerRouters($observer)
    {
        /* @var $front Mage_Core_Controller_Varien_Front */
        $front = $observer->getEvent()->getFront();

        $front->addRouter('test', $this);
    }

    /**
     * Validate and Match Cms Page and modify request
     *
     * @param Zend_Controller_Request_Http $request
     * @return bool
     */
    public function match(Zend_Controller_Request_Http $request)
    {
        if (!Mage::isInstalled()) {
            Mage::app()->getFrontController()->getResponse()
                ->setRedirect(Mage::getUrl('install'))
                ->sendResponse();
            exit;
        }

        $identifier = trim($request->getPathInfo(), '/');

        $condition = new Varien_Object(array(
            'identifier' => $identifier,
            'continue'   => true
        ));
       
       
      
        if ($condition->getRedirectUrl()) {
            Mage::app()->getFrontController()->getResponse()
                ->setRedirect($condition->getRedirectUrl())
                ->sendResponse();
            $request->setDispatched(true);
            return true;
        }

        if (!$condition->getContinue()) {
            return false;
        }

      
        if ($identifier!='abc') {
            return false;
        }

        $request->setModuleName('test')
            ->setControllerName('test')
            ->setActionName('index');
          
        $request->setAlias(
            Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
            $identifier
        );       

        return true;
    }
}


Tuesday, March 12, 2013

Back To Top on Webpage

1.html
<div id="scroll-to-top" style="display:none">Back to Top</div>

2. JS
<script language="javascript">
jQuery(function() {
    jQuery(window).scroll(function() {
        if(jQuery(this).scrollTop() != 0) {
            jQuery('#scroll-to-top').fadeIn();  
        } else {
            jQuery('#scroll-to-top').fadeOut();
        }
    });

    jQuery('#scroll-to-top').click(function() {
        jQuery('body,html').animate({scrollTop:0},800);
    });  
});
</script> 

3.CSS

#scroll-to-top {
display:none;
position:fixed;
width:50px;
height:50px;
bottom:30px;
right:30px;
z-index:9999;
text-indent:-9999px;
border-radius:50%;
background-color: #e5e5e5;
}
#scroll-to-top:hover {
background-position:-200px -150px;
background-color:#333;
}

Tuesday, March 5, 2013

How to add a "Tweet" button to a webpage?

1. HTML
<a href="javascript:poptastic('http://twitter.com/intent/tweet?text=<?php echo $message; ?>&url=<?php echo $url ?>');" title="Share With Twitter">Tweet</a>

2. JS
function poptastic(url) {
      var newWindow = window.open(url, 'name', 'height=600,width=450,top=100,left=500');
      if (window.focus) {
        newWindow.focus();
      }
     
 }

Monday, February 25, 2013

MAgento : Add custom action to Sales Order Grid

Here we use an event core_block_abstract_prepare_layout_before
1. Add following script in config.xml
<events>
<core_block_abstract_prepare_layout_before>
        <observers>
                <bdaction_block_abstract_prepare_layout_before>
                        <type>singleton</type>
                        <class>Bd_Test_Model_Observer</class>
                        <method>addMassCustomAction</method>
                </bdaction_block_abstract_prepare_layout_before>
        </observers>
</core_block_abstract_prepare_layout_before>
</events>
2.Create a observer class like Bd_Test_Model_Observer
then add a method
public static function addMassCustomAction($observer)
{
    $block = $observer->getEvent()->getBlock();
    if(get_class($block) =='Mage_Adminhtml_Block_Widget_Grid_Massaction'
        && $block->getRequest()->getControllerName() == 'sales_order')
    {
        $block->addItem('customaction', array(
        'label' => 'Custom Action',
        'url' => Mage::helper('adminhtml')->getUrl('module/controller/action'),
        ));
    }
}
3.At last create that controller and action method

Sunday, February 24, 2013

Magento : Session Storage


Typically, database servers are less utilised than web servers. Performance can usually be improved by moving session handling from the web server filesystem into the database server. Magento asks you whether you prefer a file or database session save type, but this is very easy to change in the XML. Open up /var/www/app/etc/local.xml, locate the <session_save> tags, and change the value so it looks like this (the default is <![CDATA[files]]>):

<session_save><![CDATA[db]]></session_save>
Another alternative is to use memcache for storing the sessions - this isn’t as persistent as the database store, but may be faster:

<session_save><![CDATA[memcache]]></session_save>
You can add details of your memcache server in the session save path:

<session_save_path><![CDATA[tcp://127.0.0.1:20?persistent=1&weight;=2&timeout;=10&retry;_interval=10]]></session_save_path>

Magento : Changing the Admin URL


We can improve security by changing the default URL from 'admin' to something more obscure, such as 'bdadmin'. This will decrease the probability that a malicious user will hit upon your admin log-in page by guessing that the admin site is located at domain.com/admin/, or that automated scripts scanning for Magento admin pages will find it.
To change the admin address you first need to stop Apache and clear the cache:
root# /etc/init.d/apache2 stop
root# rm -rf /var/www/var/cache/*
root# rm -rf /var/www/var/session/*
Then open up the /var/www/app/etc/local.xml file, locate the <frontName> tag, and change the 'admin' part it to something a lot more random,
eg:  <frontName><![CDATA[gJpf2VK5]]></frontName>
Then set Apache running again (bearing in mind your site will be down for the duration of this operation!):
root# /etc/init.d/apache2 start

Saturday, February 23, 2013

Magento: Get the list of events from magento


Use following code in action of magento
$eventAreas = array('global','frontend','adminhtml');
foreach ($eventAreas as $eventArea) {
    $eventConfig = Mage::app()->getConfig()
                       ->getNode(sprintf('%s/events', $eventArea));
    foreach($eventConfig->children() as $key=>$value)
    {
        foreach($value->observers->children() as $key1=>$value1)
        {
            $observer_method = array((string)$eventArea,
            (string)$key,
            (string)$key1,
            (string)Mage::app()->getConfig()
                        ->getModelClassName($value1->class),
            (string)$value1->method);
             echo '<pre>';
             print_r($observer_method);
             echo '</pre>';
        }
    }
}

Magento : Add SEO content in footer

There are following unique pages as category, product and cms pages.
Here, we will create an attribute for these pages as
1.CMS Page

<?php
$installer = $this;
$installer->startSetup();
$installer->run('
    ALTER TABLE '. $this->getTable("cms_page") .
         ' ADD column seo_footer_cms_content text NULL;
');
$installer->endSetup();
?>
2.Category Page


<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_category', 'seo_footer_category_content', array(
                        'type'              => 'text',
                        'backend'           => '',
                        'frontend'          => '',
                        'label'             => 'SEO Footer Content',
                        'input'             => 'textarea',
                        'class'             => '',
                        'source'            => '',
                        'global'            => 0,
                        'visible'           => 1,
                        'required'          => 0,
                        'user_defined'      => 1,
                        'default'           => '',
                        'searchable'        => 0,
                        'filterable'        => 0,
                        'comparable'        => 0,
                        'visible_on_front'  => 0,
                        'unique'            => 0,
                        'position'          => 1,
                    ));
$installer->endSetup();
?>
3.Product page

<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'seo_footer_product_content', array(
                        'type'              => 'text',
                        'backend'           => '',
                        'frontend'          => '',
                        'label'             => 'SEO Footer Content',
                        'input'             => 'textarea',
                        'class'             => '',
                        'source'            => '',
                        'global'            => 0,
                        'visible'           => 1,
                        'required'          => 0,
                        'user_defined'      => 1,
                        'default'           => '',
                        'searchable'        => 0,
                        'filterable'        => 0,
                        'comparable'        => 0,
                        'visible_on_front'  => 0,
                        'unique'            => 0,
                        'position'          => 1,
                    ));                
$installer->endSetup();
?>

Now you can see corresponding attribute of product and category in admin.
But for cms page we have to add following code (use adminhtml_cms_page_edit_tab_meta_prepare_form event). Lets add below code in config.xml

<adminhtml>
    <events>
        <adminhtml_cms_page_edit_tab_meta_prepare_form>
            <observers>
                <bd_cms_page_edit_tab_meta>
                    <type>singleton</type>
                    <class>Bd_Attributes_Model_Observer</class>
                    <method>createCMSAttribute</method>
                </bd_cms_page_edit_tab_meta>
            </observers>
        </adminhtml_cms_page_edit_tab_meta_prepare_form>
    </events>
</adminhtml>
now create createCMSAttribute function in Bd/Attribures/Model/Observer.php file

public function createCMSAttribue($observer)
{
    $form = $observer->getEvent()->getForm();
    $fieldset = $form->getElement('meta_fieldset');
    $fieldset->addField('seo_footer_cms_conten', 'editor', array(
        'name' => 'seo_footer_cms_content',
        'label' => Mage::helper('cms')->__('SEO Footer Content'),
        'title' => Mage::helper('cms')->__('SEO Footer Content'),
        'style'     => '',
        'wysiwyg'   => false,
        'disabled'  => 0
    ));
}

To show the content in the footer we need to add the following code in footer.phtml

<?php
   $routeName=Mage::app()->getFrontController()->getRequest()->getRouteName();
   $currentCategory = Mage::registry('current_category');
   $currentProduct = Mage::registry('current_product');
   if ($currentProduct):
    echo $currentProduct->getSeoFooterProductContent();
   elseif ($currentCategory):
    echo $currentCategory->getSeoFooterCategoryContent();
   elseif( $routeName == 'cms'):
    $page = Mage::getSingleton('cms/page');  
    echo $page->getSeoFooterCmsContent();
  endif;
  ?>