How to create shipping method in Magento1

How to create shipping method in magento?

In this extension we created shipping according to “number of quantity product” or according to “number of order”.

Note:- you can download extension from this rar_image
Add shipping method in magento through custom module.
Note:- Firstly disable the cache from Cache Management tab and which is in System tab.
there are many steps to create this extension.
Step1:- Create Mywork_MyShipping.xml file in app/etc/modules/.
where Mywork is Namespace and MyShipping is Modulename.
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_MyShipping>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Shipping />
</depends>
</Mywork_MyShipping>
</modules>
</config>
[/php]
Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/MyShipping/etc/
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_MyShipping>
<version>1.0.0</version>
</Mywork_MyShipping>
</modules>

<global>
<helpers>
<myshipping>
<class>Mywork_MyShipping_Helper</class>
</myshipping>
</helpers>

<models>
<mywork_myshipping>
<class>Mywork_MyShipping_Model</class>
</mywork_myshipping>
</models>
</global>

<default>
<carriers>
<mywork_myshipping>
<active>1</active>
<sallowspecific>1</sallowspecific>
<model>mywork_myshipping/myshipping</model>
<name>Total Fixed Rs.</name>
<price>30.00</price>
<title>My Shipping</title>
<type>I</type>
</mywork_myshipping>
</carriers>
</default>
</config>
[/php]

Step3:- Now create the system.xml file in app/code/local/Mywork/MyShipping/etc/
[php]
<?xml version="1.0" encoding="UTF-8"?>
<config>
<sections>
<carriers>
<groups>
<mywork_myshipping translate="label">
<label>My Shipping Method</label>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<active translate="label">
<label>Enabled</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</active>
<title translate="label">
<label>Title</label>
<frontend_type>text</frontend_type>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</title>

<price translate="label">
<label>Price</label>
<frontend_type>text</frontend_type>
<validate>validate-number validate-zero-or-greater</validate>
<sort_order>5</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</price>

<type translate="label">
<label>Type</label>
<frontend_type>select</frontend_type>
<source_model>Mywork_MyShipping_Model_Myshippingoption</source_model>
<sort_order>4</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</type>

<sallowspecific translate="label">
<label>Ship to Applicable Countries</label>
<frontend_type>select</frontend_type>
<frontend_class>shipping-applicable-country</frontend_class>
<source_model>adminhtml/system_config_source_shipping_allspecificcountries</source_model>
<sort_order>30</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</sallowspecific>

<specificcountry translate="label">
<label>Ship to Specific Countries</label>
<frontend_type>multiselect</frontend_type>
<sort_order>31</sort_order>
<source_model>adminhtml/system_config_source_country</source_model>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<can_be_empty>1</can_be_empty>
</specificcountry>

</fields>
</mywork_myshipping>
</groups>
</carriers>
</sections>
</config>
[/php]

Step4:- Now create the Myshipping.php file in app/code/local/Mywork/MyShipping/Model/
[php]
<?php
class Mywork_MyShipping_Model_Myshipping extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{
protected $_code = ‘mywork_myshipping’;
protected $_isFixed = true;

/**
* Enter description here…
*
* @param Mage_Shipping_Model_Rate_Request $data
* @return Mage_Shipping_Model_Rate_Result
*/
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
{
if (!$this->getConfigFlag(‘active’)) {
return false;
}

$freeBoxes = 0;
if ($request->getAllItems()) {
foreach ($request->getAllItems() as $item) {

if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
continue;
}

if ($item->getHasChildren() && $item->isShipSeparately()) {
foreach ($item->getChildren() as $child) {
if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
$freeBoxes += $item->getQty() * $child->getQty();
}
}
} elseif ($item->getFreeShipping()) {
$freeBoxes += $item->getQty();
}
}
}
$this->setFreeBoxes($freeBoxes);

$result = Mage::getModel(‘shipping/rate_result’);
if ($this->getConfigData(‘type’) == ‘O’) { // per order
$shippingPrice = $this->getConfigData(‘price’);
} elseif ($this->getConfigData(‘type’) == ‘I’) { // per item
$shippingPrice = ($request->getPackageQty() * $this->getConfigData(‘price’)) – ($this->getFreeBoxes() * $this->getConfigData(‘price’));
} else {
$shippingPrice = false;
}

$shippingPrice = $this->getFinalPriceWithHandlingFee($shippingPrice);

if ($shippingPrice !== false) {
$method = Mage::getModel(‘shipping/rate_result_method’);

$method->setCarrier(‘mywork_myshipping’);
$method->setCarrierTitle($this->getConfigData(‘title’));

$method->setMethod(‘mywork_myshipping’);
$method->setMethodTitle($this->getConfigData(‘name’));

if ($request->getFreeShipping() === true || $request->getPackageQty() == $this->getFreeBoxes()) {
$shippingPrice = ‘0.00’;
}

$method->setPrice($shippingPrice);
$method->setCost($shippingPrice);

$result->append($method);
}

return $result;
}

public function getAllowedMethods()
{
return array(‘mywork_myshipping’=>$this->getConfigData(‘name’));
}

}
[/php]

Step5:- Now create the Myshippingoption.php file in app/code/local/Mywork/MyShipping/Model/
[php]
<?php
class Mywork_MyShipping_Model_Myshippingoption
{
public function toOptionArray()
{
return array(
array(‘value’=>”, ‘label’=> Mage::helper(‘adminhtml’)->__(‘None’)),
array(‘value’=>’O’, ‘label’=>Mage::helper(‘adminhtml’)->__(‘Per Order’)),
array(‘value’=>’I’, ‘label’=>Mage::helper(‘adminhtml’)->__(‘Per Item’)),
);
}
}
[/php]

Note:- On backend you can change shipping price according to you. click on admin section system->configuration->Sales->Shipping Methods.
shipping_method_backend

Note:- On frontend you can choose shipping method.
shipping_method_frontend

Get products from the cart in magento1

[php]
$items = Mage::getSingleton(‘checkout/session’)->getQuote()->getAllVisibleItems();
foreach($items as $item) {

$item_id=$item->getId();
$pro_id= $item->getProductId();
$_product= Mage::getModel(‘catalog/product’)->load($pro_id);
$pro_name = $_product->getName();
$pro_qty = $item->getQty();
$productId = $_product->getId();
$pro_image = (string)Mage::helper(‘catalog/image’)->init($_product, ‘image’)->resize(60);
$productUrl = $_product->getProductUrl();
$delete_item_url = Mage::getUrl(‘checkout/cart/delete’, array(‘id’ => $item_id));
}
[/php]

Delete product from the cart in magento1

[php]
$pro_id = $your_product_id;
$product = Mage::getModel(‘catalog/product’)->load($pro_id);
if ($product->getData(‘type_id’) == "configurable"):

$attribute_id = $your_attribute_id;
$attribute_valid = $your_attribute_valid;

try{

$cartHelper = Mage::helper(‘checkout/cart’);
$items = $cartHelper->getCart()->getItems();

foreach($items as $item):
if($item->getProduct()->getId() == $pro_id):

$att_id_arr=array();
$att_vid_arr=array();

$attribute_data= $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
$att_kv=$attribute_data[‘info_buyRequest’][‘super_attribute’];
foreach($att_kv as $att_id=>$att_vid){
$att_id_arr[]= $att_id;
$att_vid_arr[]= $att_vid;
}

if(in_array($attribute_id, $att_id_arr) && in_array($attribute_valid, $att_vid_arr)):

$itemQuantity = $item->getQty();

if($itemQuantity == 1):

$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId)->save();

else:

$item->setQty($itemQuantity-1);
$cartHelper->getCart()->save();

endif;

break;
endif;
endif;
endforeach;

}catch (Exception $e) {

}

else:

try{

$cartHelper = Mage::helper(‘checkout/cart’);
$items = $cartHelper->getCart()->getItems();

foreach($items as $item):
if($item->getProduct()->getId() == $pro_id):
$itemQuantity = $item->getQty();

if($itemQuantity == 1):

$itemId = $item->getItemId();
$cartHelper->getCart()->removeItem($itemId)->save();

else:

$item->setQty($itemQuantity-1);
$cartHelper->getCart()->save();

endif;

break;
endif;
endforeach;

} catch (Exception $e) {

}

endif;
[/php]

Add product into cart in magento1

Add simple product into cart into magento?

[php]
$product = Mage::getModel(‘catalog/product’)->load($pro_id);
$session = Mage::getSingleton(‘core/session’, array(‘name’=>’frontend’));
$cart = Mage::helper(‘checkout/cart’)->getCart();
$cart->addProduct($product, $pro_quantity);
$session->setLastAddedProductId($product->getId());
$session->setCartWasUpdated(true);
$cart->save();
[/php]

Note:- (1) $pro_id is a product id
(2) $pro_quantity is a quantity of the product which will be add into cart.

Add configurable product into cart into magento?

[php]
$product = Mage::getModel(‘catalog/product’)->load($pro_id);
$params = array(‘product’ => $pro_id,’super_attribute’ => array($attribute_id=>$attribute_valid),’qty’ => $pro_quantity);
$session = Mage::getSingleton(‘core/session’, array(‘name’=>’frontend’));
$cart = Mage::helper(‘checkout/cart’)->getCart();
$cart->addProduct($product, $params);
$session->setLastAddedProductId($product->getId());
$session->setCartWasUpdated(true);
$cart->save();
[/php]

Note:- (1) $pro_id is a product id
(2) $pro_quantity is a quantity of the product that will be added into the cart.
(3) $attribute_id is an attribute id.
(4) $attribute_valid is a value id of the attribute.

Create Category through code in Magento1

To create the category firstly create the category Model object through below code.
[php]
$category = Mage::getModel(‘catalog/category’);
[/php]
after that define the category name, Url key, etc.

[php]
$parentId = ‘2’;
$category = Mage::getModel(‘catalog/category’);
$category->setName(‘category_name’);
$category->setUrlKey(‘category_key’);
$category->setIsActive(1);
$category->setDisplayMode(‘PRODUCTS’);
$category->setIsAnchor(1); //for active anchor
$category->setStoreId(Mage::app()->getStore()->getId());
$parentCategory = Mage::getModel(‘catalog/category’)->load($parentId);
$category->setPath($parentCategory->getPath());
$category->save();
[/php]

Note:- parentId is a category id, where we create a child category of this category.

Get Category details in Magento1

Get all categories in magento1

[php]
$categories = Mage::getModel(‘catalog/category’)->getCollection()
->addAttributeToSelect(‘id’)
->addAttributeToSelect(‘name’)
->addAttributeToSelect(‘url’)
->addAttributeToSelect(‘is_active’);

foreach ($categories as $category)
{
if ($category->getIsActive()) { // Only pull Active categories
$entity_id = $category->getId();
$name = $category->getName();
$url_key = $category->getUrlKey();
$url_path = $category->getUrl();
}
}
[/php]

Get the child category of the category in magento1

[php]
$children = Mage::getModel(‘catalog/category’)->getCategories(category_id);
foreach ($children as $category) {
echo $category->getName();
echo $category->getImageUrl();
echo $category->getDescription();
echo $category->getId();
}

[/php]

Note:- category_id like 1,2 etc.

Get all categories of the product in magento1

[php]
$_Product = Mage::getModel("catalog/product")->load(Product_id);
$categoryIds = $_Product->getCategoryIds();//array of product categories

foreach($categoryIds as $categoryId) {
$category = Mage::getModel(‘catalog/category’)->load($categoryId);
echo $category->getName();
echo $category->getImageUrl();
echo $category->getDescription();
echo $category->getId();
}
[/php]
Note:- put product id in the load function.

Get the level of the category in magento1

[php]
$category = Mage::getModel(‘catalog/category’)->load(category_id);
echo $category->getLevel();
[/php]

Note:- put the category id in the load function.

Get the category name, image, description from category id in magento1

[php]
$category = Mage::getModel(‘catalog/category’)->load(category_id);
echo $category->getName();
echo $category->getImageUrl();
echo $category->getDescription();
[/php]

Note:- put the categoryid in the load function.

Add custom category attribute in magento1

How to add custom category attribute in magento?

We have created two category attribute (i) Category Small Details1 (ii) Category Small Details2
(i) Category Small Details1 value must be required.
(i) Category Small Details2 value is not required.

Note:- you can download extension from this rar_image
Add custom category attribute in magento through custom module.
Note:- Firstly disable the cache from Cache Management tab which is in System tab.
there are many steps to create the extension.
Step1:- Create Mywork_AddCustomCategoryAttribute.xml file in app/etc/modules/.
where Mywork is Namespace and AddCustomCategoryAttribute is Modulename.


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

Note:- 1) If you want to activate the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create our own extension then we use the local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/AddCustomCategoryAttribute/etc/


<?xml version="1.0"?>
<config>
<modules>
<Mywork_AddCustomCategoryAttribute>
<version>1.0.0</version>
</Mywork_AddCustomCategoryAttribute>
</modules>

<global>
<helpers>
<AddCustomCategoryAttribute>
<class>Mywork_AddCustomCategoryAttribute_Helper</class>
</AddCustomCategoryAttribute>
</helpers>
<models>
<AddCustomCategoryAttribute>
<class>Mywork_AddCustomCategoryAttribute_Model</class>
<resourceModel>AddCustomCategoryAttribute_mysql4</resourceModel>
</AddCustomCategoryAttribute>
</models>
<resources>
<AddCustomCategoryAttribute_setup>
<setup>
<module>Mywork_AddCustomCategoryAttribute</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</AddCustomCategoryAttribute_setup>
<AddCustomCategoryAttribute_write>
<connection>
<use>core_write</use>
</connection>
</AddCustomCategoryAttribute_write>
<AddCustomCategoryAttribute_read>
<connection>
<use>core_read</use>
</connection>
</AddCustomCategoryAttribute_read>
</resources>
</global>
</config>

