Magento clear all test data from database

***********for categories********************
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′,’2009-02-20 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’);

*****************for customers*****************

SET FOREIGN_KEY_CHECKS=0;
— reset customers
TRUNCATE customer_address_entity;
TRUNCATE customer_address_entity_datetime;
TRUNCATE customer_address_entity_decimal;
TRUNCATE customer_address_entity_int;
TRUNCATE customer_address_entity_text;
TRUNCATE customer_address_entity_varchar;
TRUNCATE customer_entity;
TRUNCATE customer_entity_datetime;
TRUNCATE customer_entity_decimal;
TRUNCATE customer_entity_int;
TRUNCATE customer_entity_text;
TRUNCATE customer_entity_varchar;
TRUNCATE log_customer;
TRUNCATE log_visitor;
TRUNCATE log_visitor_info;

ALTER TABLE customer_address_entity AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_datetime AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_decimal AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_int AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_text AUTO_INCREMENT=1;
ALTER TABLE customer_address_entity_varchar AUTO_INCREMENT=1;
ALTER TABLE customer_entity AUTO_INCREMENT=1;
ALTER TABLE customer_entity_datetime AUTO_INCREMENT=1;
ALTER TABLE customer_entity_decimal AUTO_INCREMENT=1;
ALTER TABLE customer_entity_int AUTO_INCREMENT=1;
ALTER TABLE customer_entity_text AUTO_INCREMENT=1;
ALTER TABLE customer_entity_varchar AUTO_INCREMENT=1;
ALTER TABLE log_customer AUTO_INCREMENT=1;
ALTER TABLE log_visitor AUTO_INCREMENT=1;
ALTER TABLE log_visitor_info AUTO_INCREMENT=1;
SET FOREIGN_KEY_CHECKS=1;

