Magento disable a module output programmatically

Write the code below to disable module output

For Eg. Mage_Wishlist
 $outputPath = “advanced/modules_disable_output/Mage_Wishlist”;
       Mage::app()->getStore()->setConfig($outputPath, true);

Disable full module

$nodePath = “modules/Mage_Wishlist/active”;

 Mage::getConfig()->setNode($nodePath, 'false', true);

Thanks

Magento change theme programmatically

Hello ,

Write below code in action to set Package and theme for the action

  Mage::getDesign()->setArea(‘frontend’) //Area (frontend|adminhtml)
            ->setPackageName(‘default’) //Name of Package
            ->setTheme(‘modern’); // Name of theme

You may write the code in  layout handler to set theme

<reference name=”root”>
<action method=”setTheme”><theme>modern</theme></action>

</reference>

Change page layout

<reference name=”root”>
            <action method=”setTemplate”><template>page/1column.phtml</template></action>
 </reference>

Cheers

 

 

Magento Event observer

Hello all,

Hope you know basics of magento

Go through the link for list of observer
http://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/

Let me explain it one with example
Eg: “customer_session_init”
File app/code/core/Mage/Customer/Model/Session.php
Line :75

See the code
Mage::dispatchEvent(‘customer_session_init’, array(‘customer_session’=>$this));

Description:: Event is dispatched each time session is initialized.
<frontend>
<events>
<customer_session_init>
<observers>
<magext_cust_init>
<type>singleton</type>
<class>Megext_Remember_Model_Observer</class>
<method>sendCustomerLoggedInnotification</method>
</magext_cust_init>
</observers>
</customer_session_init>
</events>
</frontend>

Create class “Megext_Remember_Model_Observer” in magento way and create one method “sendCustomerLoggedInnotification”
<?php class Megext_Remember_Model_Observer
{

public function sendCustomerLoggedInnotification($observer)
{
$observer->getEvent()->getCustomerSession();

//Do here your code
}

}

Thanks

Magento create custom import script

define(‘MAGENTO’, realpath(dirname(__FILE__)));
require_once MAGENTO . ‘/app/Mage.php’;

$count = 0;
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
echo “loaded csv files..please wait.<br/>”;
$file = fopen(‘./var/import/category/categories.csv’, ‘r’);
while (($data = fgetcsv($file)) !== FALSE) { $count++;

$category = Mage::getModel(‘catalog/category’);
$category->setStoreId(0);
$data = array(); // Make array of data here
$category->addData($data);
try
{
$category->save();
}
catch(Exception $e)
{
throw new Exception(‘Failed to import:’.$e->getMessage());
}

}

echo “Import finished.”;

Magento delete all products manually

Step 1: Truncate all product tables.