Step3:- Now create the mysql4-install-1.0.0.php file in app/code/local/Mywork/AddCustomCategoryAttribute/sql/AddCustomCategoryAttribute_setup/


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

$installer->addAttribute(“catalog_category”, “catsmalldetails1”, array(
“type” => “varchar”,
“backend” => “”,
“frontend” => “”,
“label” => “Category Small Details1”,
“input” => “text”,
“class” => “”,
“source” => “”,
“global” => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
“visible” => true,
“required” => true,
“user_defined” => false,
“default” => “”,
“searchable” => false,
“filterable” => false,
“comparable” => false,

“visible_on_front” => false,
“unique” => false,
“note” => “”

));

$installer->addAttribute(“catalog_category”, “catsmalldetails2”, array(
“type” => “varchar”,
“backend” => “”,
“frontend” => “”,
“label” => “Category Small Details2”,
“input” => “text”,
“class” => “”,
“source” => “”,
“global” => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
“visible” => true,
“required” => false,
“user_defined” => false,
“default” => “”,
“searchable” => false,
“filterable” => false,
“comparable” => false,

“visible_on_front” => false,
“unique” => false,
“note” => “”

));

$installer->endSetup();

Step4:- Now create the Data.php file in app/code/local/Mywork/AddCustomCategoryAttribute/Helper/


<?php
class Mywork_AddCustomCategoryAttribute_Helper_Data extends Mage_Core_Helper_Abstract
{
}

After extension uploaded then you can see your custom category attribute on click the category.
AddCustomCategoryAttribute

Add new customer address attribute on customer grid in magento1

How to add custom customer address attribute on customer grid in magento?

In this extension we add two customer address attribute (which is already created through AddCustomCustomerAddressAttribute.rar extension) like a (i) “Reference Address1”
(ii) “Reference Address2”
Note:- you can download extension from this rar_image
Add custom customer address attribute in the customer grid in Magento through the custom module.
Note:- Firstly disable the cache from the Cache Management tab and which is in System tab.
there are many steps to create the extension.

Step1:- Create Mywork_AddCustomCustomerAddressAttributeOnCustomerGrid.xml file in app/etc/modules/.
where Mywork is Namespace and AddCustomCustomerAddressAttributeOnCustomerGrid is Modulename.
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_AddCustomCustomerAddressAttributeOnCustomerGrid>
<active>true</active>
<codePool>local</codePool>
</Mywork_AddCustomCustomerAddressAttributeOnCustomerGrid>
</modules>
</config>
[/php]

Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/AddCustomCustomerAddressAttributeOnCustomerGrid/etc/
[php]
<config>
<modules>
<Mywork_AddCustomCustomerAddressAttributeOnCustomerGrid>
<version>1.0.0</version>
</Mywork_AddCustomCustomerAddressAttributeOnCustomerGrid>
</modules>

<global>
<blocks>
<adminhtml>
<rewrite>
<customer_grid>Mywork_AddCustomCustomerAddressAttributeOnCustomerGrid_Block_Adminhtml_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
<helpers>
<AddCustomCustomerAddressAttributeOnCustomerGrid>
<class>Mywork_AddCustomCustomerAddressAttributeOnCustomerGrid_Helper</class>
</AddCustomCustomerAddressAttributeOnCustomerGrid>
</helpers>
</global>
</config>
[/php]

Step3:- Now create the Grid.php file in app/code/local/Mywork/AddCustomCustomerAddressAttributeOnCustomerGrid/Block/Adminhtml/Customer/
[php]
<?php
class Mywork_AddCustomCustomerAddressAttributeOnCustomerGrid_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{

public function setCollection($collection) {
$collection->joinAttribute(‘reference_address1’, ‘customer_address/reference_address1’, ‘entity_id’, null, ‘left’);
$collection->joinAttribute(‘reference_address2’, ‘customer_address/reference_address2’, ‘entity_id’, null, ‘left’);
$this->_collection = $collection;
}

protected function _prepareColumns()
{
$this->addColumnAfter(‘reference_address1’, array(
‘header’ => Mage::helper(‘customer’)->__(‘Reference Address1’),
‘width’ => ‘150’,
‘index’ => ‘reference_address1′
),’email’);

$this->addColumnAfter(‘reference_address2’, array(
‘header’ => Mage::helper(‘customer’)->__(‘Reference Address2’),
‘width’ => ‘150’,
‘index’ => ‘reference_address2′
),’reference_address1’);

return parent::_prepareColumns();
}

}
?>
[/php]

Note:- In setCollection function we use joinAttribute function instead of addAttributeToSelect function.
joinAttribute function coming from app/code/core/Mage/Eav/Model/Entity/Collectionn/Abstract.php
public function joinAttribute($alias, $attribute, $bind, $filter=null, $joinType=’inner’, $storeId=null)

Step4:- Now create the Data.php file in app/code/local/Mywork/AddCustomCustomerAddressAttributeOnCustomerGrid/Helper/
[php]
<?php
class Mywork_AddCustomCustomerAddressAttributeOnCustomerGrid_Helper_Data extends Mage_Core_Helper_Abstract
{
}
?>
[/php]

Add new customer address attribute in magento1

In this extension we add two customer address attribute like a (i) “Reference Address1” which is required.
(ii) “Reference Address2” which is not required.
Note:- you can download extension from this rar_image
Note:- Firstly disable the cache from Cache Management tab and which is in System tab.
there are many steps to create the extension.

Step1:- Create Mywork_AddCustomCustomerAddressAttribute.xml file in app/etc/modules/.
where Mywork is Namespace and AddCustomCustomerAddressAttribute is Modulename.
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_AddCustomCustomerAddressAttribute>
<active>true</active>
<codePool>local</codePool>
</Mywork_AddCustomCustomerAddressAttribute>
</modules>
</config>
[/php]

Note:- 1) If you want to activate the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create our own extension then we use the local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/AddCustomCustomerAddressAttribute/etc/
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_AddCustomCustomerAddressAttribute>
<version>1.0.0</version>
</Mywork_AddCustomCustomerAddressAttribute>
</modules>
<global>
<helpers>
<AddCustomCustomerAddressAttribute>
<class>Mywork_AddCustomCustomerAddressAttribute_Helper</class>
</AddCustomCustomerAddressAttribute>
</helpers>
<models>
<AddCustomCustomerAddressAttribute>
<class>Mywork_AddCustomCustomerAddressAttribute_Model</class>
<resourceModel>AddCustomCustomerAddressAttribute_mysql4</resourceModel>
</AddCustomCustomerAddressAttribute>
</models>
<resources>
<AddCustomCustomerAddressAttribute_setup>
<setup>
<module>Mywork_AddCustomCustomerAddressAttribute</module>
<class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</AddCustomCustomerAddressAttribute_setup>
<AddCustomCustomerAddressAttribute_write>
<connection>
<use>core_write</use>
</connection>
</AddCustomCustomerAddressAttribute_write>
<AddCustomCustomerAddressAttribute_read>
<connection>
<use>core_read</use>
</connection>
</AddCustomCustomerAddressAttribute_read>
</resources>
</global>
</config>
[/php]

Step3:- Now create the mysql4-install-1.0.0.php file in app/code/local/Mywork/AddCustomCustomerAddressAttribute/sql/AddCustomCustomerAddressAttribute_setup/
[php]
<?php
$installer = $this;
$installer->startSetup();

$installer->addAttribute("customer_address", "reference_address1", array(
"type" => "varchar",
"backend" => "",
"label" => "Reference Address1",
"input" => "text",
"source" => "",
"visible" => true,
"required" => true,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => "Near Reference Address1"

));

$attribute = Mage::getSingleton("eav/config")->getAttribute("customer_address", "reference_address1");
$used_in_forms=array();

$used_in_forms[]="adminhtml_customer_address";
$used_in_forms[]="customer_register_address";
$used_in_forms[]="customer_address_edit";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100)
;
$attribute->save();

$installer->addAttribute("customer_address", "reference_address2", array(
"type" => "varchar",
"backend" => "",
"label" => "Reference Address2",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => "Near Reference Address2"
));

$attribute = Mage::getSingleton("eav/config")->getAttribute("customer_address", "reference_address2");
$used_in_forms=array();

$used_in_forms[]="adminhtml_customer_address";
$used_in_forms[]="customer_register_address";
$used_in_forms[]="customer_address_edit";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 101)
;
$attribute->save();
$installer->endSetup();
[/php]

Step4:- Now create the Data.php file in app/code/local/Mywork/AddCustomCustomerAddressAttribute/Helper/
[php]
<?php
class Mywork_AddCustomCustomerAddressAttribute_Helper_Data extends Mage_Core_Helper_Abstract
{
}
[/php]

Now we are going to design steps

(1) edit.phtml which is in the app/design/frontend/rwd/default/template/customer/address/
[php]
<!——————————————-Reference Address1———————————————>
<li class="fields">
<div class="field">
<label for="reference_address1" class="required"><em>*</em><?php echo $this->__(‘Reference Address1’) ?></label>
<div class="input-box">
<input type="text" name="reference_address1" value="<?php echo $this->escapeHtml($this->getAddress()->getReferenceAddress1()) ?>" title="<?php echo $this->__(‘Reference Address1’) ?>" id="reference_address1" class="input-text required-entry" />
</div>
</div>
</li>

<!———————Reference Address2——————————————->

<li class="fields">
<div class="field">
<label for="reference_address2" class=""><em>*</em><?php echo $this->__(‘Reference Address2’) ?></label>
<div class="input-box">
<input type="text" name="reference_address2" value="<?php echo $this->escapeHtml($this->getAddress()->getReferenceAddress2()) ?>" title="<?php echo $this->__(‘Reference Address2’) ?>" id="reference_address2" class="input-text" />
</div>
</div>
</li>

<!——————————————-End Reference Address————————->
[/php]

Note:- Add this code in edit.phtml file

Add new attribute in customer grid in Magento1

How to add custom attribute in customer grid in magento?

In this extension we add three customer attribute (which is created already through AddCustomCustomerAttribute.rar extension) in the grid like a (i) “Pan Number”
(ii) “Father Name”
(iii) “Mother Name”
Note:- you can download extension from this rar_image
Add custom customer attribute in customer grid in magento through custom module.
Note:- Firstly disable the cache from Cache Management tab and which is in System tab.
there are many steps to create the extension.

Step1:- Create Mywork_AddCustomAttributeOnCustomerGrid.xml file in app/etc/modules/.
where Mywork is Namespace and AddCustomAttributeOnCustomerGrid is Modulename.
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_AddCustomAttributeOnCustomerGrid>
<active>true</active>
<codePool>local</codePool>
</Mywork_AddCustomAttributeOnCustomerGrid>
</modules>
</config>
[/php]

Note:- 1) If you want to activate the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create our own extension then we use the local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/AddCustomAttributeOnCustomerGrid/etc/
[php]
<config>
<modules>
<Mywork_AddCustomAttributeOnCustomerGrid>
<version>1.0.0</version>
</Mywork_AddCustomAttributeOnCustomerGrid>
</modules>

<global>
<blocks>
<adminhtml>
<rewrite>
<customer_grid>Mywork_AddCustomAttributeOnCustomerGrid_Block_Adminhtml_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
<helpers>
<AddCustomAttributeOnCustomerGrid>
<class>Mywork_AddCustomAttributeOnCustomerGrid_Helper</class>
</AddCustomAttributeOnCustomerGrid>
</helpers>
</global>
</config>
[/php]

Step3:- Now create the Grid.php file in app/code/local/Mywork/AddCustomAttributeOnCustomerGrid/Block/Adminhtml/Customer/
[php]
<?php
class Mywork_AddCustomAttributeOnCustomerGrid_Block_Adminhtml_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{

public function setCollection($collection) {
$collection->addAttributeToSelect(‘pan_number’);
$collection->addAttributeToSelect(‘father_name’);
$collection->addAttributeToSelect(‘mother_name’);
$this->_collection = $collection;
}

protected function _prepareColumns()
{
$this->addColumnAfter(‘pan_number’, array(
‘header’ => Mage::helper(‘customer’)->__(‘Pan Number’),
‘width’ => ‘150’,
‘index’ => ‘pan_number’
),’email’);

$this->addColumnAfter(‘father_name’, array(
‘header’ => Mage::helper(‘customer’)->__(‘Father Name’),
‘width’ => ‘150’,
‘index’ => ‘father_name’
),’pan_number’);

$this->addColumnAfter(‘mother_name’, array(
‘header’ => Mage::helper(‘customer’)->__(‘Mother Name’),
‘width’ => ‘150’,
‘index’ => ‘mother_name’
),’father_name’);

return parent::_prepareColumns();
}

}
?>
[/php]

Step4:- Now create the Data.php file in app/code/local/Mywork/AddCustomAttributeOnCustomerGrid/Helper/
[php]
<?php
class Mywork_AddCustomAttributeOnCustomerGrid_Helper_Data extends Mage_Core_Helper_Abstract
{
}
?>
[/php]

Add new customer attribute in magento1

In this extension we created three customer attribute (i) “Pan Number” which is must be required.
(ii) “Father Name” which is not required.
(iii) “Mother Name” which is not required.