SET FOREIGN_KEY_CHECKS=0;
TRUNCATE `sales_flat_creditmemo`;
TRUNCATE `sales_flat_creditmemo_comment`;
TRUNCATE `sales_flat_creditmemo_grid`;
TRUNCATE `sales_flat_creditmemo_item`;
TRUNCATE `sales_flat_invoice`;
TRUNCATE `sales_flat_invoice_comment`;
TRUNCATE `sales_flat_invoice_grid`;
TRUNCATE `sales_flat_invoice_item`;
TRUNCATE `sales_flat_order`;
TRUNCATE `sales_flat_order_address`;
TRUNCATE `sales_flat_order_grid`;
TRUNCATE `sales_flat_order_item`;
TRUNCATE `sales_flat_order_payment`;
TRUNCATE `sales_flat_order_status_history`;
TRUNCATE `sales_flat_quote`;
TRUNCATE `sales_flat_quote_address`;
TRUNCATE `sales_flat_quote_address_item`;
TRUNCATE `sales_flat_quote_item`;
TRUNCATE `sales_flat_quote_item_option`;
TRUNCATE `sales_flat_quote_payment`;
TRUNCATE `sales_flat_quote_shipping_rate`;
TRUNCATE `sales_flat_shipment`;
TRUNCATE `sales_flat_shipment_comment`;
TRUNCATE `sales_flat_shipment_grid`;
TRUNCATE `sales_flat_shipment_item`;
TRUNCATE `sales_flat_shipment_track`;
TRUNCATE `sales_invoiced_aggregated`;
TRUNCATE `sales_invoiced_aggregated_order`;
TRUNCATE `sales_payment_transaction`;
TRUNCATE `sales_order_aggregated_created`;
TRUNCATE `sales_order_tax`;
TRUNCATE `sales_order_tax_item`;
TRUNCATE `sendfriend_log`;
TRUNCATE `tag`;
TRUNCATE `tag_relation`;
TRUNCATE `tag_summary`;
TRUNCATE `wishlist`;
TRUNCATE `log_quote`;
TRUNCATE `report_event`;
ALTER TABLE `sales_flat_creditmemo` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_shipping_rate` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_track` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoiced_aggregated` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoiced_aggregated_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_payment_transaction` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_aggregated_created` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_tax` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_tax_item` AUTO_INCREMENT=1;
ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
ALTER TABLE `tag` AUTO_INCREMENT=1;
ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
ALTER TABLE `wishlist` AUTO_INCREMENT=1;
ALTER TABLE `log_quote` AUTO_INCREMENT=1;
ALTER TABLE `report_event` AUTO_INCREMENT=1;
SET FOREIGN_KEY_CHECKS=1;

Send custom email into Mail Chimp List Programmatically in magento

If you have custom collection and you wish to send it into mailchimp , Below if code that help you.

       $defaultList = Mage::getStoreConfig(Ebizmarts_MageMonkey_Model_Config::GENERAL_LIST, 1);
       $mergeVars = array(
                            'EMAIL' => 'santoshmeets@gmail.com',
                            'FNAME' => 'Santosh',
                            'LNAME' => 'Yadav',
                            'CGROUP' => 'General'
        );
         Mage::getSingleton('monkey/api')->listSubscribe($listId, $email, $mergeVars, 'html', false, TRUE);

Don’t forget to write feedback on this trick. Thanks in adavance.

Magento add custom block to product page

Depend upon the position where we would like to insert your custom block you can insert your custom block.

Below is xml to insert custom block into product page

<catalog_product_view>
            <reference name="product.info.extrahint">
                <block type="tag/product_list" name="product_tag_list" before="-" template="tag/list.phtml"/>
            </reference>


            <reference name="product.info">
                <block type="catalog/product_list_related" before="-"  name="catalog.product.related" as="related_products" template="catalog/product/list/related.phtml" >
                        <action method="addToParentGroup"><group>detailed_info</group></action>
                </block>
                    </reference>
    </catalog_product_view>

Magento add attachment to email

Hello All,
I have found working code to send email with attachment.

$attachmentFilePath = Mage::getBaseDir('media'). DS.$filpath; //filepath is path of file inside media directory
$mailTemplate = Mage::getModel('core/email_template');
if(file_exists($attachmentFilePath)){ 
                    $fileContents = file_get_contents($attachmentFilePath);
                    $attachment   = $mailTemplate->getMail()->createAttachment($fileContents,Zend_Mime::TYPE_OCTETSTREAM,Zend_Mime::DISPOSITION_ATTACHMENT,Zend_Mime::ENCODING_BASE64,basename($attachmentFilePath));
              
                }

$postObject = new Varien_Object();
//Below code require you understandtsthe term used
$mailTemplate->setDesignConfig(array('area' => 'frontend'))
                    ->setReplyTo($post['email'])
                    ->sendTransactional(
                        $template_id,  //template id of email template
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                        null,
                        array('data' => $postObject)
    );

Magento add translation file for custom module

In order to add custom translation we need to put translation file in location app\locale\en_US\Filename.csv

en_US is translation directory for english

For Admin
——————-
<adminhtml>

<translate>
<modules>
<modulename>
<files>
<default>Namespace_Module.csv</default>
</files>
</modulename>
</modules>
</translate>
</adminhtml>

For Frontend
<frontend>

<translate>
<modules>
<modulename>
<files>
<default>Namespace_Module.csv</default>
</files>
</modulename>
</modules>
</translate>
</frontend>

NB: You need to place this in config.xml

Thanks

Magento add new attribute to customer with source model

Place this script in setup and replace “attribute_code_here” with your attribute code

<?php
$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup(‘core_setup’);

$entityTypeId     = $setup->getEntityTypeId(‘customer’);
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$setup->addAttribute(‘customer’, ‘attribute_code_here’, array(
‘input’         => ‘select’,
‘type’          => ‘int’,
‘source’          =>’eav/entity_attribute_source_boolean’,
‘label’         => ‘attribute label here’,
‘visible’       => 1,
‘required’      => 0,
‘user_defined’ => 1,
));

$setup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
‘attribute_code_here’,
‘999’  //sort_order
);

$oAttribute = Mage::getSingleton(‘eav/config’)->getAttribute(‘customer’, ‘attribute_code_here’);
$oAttribute->setData(‘used_in_forms’, array(‘adminhtml_customer’));
$oAttribute->save();

$setup->endSetup();

Cheers . Thanks

Magento create admin user programmatically

Hello all,

Here is custom script to create admin user programmatically.

<?php
# Create New admin User programmatically.
require_once(‘./app/Mage.php’);
umask(0);
Mage::app();

try {
$user = Mage::getModel(‘admin/user’)
->setData(array(
‘username’  => ‘admin1’,
‘firstname’ => ‘Admin’,
‘lastname’    => ‘Admin’,
’email’     => ‘santosh@test.com’,
‘password’  =>’admin123′,
‘is_active’ => 1
))->save();

} catch (Exception $e) {
echo $e->getMessage();
exit;
}

//Assign Role Id
try {
$user->setRoleIds(array(1))  //Administrator role id is 1 ,Here you can assign other roles ids
->setRoleUserId($user->getUserId())
->saveRelations();

} catch (Exception $e) {
echo $e->getMessage();
exit;
}

echo “User created successfully”;

?>

Put the above content in a magento root folder in a file and  then browse the file.

You are done with creating new admin user.

Chears

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