TRUNCATE TABLE `catalog_product_bundle_option`;
TRUNCATE TABLE `catalog_product_bundle_option_value`;
TRUNCATE TABLE `catalog_product_bundle_selection`;
TRUNCATE TABLE `catalog_product_entity_datetime`;
TRUNCATE TABLE `catalog_product_entity_decimal`;
TRUNCATE TABLE `catalog_product_entity_gallery`;
TRUNCATE TABLE `catalog_product_entity_int`;
TRUNCATE TABLE `catalog_product_entity_media_gallery`;
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`;
TRUNCATE TABLE `catalog_product_entity_text`;
TRUNCATE TABLE `catalog_product_entity_tier_price`;
TRUNCATE TABLE `catalog_product_entity_varchar`;
TRUNCATE TABLE `catalog_product_link`;
TRUNCATE TABLE `catalog_product_link_attribute`;
TRUNCATE TABLE `catalog_product_link_attribute_decimal`;
TRUNCATE TABLE `catalog_product_link_attribute_int`;
TRUNCATE TABLE `catalog_product_link_attribute_varchar`;
TRUNCATE TABLE `catalog_product_link_type`;
TRUNCATE TABLE `catalog_product_option`;
TRUNCATE TABLE `catalog_product_option_price`;
TRUNCATE TABLE `catalog_product_option_title`;
TRUNCATE TABLE `catalog_product_option_type_price`;
TRUNCATE TABLE `catalog_product_option_type_title`;
TRUNCATE TABLE `catalog_product_option_type_value`;
TRUNCATE TABLE `catalog_product_super_attribute`;
TRUNCATE TABLE `catalog_product_super_attribute_label`;
TRUNCATE TABLE `catalog_product_super_attribute_pricing`;
TRUNCATE TABLE `catalog_product_super_link`;
TRUNCATE TABLE `catalog_product_enabled_index`;
TRUNCATE TABLE `catalog_product_website`;
TRUNCATE TABLE `catalog_product_entity`;

TRUNCATE TABLE `cataloginventory_stock`;
TRUNCATE TABLE `cataloginventory_stock_item`;
TRUNCATE TABLE `cataloginventory_stock_status`;

Step 2: Insert product related attributes like upsell, cross sel, super, bundle, etc

insert  into `catalog_product_link_type`(`link_type_id`,`code`) values (1,’relation’),(2,’bundle’),(3,’super’),(4,’up_sell’),(5,’cross_sell’);
insert  into `catalog_product_link_attribute`(`product_link_attribute_id`,`link_type_id`,`product_link_attribute_code`,`data_type`) values (1,2,’qty’,’decimal’),(2,1,’position’,’int’),(3,4,’position’,’int’),(4,5,’position’,’int’),(6,1,’qty’,’decimal’),(7,3,’position’,’int’),(8,3,’qty’,’decimal’);
insert  into `cataloginventory_stock`(`stock_id`,`stock_name`) values (1,’Default’);

Magento delete all category cleanly

Run the query in database.

TRUNCATE TABLE `catalog_category_entity`;
    TRUNCATE TABLE `catalog_category_entity_datetime`;
    TRUNCATE TABLE `catalog_category_entity_decimal`;
    TRUNCATE TABLE `catalog_category_entity_int`;
    TRUNCATE TABLE `catalog_category_entity_text`;
    TRUNCATE TABLE `catalog_category_entity_varchar`;
    TRUNCATE TABLE `catalog_category_product`;
    TRUNCATE TABLE `catalog_category_product_index`;

    insert  into `catalog_category_entity`(`entity_id`,`entity_type_id`,`attribute_set_id`,`parent_id`,`created_at`,`updated_at`,`path`,`position`,`level`,`children_count`) values (1,3,0,0,'0000-00-00 00:00:00','2013-01-04 00:25:34','1',1,0,1),(2,3,3,0,'2009-02-20 00:25:34','2009-02-20 00:25:34','1/2',1,1,0);
    insert  into `catalog_category_entity_int`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) values (1,3,32,0,2,1),(2,3,32,1,2,1);
    insert  into `catalog_category_entity_varchar`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) values (1,3,31,0,1,'Root Catalog'),(2,3,33,0,1,'root-catalog'),(3,3,31,0,2,'Default Category'),(4,3,39,0,2,'PRODUCTS'),(5,3,33,0,2,'default-category');

N.B: Please take backup of database before running above query

Magento get URL

Get Url in phtml files

1. Get Base Url :

Mage::getBaseUrl();

2. Get Skin Url :

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);

(a) Unsecure Skin Url :

$this->getSkinUrl('images/imagename.jpg');

(b) Secure Skin Url :

$this->getSkinUrl('images/imagename.gif', array('_secure'=>true));

3. Get Media Url :

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);

4. Get Js Url :

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);

5. Get Store Url :

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

6. Get Current Url

Mage::helper('core/url')->getCurrentUrl();

Get Url in cms pages or static blocks

1. Get Base Url :

{{store url=""}}

2. Get Skin Url :

{{skin url='images/imagename.jpg'}}

3. Get Media Url :

{{media url='/imagename.jpg'}}

4. Get Store Url :

{{store url='mypage.html'}}

Magento Varien Profiler

Go to admin>System>configuration>Developer>Debug Set Profiler to “Yes”
Write the code below
Varien_Profiler::enable(); // Enable profiler
Varien_Profiler::start(‘your-key’); //start the profiler with a special key

//put some php code here

Varien_Profiler::stop(‘your-key’);//next, stop your profiler with the same key
Varien_Profiler::disable();//Last, disable your profiler

Go to site you will see your key under Code Profiler column and other information in same row.

Thanks

Magento create custom advance profile

Profile Name:Enter Profile Name

Action XML

<action type=”dataflow/convert_adapter_io” method=”load”>
<var name=”type”>file</var>
<var name=”path”>var/import</var>
<var name=”filename”><![CDATA[category.csv]]></var>
<var name=”format”><![CDATA[csv]]></var>
</action>
<action type=”dataflow/convert_parser_csv” method=”parse”>
<var name=”delimiter”><![CDATA[,]]></var>
<var name=”enclose”><![CDATA[“]]></var>
<var name=”fieldnames”>true</var>
<var name=”store”><![CDATA[0]]></var>
<var name=”number_of_records”>1</var>
<var name=”decimal_separator”><![CDATA[.]]></var>
<var name=”adapter”>catalog/convert_adapter_category</var>
<var name=”method”>parse</var>
</action>

N.B.

<var name=”adapter”>catalog/convert_adapter_category</var> //Here is the name of model

<var name=”filename”><![CDATA[category.csv]]></var> //your file name here

Create class in proper directory.

<?php

class Mage_Catalog_Model_Convert_Adapter_Category extends Mage_Dataflow_Model_Convert_Adapter_Abstract
{
public function load()
{

}

public function save()
{
// you have to create this method, enforced by Mage_Dataflow_Model_Convert_Adapter_Interface
}

public function parse()
{
$batchModel = Mage::getSingleton(‘dataflow/batch’);
/* @var $batchModel Mage_Dataflow_Model_Batch */

$batchImportModel = $batchModel->getBatchImportModel();
$importIds = $batchImportModel->getIdCollection();

foreach ($importIds as $importId) {
$batchImportModel->load($importId);
$importData = $batchImportModel->getBatchData();

$this->saveRow($importData);
}
}

public function saveRow(array $importData)
{
if($importData[‘sku’])
{
$_product = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’,$importData[‘sku’]);
if($_product)
{
$_product->setCategoryIds($importData[‘category_ids’]);
$_product->save();
}
}
}
}