Note:- you can download extension from this rar_image
Add custom customer attribute in magento through custom module.
Note:- Firstly disable the cache from Cache Management tab and which is in System tab and select the rwd theme.
there are many steps to create the extension.
Step1:- Create Mywork_AddCustomCustomerAttribute.xml file in app/etc/modules/.
where Mywork is Namespace and AddCustomCustomerAttribute is Modulename.
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_AddCustomCustomerAttribute>
<active>true</active>
<codePool>local</codePool>
</Mywork_AddCustomCustomerAttribute>
</modules>
</config>
[/php]
Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/AddCustomCustomerAttribute/etc/
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_AddCustomCustomerAttribute>
<version>1.0.0</version>
</Mywork_AddCustomCustomerAttribute>
</modules>
<global>
<helpers>
<AddCustomCustomerAttribute>
<class>Mywork_AddCustomCustomerAttribute_Helper</class>
</AddCustomCustomerAttribute>
</helpers>
<models>
<AddCustomCustomerAttribute>
<class>Mywork_AddCustomCustomerAttribute_Model</class>
<resourceModel>AddCustomCustomerAttribute_mysql4</resourceModel>
</AddCustomCustomerAttribute>
</models>
<resources>
<AddCustomCustomerAttribute_setup>
<setup>
<module>Mywork_AddCustomCustomerAttribute</module>
<class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</AddCustomCustomerAttribute_setup>
<AddCustomCustomerAttribute_write>
<connection>
<use>core_write</use>
</connection>
</AddCustomCustomerAttribute_write>
<AddCustomCustomerAttribute_read>
<connection>
<use>core_read</use>
</connection>
</AddCustomCustomerAttribute_read>
</resources>

<fieldsets>
<checkout_onepage_quote>
<pan_number>
<to_customer>*</to_customer>
</pan_number>
<father_name>
<to_customer>*</to_customer>
</father_name>
<mother_name>
<to_customer>*</to_customer>
</mother_name>
</checkout_onepage_quote>
<customer_account>
<pan_number>
<to_quote>*</to_quote>
</pan_number>
<father_name>
<to_quote>*</to_quote>
</father_name>
<mother_name>
<to_quote>*</to_quote>
</mother_name>
</customer_account>
</fieldsets>
</global>
</config>
[/php]

Note:- If you want to add custom fields in the “checkout_register” form then use

[php]<fieldsets>
<checkout_onepage_quote>
<pan_number>
<to_customer>*</to_customer>
</pan_number>
<father_name>
<to_customer>*</to_customer>
</father_name>
<mother_name>
<to_customer>*</to_customer>
</mother_name>
</checkout_onepage_quote>
<customer_account>
<pan_number>
<to_quote>*</to_quote>
</pan_number>
<father_name>
<to_quote>*</to_quote>
</father_name>
<mother_name>
<to_quote>*</to_quote>
</mother_name>
</customer_account>
</fieldsets>
[/php]

other wise not.

Step3:- Now create the mysql4-install-1.0.0.php file in app/code/local/Mywork/AddCustomCustomerAttribute/sql/AddCustomCustomerAttribute_setup/
[php]
<?php
$installer = $this;
$installer->startSetup();

$installer->addAttribute("customer", "pan_number", array(
"type" => "varchar",
"backend" => "",
"label" => "Pan Number",
"input" => "text",
"source" => "",
"visible" => true,
"required" => true,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => "Customer Pan Number like 1236547898"

));

$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "pan_number");

$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100)
;
$attribute->save();

$installer->addAttribute("customer", "father_name", array(
"type" => "varchar",
"backend" => "",
"label" => "Father Name",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""

));

$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "father_name");

$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 101);
$attribute->save();

$installer->addAttribute("customer", "mother_name", array(
"type" => "varchar",
"backend" => "",
"label" => "Mother Name",
"input" => "text",
"source" => "",
"visible" => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique" => false,
"note" => ""

));

$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "mother_name");
$used_in_forms=array();
$used_in_forms[]="adminhtml_customer";
$used_in_forms[]="checkout_register";
$used_in_forms[]="customer_account_create";
$used_in_forms[]="customer_account_edit";
$used_in_forms[]="adminhtml_checkout";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 102)
;
$attribute->save();

$connection = $installer->getConnection();
$quote_table_name = $installer->getTable(‘sales/quote’);

$connection->addColumn($quote_table_name, ‘pan_number’, array(
‘type’ => Varien_Db_Ddl_Table::TYPE_TEXT,
‘length’ => 255,
‘nullable’ => false,
‘comment’ => ‘Pan Number Data’,
));

$connection->addColumn($quote_table_name, ‘father_name’, array(
‘type’ => Varien_Db_Ddl_Table::TYPE_TEXT,
‘length’ => 255,
‘nullable’ => false,
‘comment’ => ‘Father Name’,
));

$connection->addColumn($quote_table_name, ‘mother_name’, array(
‘type’ => Varien_Db_Ddl_Table::TYPE_TEXT,
‘length’ => 255,
‘nullable’ => false,
‘comment’ => ‘Mother Name’,
));

$installer->endSetup();
[/php]

Note:- If you want to add custom fields in the “checkout_register” form then use
[php]
$connection = $installer->getConnection();
$quote_table_name = $installer->getTable(‘sales/quote’);

$connection->addColumn($quote_table_name, ‘pan_number’, array(
‘type’ => Varien_Db_Ddl_Table::TYPE_TEXT,
‘length’ => 255,
‘nullable’ => false,
‘comment’ => ‘Pan Number Data’,
));

$connection->addColumn($quote_table_name, ‘father_name’, array(
‘type’ => Varien_Db_Ddl_Table::TYPE_TEXT,
‘length’ => 255,
‘nullable’ => false,
‘comment’ => ‘Father Name’,
));

$connection->addColumn($quote_table_name, ‘mother_name’, array(
‘type’ => Varien_Db_Ddl_Table::TYPE_TEXT,
‘length’ => 255,
‘nullable’ => false,
‘comment’ => ‘Mother Name’,
));
[/php]
otherwise not.

Step4:- Now create the Data.php file in app/code/local/Mywork/AddCustomCustomerAttribute/Helper/
[php]
<?php
class Mywork_AddCustomCustomerAttribute_Helper_Data extends Mage_Core_Helper_Abstract
{
}
[/php]

Now we are going to design steps

(1) register.phtml which is in the app/design/frontend/rwd/default/template/persistent/customer/form/
[php]
<!——————————-Pan Number——————————->
<li>
<label for="pan_number" class="required"><em>*</em><?php echo $this->__(‘Pan Number’) ?></label>
<div class="input-box">
<input type="text" autocapitalize="off" autocorrect="off" spellcheck="false" name="pan_number" id="pan_number" value="<?php echo $this->escapeHtml($this->getFormData()->getPanNumber()) ?>" title="<?php echo $this->__(‘Pan Number’) ?>" class="input-text required-entry" />
</div>
</li>

<!——————————-Pan Number——————————->

<!——————————-Father Name——————————->
<li>
<label for="father_name"><?php echo $this->__(‘Father Name’) ?></label>
<div class="input-box">
<input type="text" autocapitalize="off" autocorrect="off" spellcheck="false" name="father_name" id="father_name" value="<?php echo $this->escapeHtml($this->getFormData()->getFatherName()) ?>" title="<?php echo $this->__(‘Father Name’) ?>" class="input-text" />
</div>
</li>

<!——————————-Father Name——————————->

<!——————————-Mother Name——————————->
<li>
<label for="mother_name"><?php echo $this->__(‘Mother Name’) ?></label>
<div class="input-box">
<input type="text" autocapitalize="off" autocorrect="off" spellcheck="false" name="mother_name" id="mother_name" value="<?php echo $this->escapeHtml($this->getFormData()->getMotherName()) ?>" title="<?php echo $this->__(‘Mother Name’) ?>" class="input-text" />
</div>
</li>

<!——————————-Mother Name——————————->
[/php]

Note:- Add this code after
[php]
<ul class="form-list">
<li class="fields">
<?php echo $this->getLayout()->createBlock(‘customer/widget_name’)->setObject($this->getFormData())->setForceUseCustomerAttributes(true)->toHtml() ?>
</li>
[/php]

(2) edit.phtml which is in the app/design/frontend/rwd/default/template/customer/form/
[php]
<!——————————-Pan Number——————————->
<li>
<label for="pan_number" class="required"><em>*</em><?php echo $this->__(‘Pan Number’) ?></label>
<div class="input-box">
<input type="text" autocapitalize="off" autocorrect="off" spellcheck="false" name="pan_number" id="pan_number" value="<?php echo $this->escapeHtml($this->getCustomer()->getPanNumber()) ?>" title="<?php echo $this->__(‘Pan Number’) ?>" class="input-text required-entry" />
</div>
</li>

<!——————————-Pan Number——————————->

<!——————————-Father Name——————————->
<li>
<label for="father_name"><?php echo $this->__(‘Father Name’) ?></label>
<div class="input-box">
<input type="text" autocapitalize="off" autocorrect="off" spellcheck="false" name="father_name" id="father_name" value="<?php echo $this->escapeHtml($this->getCustomer()->getFatherName()) ?>" title="<?php echo $this->__(‘Father Name’) ?>" class="input-text" />
</div>
</li>

<!——————————-Father Name——————————->

<!——————————-Mother Name——————————->
<li>
<label for="mother_name"><?php echo $this->__(‘Mother Name’) ?></label>
<div class="input-box">
<input type="text" autocapitalize="off" autocorrect="off" spellcheck="false" name="mother_name" id="mother_name" value="<?php echo $this->escapeHtml($this->getCustomer()->getMotherName()) ?>" title="<?php echo $this->__(‘Mother Name’) ?>" class="input-text" />
</div>
</li>

<!——————————-Mother Name——————————->
[/php]
Add this code after
[php]
<ul class="form-list">
<li class="fields">
<?php echo $this->getLayout()->createBlock(‘customer/widget_name’)->setObject($this->getCustomer())->toHtml() ?>
</li>
[/php]

(3) billing.phtml which is in the app/design/frontend/rwd/default/template/persistent/checkout/onepage/form/
[php]
<!——————————-Pan Number——————————->
<li class="fields">
<div class="field">
<label for="billing:pan_number" class="required"><em>*</em><?php echo $this->__(‘Pan Number’) ?></label>
<div class="input-box">
<input type="text" name="billing[pan_number]" id="billing:pan_number" value="<?php echo $this->getQuote()->getCustomer()->getPanNumber(); ?>" title="<?php echo $this->__(‘Pan Number’) ?>" class="input-text required-entry" />
</div>
</div>
</li>

<!——————————-Pan Number——————————->

<!——————————-Father Name——————————->
<li class="fields">
<div class="field">
<label for="billing:father_name"><?php echo $this->__(‘Father Name’) ?></label>
<div class="input-box">
<input type="text" name="billing[father_name]" id="billing:father_name" value="<?php echo $this->getQuote()->getCustomer()->getFatherName(); ?>" title="<?php echo $this->__(‘Father Name’) ?>" class="input-text" />
</div>
</div>
</li>

<!——————————-Father Name——————————->

<!——————————-Mother Name——————————->
<li class="fields">
<div class="field">
<label for="billing:mother_name"><?php echo $this->__(‘Mother Name’) ?></label>
<div class="input-box">
<input type="text" name="billing[mother_name]" id="billing:mother_name" value="<?php echo $this->getQuote()->getCustomer()->getMotherName(); ?>" title="<?php echo $this->__(‘Mother Name’) ?>" class="input-text" />
</div>
</div>
</li>

<!——————————-Mother Name——————————->
[/php]
Add this code after
[php]
<li id="billing-new-address-form"<?php if ($this->customerHasAddresses()): ?> style="display:none;"<?php endif; ?> class="scaffold-form">
<div class="fieldset">
<input type="hidden" name="billing[address_id]" value="<?php echo $this->getAddress()->getId() ?>" id="billing:address_id" />
<ul>
<li class="fields"><?php echo $this->getLayout()->createBlock(‘customer/widget_name’)->setObject($this->getAddress()->getFirstname() ? $this->getAddress() : $this->getQuote()->getCustomer())->setForceUseCustomerRequiredAttributes(!$this->isCustomerLoggedIn())->setFieldIdFormat(‘billing:%s’)->setFieldNameFormat(‘billing[%s]’)->toHtml() ?></li>
[/php]

Create block for ImageSlider extension

(1) Firstly create the ImageSlider.php file in app/code/local/Mywork/ImageSlider/Block/Adminhtml/

[php]
<?php
class Mywork_ImageSlider_Block_Adminhtml_ImageSlider extends Mage_Adminhtml_Block_Widget_Grid_Container
{
public function __construct()
{
$this->_controller = ‘adminhtml_imageslider’;
$this->_blockGroup = ‘imageslider’;
$this->_headerText = Mage::helper(‘imageslider’)->__(‘Image Slider’);
$this->_addButtonLabel = Mage::helper(‘imageslider’)->__(‘Add Image’);
parent::__construct();
//$this->_removeButton(‘add’);
}
}
?>
[/php]

Note:- (1) in this file $this->_controller represent the controller.
(2) $this->_blockGroup is represent the module name.

(2) Create the Grid.php file in app/code/local/Mywork/ImageSlider/Block/Adminhtml/ImageSlider/

[php]
<?php

class Mywork_ImageSlider_Block_Adminhtml_ImageSlider_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

public function __construct()
{
parent::__construct();
$this->setId(‘imgsliderGrid’);
$this->setDefaultSort(‘img_id’);
$this->setDefaultDir(‘DESC’);
$this->setSaveParametersInSession(true);
}

protected function _prepareCollection()
{
$collection = Mage::getModel(‘imageslider/imgslider’)->getCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}

