After Installation Change Magento Admin Path

Changing admin path name is one of the important steps to secure your magento store.There is an option to change default admin path during installation.But you can also rename admin path after installation.

To rename admin path,

Goto to /app/etc and open local.xml file.

Find the text ![CDATA[admin]] in the file(around line no.57).This is the default admin path.

Change admin name to some other name like mystore and save it.

So it will look like ![CDATA[mystore]] and admin url will be

http://www.your-domain.com/mystore

Advertisements

Direct SQL Queries in Magento

DATABASE CONNECTIONS IN MAGENTO

 /**
 * Get the resource model
 */
 $resource = Mage::getSingleton('core/resource');

 /**
 * Retrieve the read connection
 */
 $readConnection = $resource->getConnection('core_read');

 /**
 * Retrieve the write connection
 */
 $writeConnection = $resource->getConnection('core_write');

/**
* Read Data From Table
*/

$query= 'SELECT * FROM '. $resource->getTableName('catalog/product');

/**
* Execute the query and store the results in $results
*/

$results = $readConnection->fetchAll($query);

/**
* Print out the results
*/

var_dump($results);

Magento Overriding Block,Controller,Helper,Model and Observer


Model overriding

#Write Below code in config.xml of your Module#
<models>
 <sales>
    <rewrite>
       <order_pdf_abstract>
          Namespace_Sample_Model_Order_Pdf_Abstract
       </order_pdf_abstract>
       <order_pdf_invoice>
          Namespace_Sample_Model_Order_Pdf_Invoice
       </order_pdf_invoice>
       <order_pdf_creditmemo>
          Namespace_Sample_Model_Order_Pdf_Creditmemo
       </order_pdf_Creditmemo>
       <order_pdf_shipment>
          Namespace_Sample_Model_Order_Pdf_Shipment
       </order_pdf_shipment>
   </rewrite>
 </sales>
</models>

Block Overrides

#Write Below code in config.xml of your Module#
<blocks>
   <catalog>
     <rewrite>
       <product_view>
          Namespace_Sample_Block_Catalog_Product_View
       </product_view>
     </rewrite>
   </catalog>
</blocks>

Resource Model Overriding

#Write Below code in config.xml of your Module#
<models>
   <catalog>
     <rewrite>
        <resource_product_indexer_price_default>
              Namespace_Sample_Model_Catalog_Resource_Product_Indexer_Price_Default
        </resource_product_indexer_price_default>
     </rewrite>
   </catalog>
</models>

Observer

#Write Below code in config.xml of your Module#
<events>
      <EVENT_TO_HOOK>
         <observers>
            <module>
              <type>singleton</type>
              <class>namespace_sample_model_observer</class>
              <method>methodToCall</method>
            </module>
          </observers>
        </EVENT_TO_HOOK>
</events>

Observer Example:

<events>
     <sales_order_place_after>
       <observers>
           <feed_sales_order_observer>
               <type>singleton</type>
               <class>feedsales/order_observer</class>
               <method>export_new_order</method>
           </feed_sales_order_observer>
       </observers>
     </sales_order_place_after>
</events>

Controller Overriding
Way 1) #Write Below code in config.xml of your Module#

<global>
    <rewrite>
      <namespace_sample_checkout_controllers_cartcontroller>
         <from>
            <![CDATA[#^/checkout/controllers_cart/#]]>
         </from> <!-- Mage_Checkout_Controllers_CartController -->
         <to>
            /sample/checkout_controllers_cart/
         </to> <!-- Namespace_Sample_Checkout_Controllers_CartController -->
      </namespace_sample_checkout_controllers_cartcontroller>
    </rewrite>
</global>

Way 2) #Write Below code in config.xml of your Module#
 <frontend>
     <routers>
       <sample>
        <args>
          <modules>
           <Namespace_Sample before="Mage_Checkout">
              Namespace_Sample
           </Namespace_Sample>
          </modules>
       </args>
      </sample>
    </routers>
</frontend>

#Write Below line in your controller like given below#
<?php
require_once "Mage/Checkout/controllers/Controllers/CartController.php";
class Namespace_Sample_Checkout_Controllers_CartController extends Mage_Checkout_Controllers_CartController{

}
?>

Helper Overriding

#Write Below code in config.xml of your Module#
Mage_Checkout_Helper_Cart
<helpers>
    <checkout>
      <rewrite>
        <cart>
          Namespace_Sample_Helper_Checkout_Cart
        </cart>
      </rewrite>
    </checkout>
</helpers>

Change ‘My Cart’ to ‘Shopping cart’ in top right nav or in top links

You can change your toplinks as from “My cart” to “Shopping cart” from following URL:
App/code/core/mage/Checkout/Block/Links.php 
See on line number 48 to 52.

and after that overrides the Links.php as follows

<blocks>
<checkout>
        <rewrite>
            <links>My_Module_Block_Checkout_Links</links>
        </rewrite>
</checkout>
</blocks>

and also Change in Links.php

class Mage_Checkout_Block_Links extends Mage_Core_Block_Template 

to 
class My_Module_Block_Checkout_Links extend Mage_Checkout_Block_Links 

What is a Helper ?

Create your own Helper

As you are now used to, it starts by declaring it in the config.xml of your plugin, do that in <global> and after </blocks>


<helpers>
    <test>
        <class>Pfay_Test_Helper</class>
    </test>
</helpers>

What is a Helper

As the name implies a « helper » is something that is right for you ! It is an object that will contain practical functions for you and you can call it from anywhere, you just load your helper to use it. For example:

$helper = Mage::helper(‘monhelper’);

Note that this call is actually equivalent to:

$helper = Mage::helper(‘monhelper/data’);
Indeed, it is the default helper « data » which is called.

Call helper of custom module

$helper = Mage::helper(‘modulename/helpername’);

Create your own Helper

As you are now used to, it starts by declaring it in the config.xml of your plugin, do that in <global> and after </blocks>


<helpers>
    <test>
        <class>Pfay_Test_Helper</class>
    </test>
</helpers>

Then creates the folder and the file app/code/local/Pfay/Test/Helper/data.php function wich will contains the function bytwo($ nbr) with a number as an argument and returns that number multiplied by two. A helper is an object that extends the core class Mage_Core_Helper_Abstract.

class Pfay_Test_Helper_Data extends Mage_Core_Helper_Abstract{
public function bytwo($nbr){
return $nbr*2;
}
}

Now in/app/code/local/Pfay/Test/Block/monblock.php we change the function by:

</div>
class Pfay_Test_Block_Monblock extends Mage_Core_Block_Template
{
public function methodblock()
{
$helper = Mage::helper('test');
return '2*2 = '.$helper->bytwo(2);
}
}

Now when we go on yoursite.com/index.php/test/index/, we see that it works!

Here we are ! you understand how to create a helper on magento, I invite you to practice, the only way to improve yourself ;) If you have any questions, feel free to leave a comment.

You want to help me? Share this article on twitter, do an article on this series of tutorials on your blog, tell your friends, participate in the comments and return to this site;)