protected function _prepareColumns()
{
$this->addColumn(‘img_id’, array(
‘header’ => Mage::helper(‘imageslider’)->__(‘ID’),
‘align’ =>’right’,
‘width’ => ’50px’,
‘index’ => ‘img_id’,
));

$this->addColumn(‘img_name’, array(
‘header’=>Mage::helper(‘imageslider’)->__(‘Image Title’),
//’filter’=>false,
‘index’=>’img_name’,
‘align’ => ‘left’,
‘width’ => ’50px’,

));

$this->addColumn(‘image’, array(
‘header’ => Mage::helper(‘imageslider’)->__(‘Image’),
‘filter’=>false,
‘align’ =>’left’,
‘width’ => ’50px’,
‘index’ => ‘image’,
‘renderer’ => ‘imageslider/adminhtml_imageslider_renderer_imagename’,
));

$this->addColumn(‘img_desc’, array(
‘header’ => Mage::helper(‘imageslider’)->__(‘Image Description’),
‘align’ =>’left’,
‘filter’=>false,
‘width’ => ’80px’,
‘index’ => ‘img_desc’,
));

$this->addColumn(‘status’, array(
‘header’ => Mage::helper(‘imageslider’)->__(‘Status’),
‘align’ =>’left’,
//’filter’=>false,
‘width’ => ’80px’,
‘index’ => ‘status’,
‘type’ => ‘options’,
‘options’ => array(
1 => ‘Enabled’,
0 => ‘Disabled’,
),
));

$this->addColumn(‘action’,
array(
‘header’ => Mage::helper(‘imageslider’)->__(‘Action’),
‘width’ => ‘100’,
‘type’ => ‘action’,
‘getter’ => ‘getId’,
‘actions’ => array(

array(
‘caption’ => Mage::helper(‘imageslider’)->__(‘Edit’),
‘url’ => array(‘base’=> ‘*/*/edit’),
‘field’ => ‘id’
)
),
‘filter’ => false,
‘sortable’ => false,
‘index’ => ‘stores’,
‘is_system’ => true,
));

/*if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn(‘store_id’, array(
‘header’ => Mage::helper(‘bannerpro’)->__(‘Store View’),
‘index’ => ‘store_id’,
‘type’ => ‘store’,
‘width’ => ‘150px’,
‘store_all’ => true,
‘store_view’ => true,
‘sortable’ => false,
‘filter_condition_callback’
=> array($this, ‘_filterStoreCondition’),
));
}*/

return parent::_prepareColumns();
}

protected function _prepareMassaction()
{
$this->setMassactionIdField(‘img_id’);
$this->getMassactionBlock()->setFormFieldName(‘delete_massimage’);
$this->getMassactionBlock()->addItem(‘delete’, array(
‘label’ => Mage::helper(‘imageslider’)->__(‘Delete’),
‘url’ => $this->getUrl(‘*/*/massDelete’),
‘confirm’ => Mage::helper(‘imageslider’)->__(‘Are you sure?’)
));

$statuses = Mage::getSingleton(‘imageslider/status’)->getOptionArray();
// array_unshift($statuses, array(‘label’=>”, ‘value’=>”));
//print_r($statuses); die;

$this->getMassactionBlock()->addItem(‘status’, array(
‘label’=> Mage::helper(‘imageslider’)->__(‘Change status’),
‘url’ => $this->getUrl(‘*/*/massStatus’, array(‘_current’=>true)),
‘additional’ => array(
‘visibility’ => array(
‘name’ => ‘status’,
‘type’ => ‘select’,
‘class’ => ‘required-entry’,
‘label’ => Mage::helper(‘imageslider’)->__(‘Status’),
‘values’ => $statuses
)
)
));

return $this;
}

/**
* Row click URL
*
* @return string
*/
public function getRowUrl($row)
{
return $this->getUrl(‘*/*/edit’, array(‘img_id’ => $row->getId()));
}
}
[/php]

Note:- 1) this file represent the image slider data into a grid view.

(3) Create the Edit.php file in app/code/local/Mywork/ImageSlider/Block/Adminhtml/ImageSlider/

[php]
<?php

class Mywork_ImageSlider_Block_Adminhtml_ImageSlider_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
{
public function __construct()
{
parent::__construct();
$this->_controller = ‘adminhtml_imageslider’;
$this->_blockGroup = ‘imageslider’;
$this->_mode=’edit’;
//$this->_headerText = Mage::helper(‘imageslider’)->__(‘Image Slider Information’);
$this->_updateButton(‘save’, ‘label’, Mage::helper(‘imageslider’)->__(‘Save Image’));
$this->_updateButton(‘delete’, ‘label’, Mage::helper(‘imageslider’)->__(‘Delete Image’));

}

/**
* Get edit form container header text
*
* @return string
*/
public function getHeaderText()
{
if (Mage::registry(‘imageslider_model’)->getId()) {
return Mage::helper(‘imageslider’)->__(‘Edit Image Slider’);
}
else {
return Mage::helper(‘imageslider’)->__(‘New Image Slider’);
}
}

}
[/php]

(4) Create the Form.php file in app/code/local/Mywork/ImageSlider/Block/Adminhtml/ImageSlider/Edit/

[php]
<?php
class Mywork_ImageSlider_Block_Adminhtml_ImageSlider_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{

/**
* Init form
*/
public function __construct()
{
parent::__construct();
$this->setId(‘images_form’);
// $this->setTitle(Mage::helper(‘adminhtml’)->__(‘Images Information’));
}

protected function _prepareForm()
{
$model = Mage::registry(‘imageslider_model’);
$form = new Varien_Data_Form(array(
‘id’ => ‘edit_form’,
‘action’ => $this->getUrl(‘*/*/save’),
‘method’ => ‘post’,
‘enctype’ => ‘multipart/form-data’
));

$fieldset = $form->addFieldset(‘add_imgslider_form’, array(‘legend’ => Mage::helper(‘imageslider’)->__(‘Image Slider’)));

if ($model->getId()) {
$fieldset->addField(‘img_id’, ‘hidden’, array(
‘name’ => ‘img_id’,

));
}

$fieldset->addField(‘img_name’, ‘text’, array(
‘label’ => Mage::helper(‘imageslider’)->__(‘Filename’),
‘name’ => ‘img_name’,
‘required’ => true,
‘note’ => Mage::helper(‘imageslider’)->__(‘example: slider1’),
// ‘value’ => $model->getImgName()
));

$fieldset->addField(‘image’, ‘image’, array(
‘label’ => Mage::helper(‘imageslider’)->__(‘Images’),
‘class’ => ‘required-file Please select a file’,
‘required’ => false,
‘name’ => ‘image’,
//’value’ => $model->getImgDesc()
));

$fieldset->addField(‘img_desc’, ‘textarea’, array(
‘label’ => Mage::helper(‘imageslider’)->__(‘Image Description’),
// ‘class’ => ‘required-file Please select a file’,
‘required’ => false,
‘name’ => ‘img_desc’,
//’value’ => $model->getImgDesc()

));

$form->setUseContainer(true);
$this->setForm($form);

if ( Mage::getSingleton(‘adminhtml/session’)->getImagesliderModel() )
{
$form->setValues(Mage::getSingleton(‘adminhtml/session’)->getImagesliderModel());
Mage::getSingleton(‘adminhtml/session’)->setBannerproData(null);
} elseif ( Mage::registry(‘imageslider_model’) ) {
$form->setValues(Mage::registry(‘imageslider_model’)->getData());
}

return parent::_prepareForm();
}

}
[/php]

(5) Create the Imagename.php file in app/code/local/Mywork/ImageSlider/Block/Adminhtml/ImageSlider/Renderer/

[php]
<?php
class Mywork_ImageSlider_Block_Adminhtml_ImageSlider_Renderer_Imagename extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
if($row[‘image’]!=""){
$img_directory=Mage::getConfig()->getBaseDir(‘media’).’/’.$row[‘image’];
$image_url= Mage::getBaseUrl(‘media’).$row[‘image’];
if(file_exists($img_directory)){
return ‘<img src="’.$image_url.’" width="50px" />’;
}else{
return ”;
}

}else{
return ”;
}

}

}
?>
[/php]

Create ImageSliderController.php file

Create the ImageSliderController.php file in app/code/local/Mywork/ImageSlider/controllers/Adminhtml/
[php]
<?php
class Mywork_ImageSlider_Adminhtml_ImageSliderController extends Mage_Adminhtml_Controller_Action
{
public function indexAction() {
$this->loadLayout()
->renderLayout();
}
public function newAction() {
// the same form is used to create and edit
$this->_forward(‘edit’);
}

public function editAction() {
//echo ‘Hello’; die;

$this->_title($this->__(‘Image Slider’));

// 1. Get ID and create model
$id = $this->getRequest()->getParam(‘id’);

$model = Mage::getModel(‘imageslider/imgslider’);

// 2. Initial checking
if ($id) {
$model->load($id);
if (! $model->getId()) {
Mage::getSingleton(‘adminhtml/session’)->addError(
Mage::helper(‘imageslider’)->__(‘This Image Slider no longer exists.’));
$this->_redirect(‘*/*/’);
return;
}
}

$this->_title($model->getId() ? $model->getImgName() : $this->__(‘New Image Slider’));

// 3. Set entered data if was error when we do save
$data = Mage::getSingleton(‘adminhtml/session’)->getFormData(true);

if (!empty($data)) {
$model->setData($data);
}

// 4. Register model to use later in blocks
Mage::register(‘imageslider_model’, $model);

$this->loadLayout()
->_addBreadcrumb(
$id ? Mage::helper(‘imageslider’)->__(‘Edit Image Slider’) : Mage::helper(‘imageslider’)->__(‘New Image Slider’),
$id ? Mage::helper(‘imageslider’)->__(‘Edit Image Slider’) : Mage::helper(‘imageslider’)->__(‘New Image Slider’))
->_addContent($this->getLayout()->createBlock(‘imageslider/adminhtml_imageslider_edit’))
->renderLayout();
}

public function saveAction()
{
$write_conn = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
$data = $this->getRequest()->getPost();
$img_name=$_FILES["image"]["name"];
if(isset($img_name) && $img_name!=”){
try{
$uploader1 = new Varien_File_Uploader(‘image’);
// Any extention would work
$uploader1->setAllowedExtensions(array(‘jpg’,’jpeg’,’gif’,’png’));
$uploader1->setAllowRenameFiles(false);
$uploader1->setFilesDispersion(false);
// We set media as the upload dir
$path = Mage::getBaseDir(‘media’) . DS . ‘imageslider’ . DS ;
$uploader1->save($path, $img_name);
}catch (Exception $e){

}
//$data[‘image’] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).’imageslider/’.$img_name;
$data[‘image’] = ‘imageslider/’.$img_name;

}else{
if(isset($data[‘image’][‘delete’]) && $data[‘image’][‘delete’] == 1) {
if ($data[‘image’][‘value’] != ”):
unlink(Mage::getBaseDir(‘media’) . DS . $data[‘image’][‘value’]);
$data[‘image’] = ”;
endif;

} else {
unset($data[‘image’]);
}
}

$model = Mage::getModel(‘imageslider/imgslider’);
$model->setData($data)
->setId($this->getRequest()->getParam(‘img_id’));
$model->save();

Mage::getSingleton(‘adminhtml/session’)->addSuccess(
Mage::helper(‘adminhtml’)->__(
‘Image Slider is added’
));
$this->_redirect(‘*/*/index’);

}

public function deleteAction() {
if( $this->getRequest()->getParam(‘id’) > 0 ) {
try {
$model = Mage::getModel(‘imageslider/imgslider’);

$model->setId($this->getRequest()->getParam(‘id’))
->delete();

Mage::getSingleton(‘adminhtml/session’)->addSuccess(Mage::helper(‘adminhtml’)->__(‘Item was successfully deleted’));
$this->_redirect(‘*/*/’);
} catch (Exception $e) {
Mage::getSingleton(‘adminhtml/session’)->addError($e->getMessage());
$this->_redirect(‘*/*/edit’, array(‘id’ => $this->getRequest()->getParam(‘id’)));
}
}
$this->_redirect(‘*/*/’);
}

public function massDeleteAction() {
$delete_massimageIds = $this->getRequest()->getParam(‘delete_massimage’);
if(!is_array($delete_massimageIds)) {
Mage::getSingleton(‘adminhtml/session’)->addError(Mage::helper(‘adminhtml’)->__(‘Please select item(s)’));
} else {
try {
foreach ($delete_massimageIds as $delete_massimageId) {
$imgslider_data = Mage::getModel(‘imageslider/imgslider’)->load($delete_massimageId);
$imgslider_data->delete();
}
Mage::getSingleton(‘adminhtml/session’)->addSuccess(
Mage::helper(‘adminhtml’)->__(
‘Total of %d record(s) were successfully deleted’, count($delete_massimageIds)
)
);
} catch (Exception $e) {
Mage::getSingleton(‘adminhtml/session’)->addError($e->getMessage());
}
}
$this->_redirect(‘*/*/index’);
}

public function massStatusAction()
{
$delete_massimageIds = $this->getRequest()->getParam(‘delete_massimage’);
if(!is_array($delete_massimageIds)) {
Mage::getSingleton(‘adminhtml/session’)->addError($this->__(‘Please select item(s)’));
} else {
try {
foreach ($delete_massimageIds as $delete_massimageId) {
$bannerpro = Mage::getSingleton(‘imageslider/imgslider’)
->load($delete_massimageId)
->setStatus($this->getRequest()->getParam(‘status’))
->setIsMassupdate(true)
->save();
}
$this->_getSession()->addSuccess(
$this->__(‘Total of %d record(s) were successfully updated’, count($delete_massimageIds))
);
} catch (Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
}
$this->_redirect(‘*/*/index’);
}

}
?>
[/php]

Note:- 1) In this controller file when we want to add image then we use newAction() function.
2) In this controller file when we want to edit image then we use editAction() function.
3) to save the image we use saveAction() function.
4) to delete the image we use deleteAction() function.
5) to delete more than one image at a time then we use massDeleteAction() function.
6) to change the image status then we use massStatusAction() function.

Create imageslider.xml file

Create the imageslider.xml file in app/design/adminhtml/default/default/layout/
[php]
<?xml version="1.0"?>
<layout version="1.0.0">
<imageslider_adminhtml_imageslider_index>
<reference name="content">
<block type="imageslider/adminhtml_imageslider" name="imageslider" />
</reference>
</imageslider_adminhtml_imageslider_index>
</layout>
[/php]

Note:- 1) imageslider_adminhtml_imageslider_index element represent the ModuleName_adminhtml_ControllerName_FunctionName
2) reference element which has name attribute and value is content.
in this block type imageslider/adminhtml_imageslider represents Modulename/adminhtml_BlockfileName.

Create config.xml file

Firstly create the config.xml file in app/code/local/Mywork/ImageSlider/etc/
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_ImageSlider>
<version>1.0.0</version>
</Mywork_ImageSlider>
</modules>
<admin>
<routers>
<imageslider>
<use>admin</use>
<args>
<module>Mywork_ImageSlider</module>
<frontName>imageslider</frontName>
</args>
</imageslider>
</routers>
</admin>

<adminhtml>
<menu>
<imagesliderdata>
<title>Image Slider</title>
<sort_order>200</sort_order>
<children>
<imgsld module="imageslider">
<title>Manage Image Slider</title>
<sort_order>200</sort_order>
<action>imageslider/adminhtml_imageslider</action>
</imgsld>
</children>
</imagesliderdata>
</menu>

<layout>
<updates>
<imageslider>
<file>imageslider.xml</file>
</imageslider>
</updates>
</layout>

</adminhtml>

<global>
<helpers>
<imageslider>
<class>Mywork_ImageSlider_Helper</class>
</imageslider>
</helpers>
<blocks>
<imageslider>
<class>Mywork_ImageSlider_Block</class>
</imageslider>
</blocks>
<models>
<imageslider>
<class>Mywork_ImageSlider_Model</class>
<resourceModel>imageslider_mysql4</resourceModel>
</imageslider>
<imageslider_mysql4>
<class>Mywork_ImageSlider_Model_Mysql4</class>
<entities>
<imgslider>
<table>image_slider</table>
</imgslider>
</entities>
</imageslider_mysql4>
</models>

<resources>
<imageslider_setup>
<setup>
<module>Mywork_ImageSlider</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</imageslider_setup>

<imageslider_write>
<connection>
<use>core_write</use>
</connection>
</imageslider_write>
<imageslider_read>
<connection>
<use>core_read</use>
</connection>
</imageslider_read>

</resources>

</global>

</config>
[/php]
Note:- 1) Firstly create the module version in the version element.
2) create the routers which is in admin element and router is imageslider.
3) create the menu in the adminhtml element where action is imageslider/adminhtml_imageslider.
4) create the imageslider.xml file which is in the app/design/adminhtml/default/default/layout/.
5) to create blocks, models and helpers in the global element.
6) to create the table must be in the resources element.

Create Mywork_ImageSlider.xml file

Firstly create the Mywork_ImageSlider.xml file in app/etc/modules/
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_ImageSlider>
<active>true</active>
<codePool>local</codePool>
</Mywork_ImageSlider>
</modules>
</config>
[/php]
Note:- 1) If you want to active the extensions then use true otherwise false.
2) there is three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.
3) When this extension call then firstly call this file, if active true then run otherwise not run.

Create Controller for Backend Module

How to create custom controller on backend in magento?

In this extension we created two controller IndexController and GetdataController.

Note:- you can download extension from this rar_image
Note:- Firstly disable the cache from Cache Management tab and which is in System tab.
there are many steps to create this extension.
Step1:- Create Mywork_Useadmcontroller.xml file in app/etc/modules/.
where Mywork is Namespace and Useadmcontroller is Modulename.
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_Useadmcontroller>
<active>true</active>
<codePool>local</codePool>
</Mywork_Useadmcontroller>
</modules>
</config>
[/php]
Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create our own extension then we use the local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/Useadmcontroller/etc/
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_Useadmcontroller>
<version>1.0.0</version>
</Mywork_Useadmcontroller>
</modules>

<admin>
<routers>
<useadmcontroller>
<use>admin</use>
<args>
<module>Mywork_Useadmcontroller</module>
<frontName>useadmcontroller</frontName>
</args>
</useadmcontroller>
</routers>
</admin>

<adminhtml>
<menu>
<Mainmenu>
<title>Mywork</title>
<sort_order>200</sort_order>
<children>
<menu1>
<title>Use Controller</title>
<sort_order>1</sort_order>
<children>
<submenu1 module="useadmcontroller">
<title>Use Index Controller with index Action</title>
<sort_order>1</sort_order>
<action>useadmcontroller/adminhtml_index</action>
</submenu1>
<submenu2 module="useadmcontroller">
<title>Use Index Controller with mydetails Action</title>
<sort_order>2</sort_order>
<action>useadmcontroller/adminhtml_index/mydetails</action>
</submenu2>
<submenu3 module="useadmcontroller">
<title>Use Getdata Controller with index Action</title>
<sort_order>3</sort_order>
<action>useadmcontroller/adminhtml_getdata</action>
</submenu3>
<submenu4 module="useadmcontroller">
<title>Use Getdata Controller with mydetails Action</title>
<sort_order>4</sort_order>
<action>useadmcontroller/adminhtml_getdata/mydetails</action>
</submenu4>
</children>
</menu1>
</children>
</Mainmenu>
</menu>
</adminhtml>

<global>
<helpers>
<useadmcontroller>
<class>Mywork_Useadmcontroller_Helper</class>
</useadmcontroller>
</helpers>
</global>
</config>
[/php]
Note:- In the config.xml file we created the module version, backend module access through useadmcontroller and we define menu which is name is Mywork and it has “Use Controller” menu and it has 4 sub menu
(1) Use Index Controller with index Action
(2) Use Index Controller with mydetails Action
(3) Use Getdata Controller with index Action
(4) Use Getdata Controller with mydetails Action

Step3:- Now create the IndexController.php file in app/code/local/Mywork/Useadmcontroller/controllers/Adminhtml/
[php]
<?php
class Mywork_Useadmcontroller_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action
{
public function indexAction() {

echo ‘Hello, Index controller with index action.’;

}

public function mydetailsAction() {

echo ‘Hello, Index controller with mydetails action.’;

}
}
?>
[/php]

Note:- In this file, we create two functions.
(1) indexAction() (2) mydetailsAction()

Step4:- Now create the GetdataController.php file in app/code/local/Mywork/Useadmcontroller/controllers/Adminhtml/
[php]
<?php
class Mywork_Useadmcontroller_Adminhtml_GetdataController extends Mage_Adminhtml_Controller_Action
{
public function indexAction() {

echo ‘Hello Getdata Controller with index action.’;

}

public function mydetailsAction() {

echo ‘Hello Getdata Controller with mydetails action.’;

}
}
?>
[/php]
Note:- In this file, we create two functions.
(1) indexAction() (2) mydetailsAction()

Step5:- Now create the Data.php file in app/code/local/Mywork/Useadmcontroller/Helper/
[php]
<?php
class Mywork_Useadmcontroller_Helper_Data extends Mage_Core_Helper_Abstract
{

}
?>
[/php]

admin_controller

Magento1 Interview Questions

Q1: What is Magento

Magento is open source Ecommerce CMS which is built in PHP. It is most popular for ecommerce website. It uses multiple other PHP frameworks such as ZEND, Laminas and Symfony.

Magento is also specially designed for online sellers who need a simple e-commerce store. It is free to all so anyone can create their own website. Magento is easy to install, easy to customize, and easy to scale, and this makes it a popular choice for all sorts of companies, from small businesses to huge multinationals.

Magento was acquired by Adobe Inc in May 2018 for $1.68 Billion USD.

Q2: Module’s version information save in which table?

All the module’s version information save into the “core_resource” table. If you want to change the version of the module then the old version replaced by a new version.

Q3: How to create a store in magento1?

Firstly login to the magento admin panel after that click the “System/Manage Stores” tab. Now click the “Create Store” button. Now select the website, write the name of the store and now select the root category after that click the “Save Store” button.
create_store

Q4: How to change theme in magento1?

Change theme in Magento is very simple, firstly login to Magento admin panel, after that go to the System tab, after this click the Design tab and after that click the Add Design Change button and now choose the theme what do you want.

Note:- system/design

Q5: How to check customer is logged in or not in magento1?

If you want to check customer is logged in or not through code so we can use below code.


$check_login=Mage::getSingleton("customer/session")->isLoggedIn();
if(!$check_login){
echo 'Not Logged in';
}else{
echo 'logged in';
}

Q6: How to get static block in phtml file in magento1?


$this->getLayout()->createBlock('cms/block')->setBlockId('static block code')->toHtml();

Q7: Difference between register, registry and unregister in magento1?

Magento registry is used to share information anywhere in the Magento store. It works like a Global variable. there are 3 static methods (i) register (ii) registry (iii) unregister of the Mage class.

(i) Mage::register:- It is used to register the value in the registry.


Mage::register('name', $value);

(ii) Mage::registry:- (ii) It is used to get the value from the registry.

Mage::registry('name');

(iii) Mage::unregister:- (ii) this method is used to remove it from the registry.


Mage::unregister('name');

Q8: What is Singleton in Magento1?

It is a design pattern, it is used for memory utilization. When you create an Object then It always checks Object is already created or not if already created then use this, otherwise create a new object.

Q9: Difference between Mage::getSingleton() and Mage::getModel() in magento1?

Mage::getSingleton():- It always check that Object is already created or not if already created then use this, otherwise create new object. It follows the Singleton design pattern.


Mage::getSigleton('ModuleName/ModelName');

Mage::getModel():- It always create new Object. It follow Standard Factory pattern.


Mage::getModel('ModuleName/ModelName');

Q10: What is EAV model in Magento1?

EAV model:- EAV(Entity Attribute Value) is a data model. When we want to add a new field in the table then we do not need to modify the table structure.

Note:- EAV structure for product in magento, product ID is stored in catalog_product_entity_int table, product name in catalog_product_entity_varchar, product price in catalog_product_entity_decimal, product created date in catalog_product_entity_datetime and product description in catalog_product_entity_text table.

Example:- If we want to add a field name is productinfo then we add into catalog_product_entity_varchar.

Q11: Difference between EAV and Flat model in magento1?

(i) EAV model is very useful when we want to add a field in a table structure, not need to modify the structure. In the Data model, when we add a new field then modify the table structure.

(ii) EAV model is very complex for (select, insert and update query) because we use relation from many tables. The data model is very easy to (select, insert, and update query).

(iii) EAV model is slow and the Data model is fast than the EAV model.

Q12: How many types of product in magento1?

There are five types of product
1) Simple Product
2) Configurable Product
3) Grouped Product
4) Bundled Product
5) Virtual Product

Note:- to get the complete information click on the Link

Q13: How many types of code pool in magento1?

1) core pool
2) community pool
3) local pool

Note:- to get the complete information click on the Link

Q14: How to get customer details in magento1?


$customerData = Mage::getSingleton('customer/session')->getCustomer();
print_r($customerData);

Q15: customer attribute save in which tables in magento1?

custom customer attribute save in tables
(1) eav_attribute
(2) customer_eav_attribute
(3) If we use checkout_register form then we use sales_flat_quote table
(4) value of attribute save in tables like
customer_entity_datetime, customer_entity_decimal, customer_entity_int, customer_entity_text, customer_entity_varchar
Note:- attributes value save in a table according to datatype
ex:- if we create attribute data type varchar then save in the customer_entity_varchar and if we create attribute data type int then save in the customer_entity_int so on.

Q16: Customer’s address attribute save in which tables in magento1?

custom customer address attribute save in tables
(1) eav_attribute
(2) customer_eav_attribute
(3) value of attribute save in tables like
customer_address_entity_datetime, customer_address_entity_decimal, customer_address_entity_int, customer_address_entity_text, customer_address_entity_varchar
Note:- attributes value save in a table according to datatype
ex:- if we create attribute data type varchar then save in the customer_address_entity_varchar and if we create attribute data type int then save in the customer_address_entity_int so on.

Select Query

Select Query:- It is used to get the records from the custom table.

Syntax:-


$read_connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$query_data="select * from tablename";
$rowsArray = $read_connection->fetchAll($query_data); // return all rows
$rowArray = $read_connection->fetchRow($query_data); //return row

Example:- Suppose, you have to get all records from the employee table.


$read_connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$query_data="select * from employee";
$rowsArray = $read_connection->fetchAll($query_data); // return all rows

Delete Query

Delete Query:- It is used to delete record from the custom table.

Syntax:-


$write_connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$where = array($write_connection ->quoteInto('tableId =?', table_id));
$write_connection->delete('tablename',$where);

Example:- Suppose, you have to delete records of the employee table which has employee’s 1.


$write_connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$where = array($write_connection ->quoteInto('tableId =?', '1'));
$write_connection->delete('employee',$where);

Update Query

Update Query:- It is used to update record into the custom table.

Syntax:-


$write_connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$fields_arr = array();
$fields_arr['field_name1'] = value_1;
$fields_arr['field_name2'] = value_2;
.....................................
$fields_arr['field_namen'] = value_n;
$where = $write_connection ->quoteInto('tableId =?', field_id);
$write_connection->update('tablename', $fields_arr, $where);

Example:- Suppose, you have to update records name and age of the employee table which has employee’s 1.


$write_connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$fields_arr = array();
$fields_arr['name'] = 'John';
$fields_arr['age'] = '29';
$where = $write_connection ->quoteInto('tableId =?', '1');
$write_connection->update('tablename', $fields_arr, $where);

Insert Query

Insert Query:- It is used to insert record into the custom table.

Syntax:-


$write_connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$fields_arr = array();
$fields_arr['field_name1'] = value_1;
$fields_arr['field_name2'] = value_2;
.....................................
$fields_arr['field_namen'] = value_n;
$write_connection ->insert('tablename', $fields_arr);

Example:- Suppose, you have to insert records name and age into the employee table.


$write_connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$fields_arr = array();
$fields_arr['name'] = 'John';
$fields_arr['age'] = '29';
$write_connection ->insert('employee', $fields_arr);

Create table for custom magento1 module

Note:- you can download code from this rar_image
Note:-1) Firstly Disable all cache from the Cache Management tab in System tab.
2) select the default theme of the Magento from the System->Design.

Step1:- Create Mywork_Createtable.xml file in app/etc/modules/.
where Mywork is Namespace and Createtable is Modulename.


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

Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create our own extension then we use the local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/Createtable/etc/config.xml


<?xml version="1.0"?>
<config>
<modules>
<Mywork_Createtable>
<version>1.0.0</version>
</Mywork_Createtable>
</modules>

<frontend>
<routers>
<createtable>
<use>standard</use>
<args>
<module>Mywork_Createtable</module>
<frontName>createtable</frontName>
</args>
</createtable>
</routers>
<layout>
<updates>
<createtable>
<file>createtable.xml</file>
</createtable>
</updates>
</layout>
</frontend>

<global>

<blocks>
<createtable>
<class>Mywork_Createtable_Block</class>
</createtable>
</blocks>

<models>
<createtable>
<class>Mywork_Createtable_Model</class>
<resourceModel>createtable_mysql4</resourceModel>
</createtable>
<createtable_mysql4>
<class>Mywork_Createtable_Model_Mysql4</class>
<entities>
<myinfo>
<table>myinfo</table>
</myinfo>
</entities>
</createtable_mysql4>
</models>

<resources>
<createtable_setup>
<setup>
<module>Mywork_Createtable</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</createtable_setup>

<createtable_write>
<connection>
<use>core_write</use>
</connection>
</createtable_write>
<createtable_read>
<connection>
<use>core_read</use>
</connection>
</createtable_read>

</resources>
</global>
</config>

Note:- 1) In this file we created a version of this module is 1.0.0
2) In the frontend tag, we created routers tag. In routers tag, we define the frontend name is createtable,
and we access the extension value from siteurl/createtable.

Step3:- Now we create the IndexController.php in app/code/local/Mywork/Createtable/controllers/IndexController.php


<?php
class Mywork_Createtable_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
}

Step4:- Now create the Useblock.php in app/code/local/Mywork/Createtable/Block/Createtable.php


<?php
class Mywork_Createtable_Block_Createtable extends Mage_Core_Block_Template
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
}

Step5:- Now create the Myinfo.php in app/code/local/Mywork/Createtable/Model/Myinfo.php


<?php
class Mywork_Createtable_Model_Myinfo extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('createtable/myinfo');
}
}

Step6:- Now create the Myinfo.php in app/code/local/Mywork/Createtable/Model/Mysql4/Myinfo.php


<?php
class Mywork_Createtable_Model_Mysql4_Myinfo extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
// Note that the img_id refers to the key field in your database table.
$this->_init('createtable/myinfo', 'my_id');
}
}

Step7:- Now create the Myinfo.php in app/code/local/Mywork/Createtable/Model/Mysql4/Myinfo/Collection.php


<?php
class Mywork_Createtable_Model_Mysql4_Myinfo_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('createtable/myinfo');
}
}

Step8:- Now create the mysql4-install-1.0.0.php in app/code/local/Mywork/Createtable/sql/createtable_setup/mysql4-install-1.0.0.php


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

$installer->run(”
CREATE TABLE IF NOT EXISTS {$this->getTable(‘myinfo’)} (
`my_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`my_name` varchar(500) NOT NULL,
`my_image` varchar(500) NOT NULL,
`about_me` text NOT NULL,
PRIMARY KEY (`my_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
“);
$installer->endSetup();


Step9:- Now create the createtable.xml in app/design/frontend/default/default/layout/createtable.xml


<?xml version="1.0"?>
<layout version="1.0">
<createtable_index_index>
<reference name="content">
<block type="createtable/createtable" name="createtable" template="createtable/createtable.phtml" />
</reference>
</createtable_index_index>
</layout>

Step10:- Now create the createtable.phtml in app/design/frontend/default/default/layout/createtable/createtable.phtml


<?php
$admin_userModel = Mage::getModel('createtable/myinfo');
print_r($admin_userModel);
?>

Note:- call the url
siteurl/createtable Output:- Mywork_Createtable_Model_Myinfo Object ( [_eventPrefix:protected] => core_abstract [_eventObject:protected] => object [_resourceName:protected] => createtable/myinfo [_resource:protected] => [_resourceCollectionName:protected] => createtable/myinfo_collection [_cacheTag:protected] => [_dataSaveAllowed:protected] => 1 [_isObjectNew:protected] => [_data:protected] => Array ( ) [_hasDataChanges:protected] => [_origData:protected] => [_idFieldName:protected] => [_isDeleted:protected] => [_oldFieldsMap:protected] => Array ( ) [_syncFieldsMap:protected] => Array ( ) )

Create helper for front-end module in magento1

Note:- you can download code from this rar_image
Note:-1) Firstly Disable the all cache from Cache Management tab in System tab.
2) select the default theme of the magento from the System->Design.

Step1:- Create Mywork_Usehelper.xml file in app/etc/modules/.
where Mywork is Namespace and Usehelper is Modulename.
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_Usehelper>
<active>true</active>
<codePool>local</codePool>
</Mywork_Usehelper>
</modules>
</config>
[/php]
Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/Usehelper/etc/config.xml
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_Usehelper>
<version>1.0.0</version>
</Mywork_Usehelper>
</modules>

<frontend>
<routers>
<usehelper>
<use>standard</use>
<args>
<module>Mywork_Usehelper</module>
<frontName>usehelper</frontName>
</args>
</usehelper>
</routers>
<layout>
<updates>
<usehelper>
<file>usehelper.xml</file>
</usehelper>
</updates>
</layout>
</frontend>

<global>

<blocks>
<usehelper>
<class>Mywork_Usehelper_Block</class>
</usehelper>
</blocks>

<helpers>
<usehelper>
<class>Mywork_Usehelper_Helper</class>
</usehelper>
</helpers>

</global>
</config>
[/php]
Note:- 1) In this file we created version of this module is 1.0.0
2) In the frontend tag we created routers tag. In routers tag we define the frontend name is usehelper,
and we access the extension value from siteurl/usehelper.

Step3:- Now we create the IndexController.php in app/code/local/Mywork/Usehelper/controllers/IndexController.php
[php]
<?php
class Mywork_Usehelper_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
}
[/php]

Step4:- Now create the Useblock.php in app/code/local/Mywork/Usehelper/Block/Usehelper.php
[php]
<?php
class Mywork_Usehelper_Block_Usehelper extends Mage_Core_Block_Template
{
public function _prepareLayout()
{

return parent::_prepareLayout();
}

public function Addition()
{
$helper=Mage::helper("usehelper/data");
$total=$helper->getAddition(100,200);
return $total;
}
}
[/php]

Step5:- Now create the Data.php in app/code/local/Mywork/Usehelper/Helper/Data.php
[php]
<?php
class Mywork_Usehelper_Helper_Data extends Mage_Core_Helper_Abstract
{
public function getMsg() {
return ‘Hello friends?’;
}

public function getAddition($par1,$par2) {
return $par1+$par2;
}

}
[/php]

Step6:- Now create the usehelper.xml in app/design/frontend/default/default/layout/usehelper.xml
[php]
<?xml version="1.0"?>
<layout version="0.1.0">
<usehelper_index_index>
<reference name="content">
<block type="usehelper/usehelper" name="usehelper" template="usehelper/usehelper.phtml" />
</reference>
</usehelper_index_index>
</layout>
[/php]

Step7:- Now create the usehelper.phtml in app/design/frontend/default/default/template/usehelper/usehelper.phtml
[php]
<?php
/********************Addition Two numbers********************/
echo ‘Total number is ‘.$this->Addition();
echo ‘<br/>’;
$my_helper=Mage::helper("usehelper/data");
echo ‘Message is ‘.$my_helper->getMsg();
?>
[/php]

Note:- call the url
siteurl/usehelper Output:-
Total number is 300
Message is Hello friends?

Create model for front-end module in magento1

Note:- you can download code from this rar_image
Note:-1) Firstly Disable all cache from the Cache Management tab in System tab.
2) select the default theme of the Magento from the System->Design.

Step1:- Create Mywork_Usemodel.xml file in app/etc/modules/.
where Mywork is Namespace and Usemodel is Modulename.


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

Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create our own extension then we use the local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/Usemodel/etc/config.xml


<?xml version="1.0"?>
<config>
<modules>
<Mywork_Usemodel>
<version>1.0.0</version>
</Mywork_Usemodel>
</modules>

<frontend>
<routers>
<usemodel>
<use>standard</use>
<args>
<module>Mywork_Usemodel</module>
<frontName>usemodel</frontName>
</args>
</usemodel>
</routers>

<layout>
<updates>
<usemodel>
<file>usemodel.xml</file>
</usemodel>
</updates>
</layout>
</frontend>

<global>
<blocks>
<usemodel>
<class>Mywork_Usemodel_Block</class>
</usemodel>
</blocks>

<models>
<usemodel>
<class>Mywork_Usemodel_Model</class>
<resourceModel>usemodel_mysql4</resourceModel>
</usemodel>
<usemodel_mysql4>
<class>Mywork_Usemodel_Model_Mysql4</class>
<entities>
<getadmin>
<table>admin_user</table>
</getadmin>
</entities>
</usemodel_mysql4>
</models>
</global>
</config>

Note:- 1) In this file we created a version of this module is 1.0.0
2) In the frontend tag, we created routers tag. In routers tag, we define the frontend name is usemodel,
and we access the extension value from siteurl/usemodel.

Step3:- Now we create the IndexController.php in app/code/local/Mywork/Usemodel/controllers/IndexController.php


<?php
class Mywork_Usemodel_IndexController extends Mage_Core_Controller_Front_Action {

public function indexAction() {

$this->loadlayout();
$this->renderlayout();
}

}
?>

Step4:- Now create the Usemodel.php in app/code/local/Mywork/Useblock/Block/Usemodel.php


<?php
class Mywork_Usemodel_Block_Usemodel extends Mage_Core_Block_Template
{

public function _prepareLayout()
{

return parent::_prepareLayout();
}

}

Step5:- Now create the Getadmin.php in app/code/local/Mywork/Usemodel/Model/Getadmin.php


<?php

class Mywork_Usemodel_Model_Getadmin extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init(‘usemodel/getadmin’);
}
}

Step6:- Now create the Getadmin.php in app/code/local/Mywork/Usemodel/Model/Mysql4/Getadmin.php


<?php

class Mywork_Usemodel_Model_Mysql4_Getadmin extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
// Note that the user_id refers to the key field in your database table.
$this->_init(‘usemodel/getadmin’, ‘user_id’);
}

}

Step7:- Now create the Collection.php in app/code/local/Mywork/Usemodel/Model/Mysql4/Getadmin/Collection.php


<?php

class Mywork_Usemodel_Model_Mysql4_Getadmin_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init(‘usemodel/getadmin’);
}
}

Step8:- Now create the usemodel.xml in app/design/frontend/default/default/layout/usemodel.xml


<?xml version="1.0"?>
<layout version="1.0">
<usemodel_index_index>
<reference name="content">
<block type="usemodel/usemodel" name="usemodel" template="usemodel/usemodel.phtml" />
</reference>
</usemodel_index_index>
</layout>

Step9:- Now create the usemodel.phtml in app/design/frontend/default/default/template/usemodel/usemodel.phtml


<?php
$admin_userModel = Mage::getModel('usemodel/getadmin')->load(1);
echo $admin_userModel->firstname;
echo $admin_userModel->lastname;
?>

Note:- call the url
siteurl/usemodel Output:- Johnkumar

Create block for front-end module in magento1

Note:- you can download code from this rar_image
Note -1) Firstly Disable all cache from the Cache Management tab in System tab.
2) select the default theme of the Magento from the System->Design.

Step1:- Create Mywork_Useblock.xml file in app/etc/modules/.
where Mywork is Namespace and Useblock is Modulename.


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

Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/Useblock/etc/config.xml


<?xml version="1.0"?>
<config>
<modules>
<Mywork_Useblock>
<version>1.0.0</version>
</Mywork_Useblock>
</modules>

<frontend>
<routers>
<useblock>
<use>standard</use>
<args>
<module>Mywork_Useblock</module>
<frontName>useblock</frontName>
</args>
</useblock>
</routers>

<layout>
<updates>
<useblock>
<file>useblock.xml</file>
</useblock>
</updates>
</layout>
</frontend>

<global>
<blocks>
<useblock>
<class>Mywork_Useblock_Block</class>
</useblock>
</blocks>
</global>
</config>

Note:- 1) In this file we created a version of this module is 1.0.0
2) In the frontend tag we created routers tag. In routers tag we define the frontend name is useblock,
and we access the extension value from siteurl/useblock.

Step3:- Now we create the IndexController.php in app/code/local/Mywork/Useblock/controllers/IndexController.php


<?php
class Mywork_Useblock_IndexController extends Mage_Core_Controller_Front_Action {

public function indexAction() {

$this->loadlayout();
$this->renderlayout();
}

}
?>

Step4:- Now create the Useblock.php in app/code/local/Mywork/Useblock/Block/Useblock.php


<?php
class Mywork_Useblock_Block_Useblock extends Mage_Core_Block_Template
{

public function _prepareLayout()
{

return parent::_prepareLayout();
}

public function get_myname()
{
return ‘John’;
}
}

Step5:- Now create the useblock.xml in app/design/frontend/default/default/layout/useblock.xml


<?xml version="1.0"?>
<layout version="1.0">
<useblock_index_index>
<reference name="content">
<block type="useblock/useblock" name="useblock" template="useblock/useblock.phtml" />
</reference>
</useblock_index_index>
</layout>

Step6:- Now create the useblock.phtml in app/design/frontend/default/default/template/useblock/useblock.phtml


<?php
$myname=$this->get_myname();
echo 'Hello friend'.$myname;
?>

Note:- call the url
siteurl/useblock Output:- Hello friendJohn

Create controller for frontend module in magento1

Note:- you can download code from this rar_image
Note -1) Firstly Disable all cache from the Cache Management tab in System tab.
2) select the default theme of the Magento from the System->Design.

Step1:- Create Mywork_Usecontroller.xml file in app/etc/modules/.
where Mywork is Namespace and Usecontroller is Modulename.


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

Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/Usecontroller/etc/config.xml


<?xml version="1.0"?>
<config>
<modules>
<Mywork_Usecontroller>
<version>1.0.0</version>
</Mywork_Usecontroller>
</modules>

<frontend>
<routers>
<usecontroller>
<use>standard</use>
<args>
<module>Mywork_Usecontroller</module>
<frontName>usecontroller</frontName>
</args>
</usecontroller>
</routers>
</frontend>
</config>

Note:- 1) In this file we created version of this module is 1.0.0
2) In the frontend tag we created routers tag. In routers tag we define the frontend name is usecontroller,
and we access the extension value from siteurl/usecontroller.

Step3:- Now we create the IndexController.php in app/code/local/Mywork/Usecontroller/controllers/IndexController.php


<?php
class Mywork_Usecontroller_IndexController extends Mage_Core_Controller_Front_Action {

public function indexAction() {

echo ‘hello index action call on index controller’;

}

public function myinfoAction() {

echo ‘hello myinfo action call on index controller’;

}
}
?>

Step4:- Now we create the GetvalueController.php in app/code/local/Mywork/Usecontroller/controllers/GetvalueController.php


<?php
class Mywork_Usecontroller_GetvalueController extends Mage_Core_Controller_Front_Action {

public function indexAction() {
echo ‘hello index action call on Getvalue Controller’;
}

public function myinfoAction() {
echo ‘hello myinfo action call on Getvalue Controller’;
}
}
?>

Note:- Call the url:-
1) siteurl/usecontroller/ Output:- hello index action call on index conroller
2) siteurl/usecontroller/index/myinfo Output:- hello myinfo action call on index conroller
3) siteurl/usecontroller/index/getvalue Output:- hello index action call on Getvalue Controller
4) siteurl/usecontroller/index/getvalue/myinfo Output:- hello myinfo action call on Getvalue Controller

Delete Simple product programmatically in Magento1

There are some steps to delete a simple product through code.
1) Firstly include the Mage.php file.
2) after that load product id into the product model.
3) call the delete method to delete the product.


require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
Mage::register("isSecureArea", true);
$productId=24; //product id
try{
Mage::getModel("catalog/product")->load($productId)->delete();
echo 'delete';
}catch(Exception $e){
Mage::log($e->getMessage());
}

Update simple product programmatically in magento1

There are some steps to update a simple product through code.
1) Firstly include the Mage.php file.
2) after that load product id into the product model.
3) now put the updated product value information like name, price, description, etc.


require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$productId=24; //product id
$product = Mage::getModel('catalog/product')->load($productId);
try{
$product->setName('Product test data') //product name
->setSku('product-test-data') //SKU
->setWeight(250.00)
->setStatus(1) //product status (1 - enabled, 2 - disabled)
->setPrice(300.00) //price
->setDescription('product test long description')
->setShortDescription('product test short description')
->setCategoryIds(array(2)); //assign product to categories
$product->save();
}catch(Exception $e){
Mage::log($e->getMessage());
}

Create Simple Product programmatically in Magento1

There are some steps to create a simple product through code.
1) Firstly include the Mage.php file.
2) after that create the product model.
3) after that set type id is simple.
4) define product information like name, status, price, etc.


require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$product = Mage::getModel('catalog/product');
try{
$product->setStoreId(1) //you can set data in store scope
->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
->setAttributeSetId(4) //ID of a attribute set named 'default'
->setTypeId('simple') //product type
->setName('Product test') //product name
->setSku('product-test') //SKU
->setWeight(100.00)
->setStatus(1) //product status (1 - enabled, 2 - disabled)
->setTaxClassId(0) //tax class (0 - none, 2 - taxable, 4 - shipping)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
->setPrice(200.00) //price

->setMetaTitle(‘product test meta title’)
->setMetaKeyword(‘product test meta keyword’)
->setMetaDescription(‘product test meta description’)

->setDescription(‘product test long description’)
->setShortDescription(‘product test short description’)

->setMediaGallery (array(‘images’=>array (), ‘values’=>array ())) //media gallery initialization
->addImageToMediaGallery(‘media/catalog/product/test-product.png’, array(‘image’,’thumbnail’,’small_image’), false, false) //assigning image, thumb and small image to media gallery

->setStockData(array(
‘use_config_manage_stock’ => 0, //’Use config settings’ checkbox
‘manage_stock’=>1, //manage stock
‘min_sale_qty’=>1, //Minimum Qty Allowed in Shopping Cart
‘max_sale_qty’=>2, //Maximum Qty Allowed in Shopping Cart
‘is_in_stock’ => 1, //Stock Availability
‘qty’ => 999 //qty
)
)

->setCategoryIds(array(2)); //assign product to categories
$product->save();
}catch(Exception $e){
Mage::log($e->getMessage());
}


Difference between Mage::getSingleton() and Mage::getModel()

Mage::getSingleton():- It always check that Object is already created or not if already created then use this, otherwise create new object. It follows the Singleton design pattern.
[php]
Mage::getSigleton(‘ModuleName/ModelName’);
[/php]

Mage::getModel():- It always create new Object. It follow Standard Factory pattern.
[php]
Mage::getModel(‘ModuleName/ModelName’);
[/php]

Difference between register, registry and unregister

Magento registry is used to share information anywhere in the Magento store. It works like a Global variable. there are 3 static methods (i) register (ii) registry (iii) unregister of the Mage class.

(i) Mage::register:- It is used to register the value in the registry.
[php]
Mage::register(‘name’, $value);
[/php]
(ii) Mage::registry:- (ii) It is used to get the value from the registry.
[php]
Mage::registry(‘name’);
[/php]

(iii) Mage::unregister:- (ii) this method is used to remove it from the registry.
[php]
Mage::unregister(‘name’);
[/php]

What is codePool

codePool is a tag which you have to specify when registering new module in app/etc/modules/Namespace_Modulename.xml
[php]<codePool>local or community</codePool>[/php]

There are 3 codePool in the Magento.
(i) core (ii) community (iii) local

both 3 codePool in the app/code folder.
(i) core:- this is used by magento core team.

(ii) community:- this is used by third party development.

(ii) local:- this is used by own module development and overriding of core and community modules according to client requirements.

Difference between EAV and Flat model

EAV model:- EAV(Entity Attribute Value) is a data model. When we want to add a new field in the table then we do not need to modify the table structure.

Note:- EAV structure for product in magento, product ID is stored in catalog_product_entity_int table, product name in catalog_product_entity_varchar, product price in catalog_product_entity_decimal, product created date in catalog_product_entity_datetime and product description in catalog_product_entity_text table.

Example:- If we want to add a field name is productinfo then we add into catalog_product_entity_varchar.

Difference between EAV model and Flat model
(i) EAV model is very useful when we want to add a field in a table structure, not need to modify the structure. In the Data model, when we add a new field then modify the table structure.

(ii) EAV model is very complex for (select, insert and update query) because we use relation from many tables. The data model is very easy to (select, insert and update query).

(iii) EAV model is slow and the Data model is fast than the EAV model.

What is Design Pattern

A design pattern is a software engineering part. Magento uses many types of design patterns.
(i) MVC Pattern:- MVC stand for Model View Controller.

Model:- is used for database logic.

View:- is used for presentation logic.

Controller:- is used for routing.

(ii) Front Controller Pattern:- It has a single entry point (index.php) for all of it’s requests.

(iii) Singleton Pattern:-It can be only one instance of a given class. It checks instance is already created or not if created then use this otherwise create a new instance.
Example:-
<pre><code class=”language-php”>
Mage::getSingleton(‘catalog/session’);
</code></pre>
(iii) Factory Pattern:-The Factory Method is used to instantiate classes in Magento.
Example:-


Mage::getModel('catalog/product');

(iv) Registry Pattern:-It is used for transferring data between scopes when they cannot be passed on. It is a global scoped container for storing data. All the singletons are stored in the internal registry.
Example:-


Mage::register($key, $value);

It is used for storing value.
Example:-


Mage::registery($key);

It is used for getting the store value.
Example:-


Mage::unregister($key);

It is used for unregister the variable which is in the registry.

Get Base Url , Skin Url , Media Url , Js Url , Store Url and Current Url in phtml file and cms page

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'}}

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();

How to optimize Magento website

Magento has a heavy structure so need to optimize so that website will run smoothly.

There are many steps to optimize the site in Magento

(1) Enable Magento caching(after admin login system/Cache Management).

(2) Merge Javascript file and Merge CSS file(after admin login system/ADVANCED/Developer).

(3) Compress images.

(4) Disable unneeded Magento modules(after admin login system/ADVANCED/Advanced).

(5) MySQL table optimization.

(6) Use Nginx or Litespeed webserver, not Apache.

(7) Use the Full Page Cache.

(8) Use a Dedicated Server.

(9) Use a Content Delivery Network (CDN) for parallel transfer of static content.

(10) Don’t use too many different external sources (for images, iframes, (twitter/Facebook)feeds, etc.)

Event Observer

Event:- An Event is an action that takes place during normal program execution ex:- sales_order_place_before, sales_order_place_after etc.

Observer:- Observer is the event handler.

“Customizing Magento code using Event observer allows us to add your own logic, without changing any core code file.”

Now we create the extension AdminSessionUserLoginFailed and we will use event and observer in this extension.

“This extension is used for login failed record save in the login failed table”.

Firstly create the table loginfailed in the magento site database


CREATE TABLE IF NOT EXISTS `loginfailed` (
`id` int(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
`user_name` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Step1:- Firstly create the Mywork_AdminSessionUserLoginFailed.xml file which is in the app/etc/modules/


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

Note:- Where “Mywork” is a namespace and “AdminSessionUserLoginFailed” is a module name.

Step2:- Now create the config.xml file which is in the app/code/local/Mywork/AdminSessionUserLoginFailed/etc/

<?xml version=”1.0″?> <config> <modules> <Mywork_AdminSessionUserLoginFailed> <version>1.0.0</version> </Mywork_AdminSessionUserLoginFailed> </modules>

<global>
<events>
<admin_session_user_login_failed>
<observers>
<mywork_adminsessionuserloginfailed_observer>
<type>singleton</type>
<class>Mywork_AdminSessionUserLoginFailed_Model_Observer</class>
<method>Loginfailed</method>
</mywork_adminsessionuserloginfailed_observer>
</observers>
</admin_session_user_login_failed>
</events>
</global>
</config>

Note:-
(1) :- This is the name of the event for which you want to create observer.

(2) :- It denotes that you are creating the observer for the event.

(3) :- This is name of observer. It could be anything. Maybe you can write your name. But make sure that this is unique and has not been used in any other observer. Either of core, community, or local module. So the best practice is to prefix namespace and module name.

(4) :- This tag tell type of method.

(5) :- This tag contains name of the class in which observer event handler function specified. So that whenever the event will be dispatched observer will load this class.

(6) :- This tag contains name of the method which will be executed of event firing or dispatch.

Step3:- Now create the Observer.php file which is in the app/code/local/Mywork/AdminSessionUserLoginFailed/Model/


<?php
class Mywork_AdminSessionUserLoginFailed_Model_Observer
{
public function Loginfailed(Varien_Event_Observer $observer)
{
//$observer contains the object returns in the event.
$event = $observer->getEvent();
$user_name = $event->getUserName();

$write_connection = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
$fields_arr = array();
$fields_arr[‘user_name’] = $user_name;
$write_connection->insert(‘loginfailed’, $fields_arr);
return $this;
}
}
?>

when we put the wrong username or password then username save into login failed.
login_failed

Now the wrong username saves into a login failed table.
login_failed_saveindb

How many type of products

there are six type of products in magento.
(1) Simple Product:- A Magento simple product should be used for a single item without any specific selectable variations like table, chair etc.

(2) Configurable Product:- A Magento configurable product should be used for a single item with specific selectable variations like white t-shirt, blue t-shirt etc.
configurable_product

(3) Grouped Product:- A grouped product is just a “group” of simple products like combination of many bottle etc.
group_product

(4) Bundled Product:- A Magento bundle product should be used for a bundle of simple (or virtual) products which are not to be sold separately like group of chair and table.
bundled_product

(5) Virtual Product:- A Magento virtual product should be used for a virtual (not touchable) item. like a insurance, a reservation, an extra product guarantee or whatever. A virtual product does not allow selecting a shipping method at checkout simply because there’s nothing to ship.
virtual_product

(6) Downloadable Product:- A Magento downloadable product should be used for online software items like a pdf, MP3 file etc.
downloadable_product

What is ORM

(1) What is ORM?
ORM stands for Object Relational Mapping. It’s a programming technique used to convert different types of data to Objects and vice versa in OOP.
There are 2 types of ORM:
(1) Convert different types of data to objects.
(2) Convert objects to various types of data.

(2) ORM in Magento?
In Magento, ORM is displayed as Models in Magento design pattern MVC. Most of the models are inherited from the Varien_Object class, along with using PHP magic _get and _set functions, to retrieve and set the data of the object:
ex:- $product = Mage::getModel(‘catalog/product’)->load($product_id)->setPrice(200);
echo $product->getPrice();

Add attribute in the product grid

How to add Product’s short description in the Product Grid

Note:- you can download extension from this rar_imageAdd attribute in the product grid in magento through custom module.
Note:- Firstly disable the cache from Cache Management tab which is in System tab.
there are many steps to create the extension.
Step1:- Create Mywork_AddAttributeOnProductGrid.xml file in app/etc/modules/.
where Mywork is Namespace and AddAttributeOnProductGrid is Modulename.


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

Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/AddAttributeOnProductGrid/etc/config.xml


<config>
<modules>
<Mywork_AddAttributeOnProductGrid>
<version>1.0.0</version>
</Mywork_AddAttributeOnProductGrid>
</modules>

<global>
<blocks>
<adminhtml>
<rewrite>
<catalog_product_grid>Mywork_AddAttributeOnProductGrid_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
</rewrite>
</adminhtml>
</blocks>
<helpers>
<AddAttributeOnProductGrid>
<class>Mywork_AddAttributeOnProductGrid_Helper</class>
</AddAttributeOnProductGrid>
</helpers>
</global>
</config>

Note:- In this file we created version of this module is 1.0.0

Step3:- Now create the Grid.php file in app/code/local/Mywork/AddAttributeOnProductGrid/Block/Adminhtml/Catalog/Product/

In this file we create class name Mywork_AddAttributeOnProductGrid_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid class. which has two functions
(1) setCollection()
(2) _prepareColumns()


<?php
class Mywork_AddAttributeOnProductGrid_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
public function setCollection($collection) {
$collection->addAttributeToSelect('short_description');
$this->_collection = $collection;
}

protected function _prepareColumns()
{
$this->addColumnAfter(‘short_description’,
array(
‘header’=> Mage::helper(‘catalog’)->__(‘Short Description’),
‘width’ => ’60px’,
‘index’ => ‘short_description’,
),’sku’);

return parent::_prepareColumns();
}
}
?>

Step3:- Now create the Data.php file which is in the app/code/local/Mywork/AddAttributeOnProductGrid/Helper/Data.php


<?php
class Mywork_AddAttributeOnProductGrid_Helper_Data extends Mage_Core_Helper_Abstract
{
}
?>

Update order grid

How to add number of product’s quantity of the order in Order grid

Note:- you can download extension from this rar_imageUpdate order grid in magento through custom module.
Note:- Firstly disable the cache from Cache Management tab which is in System tab.
there are many steps to create the extension.
Step1:- Create Mywork_UpdateOrderedGrid.xml file in app/etc/modules/.
where Mywork is Namespace and UpdateOrderedGrid is Modulename.


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

Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/UpdateOrderedGrid/etc/config.xml


<config>
<modules>
<Mywork_UpdateOrderedGrid>
<version>1.0.0</version>
</Mywork_UpdateOrderedGrid>
</modules>

<global>
<blocks>
<adminhtml>
<rewrite>
<sales_order_grid>Mywork_UpdateOrderedGrid_Block_Adminhtml_Sales_Order_Grid</sales_order_grid>
</rewrite>
</adminhtml>
</blocks>
<helpers>
<UpdateOrderedGrid>
<class>Mywork_UpdateOrderedGrid_Helper</class>
</UpdateOrderedGrid>
</helpers>
</global>
</config>

Note:- 1) In this file we created version of this module is 1.0.0
2) When we want to update the sales order grid then we use sales_order_grid tag which is in the rewrite tag.

Step:3:- Now we create the Grid.php which is in the app/code/local/Mywork/UpdateOrderedGrid/Block/Adminhtml/Sales/Order/


<?php
class Mywork_UpdateOrderedGrid_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{

public function setCollection($collection) {
$collection->join(‘sales/order_item’, ‘order_id=entity_id’, array(‘name’=>’name’, ‘sku’ =>’sku’, ‘qty_ordered’=>’qty_ordered’ ), null,’left’);
$this->_collection = $collection;
}

protected function _prepareColumns()
{
$this->addColumnAfter(‘qty_ordered’, array(
‘header’ => Mage::helper(‘sales’)->__(‘Items Ordered’),
‘index’ => ‘qty_ordered’,
),’shipping_name’);
return parent::_prepareColumns();
}
}
?>

Note:-In this file we create the class name Mywork_UpdateOrderedGrid_Block_Adminhtml_Sales_Order_Grid
this class extends Mage_Adminhtml_Block_Sales_Order_Grid class.

in the public function setCollection($collection) we add $collection->join(‘sales/order_item’, ‘order_id=entity_id’, array(‘name’=>’name’, ‘sku’ =>’sku’, ‘qty_ordered’=>’qty_ordered’ ), null,’left’);

in the protected function _prepareColumns() we create the
$this->addColumnAfter(‘qty_ordered’, array(
‘header’ => Mage::helper(‘sales’)->__(‘Items Ordered’),
‘index’ => ‘qty_ordered’,
),’shipping_name’);
return parent::_prepareColumns();
}

Step4:- Now we create the Data.php file in Helper folder


<?php
class Mywork_UpdateOrderedGrid_Helper_Data extends Mage_Core_Helper_Abstract
{
}
?>

Add custom category attribute

How to add custom category attribute in magento?

We have created two category attribute (i) Category Small Details1 (ii) Category Small Details2
(i) Category Small Details1 value must be required.
(i) Category Small Details2 value is not required.

Note:- you can download extension from this rar_image
Add custom category attribute in magento through custom module.
Note:- Firstly disable the cache from Cache Management tab which is in System tab.
there are many steps to create the extension.
Step1:- Create Mywork_AddCustomCategoryAttribute.xml file in app/etc/modules/.
where Mywork is Namespace and AddCustomCategoryAttribute is Modulename.
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_AddCustomCategoryAttribute>
<active>true</active>
<codePool>local</codePool>
<version>1.0.0</version>
</Mywork_AddCustomCategoryAttribute>
</modules>
</config>
[/php]

Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/AddCustomCategoryAttribute/etc/
[php]
<?xml version="1.0"?>
<config>
<modules>
<Mywork_AddCustomCategoryAttribute>
<version>1.0.0</version>
</Mywork_AddCustomCategoryAttribute>
</modules>

<global>
<helpers>
<AddCustomCategoryAttribute>
<class>Mywork_AddCustomCategoryAttribute_Helper</class>
</AddCustomCategoryAttribute>
</helpers>
<models>
<AddCustomCategoryAttribute>
<class>Mywork_AddCustomCategoryAttribute_Model</class>
<resourceModel>AddCustomCategoryAttribute_mysql4</resourceModel>
</AddCustomCategoryAttribute>
</models>
<resources>
<AddCustomCategoryAttribute_setup>
<setup>
<module>Mywork_AddCustomCategoryAttribute</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</AddCustomCategoryAttribute_setup>
<AddCustomCategoryAttribute_write>
<connection>
<use>core_write</use>
</connection>
</AddCustomCategoryAttribute_write>
<AddCustomCategoryAttribute_read>
<connection>
<use>core_read</use>
</connection>
</AddCustomCategoryAttribute_read>
</resources>
</global>
</config>
[/php]

Step3:- Now create the mysql4-install-1.0.0.php file in app/code/local/Mywork/AddCustomCategoryAttribute/sql/AddCustomCategoryAttribute_setup/
[php]
<?php
$installer = $this;
$installer->startSetup();

$installer->addAttribute("catalog_category", "catsmalldetails1", array(
"type" => "varchar",
"backend" => "",
"frontend" => "",
"label" => "Category Small Details1",
"input" => "text",
"class" => "",
"source" => "",
"global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
"visible" => true,
"required" => true,
"user_defined" => false,
"default" => "",
"searchable" => false,
"filterable" => false,
"comparable" => false,

"visible_on_front" => false,
"unique" => false,
"note" => ""

));

$installer->addAttribute("catalog_category", "catsmalldetails2", array(
"type" => "varchar",
"backend" => "",
"frontend" => "",
"label" => "Category Small Details2",
"input" => "text",
"class" => "",
"source" => "",
"global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
"visible" => true,
"required" => false,
"user_defined" => false,
"default" => "",
"searchable" => false,
"filterable" => false,
"comparable" => false,

"visible_on_front" => false,
"unique" => false,
"note" => ""

));

$installer->endSetup();
[/php]

Step4:- Now create the Data.php file in app/code/local/Mywork/AddCustomCategoryAttribute/Helper/
[php]
<?php
class Mywork_AddCustomCategoryAttribute_Helper_Data extends Mage_Core_Helper_Abstract
{
}
[/php]

After extension uploaded then you can see your custom category attribute on click the category.
AddCustomCategoryAttribute

Add custom product attribute

How to add custom product attribute in magento?

We have created product attribute (i) “Product Company Name” which is must be required.Note:- you can download extension from this rar_image
Add custom product attribute in magento through custom module.
Note:- Firstly disable the cache from Cache Management tab which is in System tab.
there are many steps to create the extension.
Step1:- Create Mywork_AddCustomProductAttribute.xml file in app/etc/modules/.
where Mywork is Namespace and AddCustomProductAttribute is Modulename.


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

Note:- 1) If you want to active the extensions then use true otherwise false.
2) there are three code pool (a) local (b) core (c) community
when we create the own extension then we use local code pool.

Step2:- Now create the config.xml file in app/code/local/Mywork/AddCustomProductAttribute/etc/


<?xml version="1.0"?>
<config>
<modules>
<Mywork_AddCustomProductAttribute>
<version>1.0.0</version>
</Mywork_AddCustomProductAttribute>
</modules>
<global>
<helpers>
<AddCustomProductAttribute>
<class>Mywork_AddCustomProductAttribute_Helper</class>
</AddCustomProductAttribute>
</helpers>
<models>
<AddCustomProductAttribute>
<class>Mywork_AddCustomProductAttribute_Model</class>
<resourceModel>AddCustomProductAttribute_mysql4</resourceModel>
</AddCustomProductAttribute>
</models>
<resources>
<AddCustomProductAttribute_setup>
<setup>
<module>Mywork_AddCustomProductAttribute</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</AddCustomProductAttribute_setup>
<AddCustomProductAttribute_write>
<connection>
<use>core_write</use>
</connection>
</AddCustomProductAttribute_write>
<AddCustomProductAttribute_read>
<connection>
<use>core_read</use>
</connection>
</AddCustomProductAttribute_read>
</resources>
</global>
</config>

Step3:- Now create the mysql4-install-1.0.0.php file in app/code/local/Mywork/AddCustomProductAttribute/sql/AddCustomProductAttribute_setup/


<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute("catalog_product", "product_companyname", array(
"type" => "varchar",
"backend" => "",
"frontend" => "",
"label" => "Product Company Name",
"input" => "text",
"class" => "",
"source" => "",
"global" => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
"visible" => true,
"required" => true,
"user_defined" => false,
"default" => "",
"searchable" => false,
"filterable" => false,
"comparable" => false,

“visible_on_front” => false,
“unique” => false,
“note” => “”
));
$installer->endSetup();

Step4:- Now create the Data.php file in app/code/local/Mywork/AddCustomProductAttribute/Helper/


<?php
class Mywork_AddCustomProductAttribute_Helper_Data extends Mage_Core_Helper_Abstract
{
}

After extension uploaded then you can see your custom product attribute on click the product.
AddCustomProductAttribute

Magento redirect url

_redirect()

This function is used for between Magento modules.


<?php $this->_redirect('module/controller/action');
example:- $this->_redirect('customer/account/create');
?>

We can redirect within our current controller by specifying ‘*’.


<?php $this->_redirect('*/*/add'); ?>

Note:- first * is used for module and second * is used for controller.

_redirectUrl()

This function is used for external URL or outside Magento base.


<?php $this->_redirectUrl('http://google.com'); ?>

What is Magento

Magento is an open-source e-commerce CMS which is built in PHP. It is most popular for the e-commerce website. It uses multiple other PHP frameworks such as ZEND, Laminas, and Symfony.

Magento is also specially designed for online sellers who need a simple e-commerce store. It is free to all so anyone can create their own website. Magento is easy to install, easy to customize, and easy to scale, and this makes it a popular choice for all sorts of companies, from small businesses to huge multinationals.

Magento was acquired by Adobe Inc. in May 2018 for USD 1.68 Billion.

Magento comes in two primary versions:

1. Magento Open Source (formerly Community Edition): A free version with essential features for building and managing an online store.

2. Magento Commerce (formerly Enterprise Edition): A paid, cloud-based version with advanced features like customer segmentation, personalized content, enhanced security, and dedicated support.

Magento is highly customizable, scalable, and widely used by businesses ranging from small startups to large enterprises due to its flexibility in managing complex e-commerce operations. It also supports integration with third-party services like payment gateways, shipping providers, and CRMs.