Step 1. Each Module should have one Namespace & Module name. Name space could be some company name etc. & module name should be something that represents module functionality. So let suppose Namespace is ExtendTree and Module name is RewriteClasses Now Create Global Configuration File inside root directory /app/etc/modules as name "ExtendTree_RewriteClasses.xml"
truelocal
Step 2. Now Create Module Configuration File inside the directory /app/code/local/ExtendTree/RewriteClasses /etc as name "config.xml"
Step 3. Now create the helper class. Suppose we want to rewrite class Mage_Adminhtml_Helper_Catalog_Product_Edit_Action_Attribute then we will create one helper class in our module in directory app/code/local/ExtendTree/RewriteClasses/Helper/Catalog/Product/Edit/Action/ by name Attribute.php
class ExtendTree_RewriteClasses_Helper_Catalog_Product_Edit_Action_Attribute extends Mage_Adminhtml_Helper_Catalog_Product_Edit_Action_Attribute {
/**
* Selected products for mass-update
*
* @var Mage_Catalog_Model_Entity_Product_Collection
*/
protected $_products;
/**
* Array of same attributes for selected products
*
* @var Mage_Eav_Model_Mysql4_Entity_Attribute_Collection
*/
protected $_attributes;
/**
* Excluded from batch update attribute codes
*
* @var array
*/
protected $_excludedAttributes = array('url_key');
/**
* Return product collection with selected product filter
* Product collection didn't load
*
* @return Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
*/
public function getProducts() {
if (is_null($this->_products)) {
$productsIds = $this->getProductIds();
if (!is_array($productsIds)) {
$productsIds = array(0);
}
$this->_products = Mage::getResourceModel('catalog/product_collection')
->setStoreId($this->getSelectedStoreId())
->addIdFilter($productsIds);
}
return $this->_products;
}
/**
* Return array of selected product ids from post or session
*
* @return array|null
*/
public function getProductIds() {
$session = Mage::getSingleton('adminhtml/session');
if ($this->_getRequest()->isPost() && $this->_getRequest()->getActionName() == 'edit') {
$session->setProductIds($this->_getRequest()->getParam('product', null));
}
return $session->getProductIds();
}
/**
* Return selected store id from request
*
* @return integer
*/
public function getSelectedStoreId() {
return (int) $this->_getRequest()->getParam('store', Mage_Core_Model_App::ADMIN_STORE_ID);
}
/**
* Return array of attribute sets by selected products
*
* @return array
*/
public function getProductsSetIds() {
return $this->getProducts()->getSetIds();
}
/**
* Return collection of same attributes for selected products without unique
*
* @return Mage_Eav_Model_Mysql4_Entity_Attribute_Collection
*/
public function getAttributes() {
if (is_null($this->_attributes)) {
$this->_attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)
->getAttributeCollection()
->addIsNotUniqueFilter()
->setInAllAttributeSetsFilter($this->getProductsSetIds());
if ($this->_excludedAttributes) {
$this->_attributes->addFieldToFilter('attribute_code', array('nin' => $this->_excludedAttributes));
}
// check product type apply to limitation and remove attributes that impossible to change in mass-update
$productTypeIds = $this->getProducts()->getProductTypeIds();
foreach ($this->_attributes as $attribute) {
/* @var $attribute Mage_Catalog_Model_Entity_Attribute */
foreach ($productTypeIds as $productTypeId) {
$applyTo = $attribute->getApplyTo();
if (count($applyTo) > 0 && !in_array($productTypeId, $applyTo)) {
$this->_attributes->removeItemByKey($attribute->getId());
break;
}
}
}
}
return $this->_attributes;
}
/**
* Return product ids that not available for selected store
*
* @deprecated since 1.4.1
* @return array
*/
public function getProductsNotInStoreIds() {
return array();
}
}
Step 4. Now create the model class. Suppose we want to rewrite class Mage_Adminhtml_Model_Customer_Renderer_Region then we will create one model class in our module in directory app/code/local/ExtendTree/RewriteClasses/Model/Customer/Renderer by name Region.php
class ExtendTree_RewriteClasses_Model_Customer_Renderer_Region extends Mage_Adminhtml_Model_Customer_Renderer_Region {
/**
* Country region collections
*
* array(
* [$countryId] => Varien_Data_Collection_Db
* )
*
* @var array
*/
static protected $_regionCollections;
public function render(Varien_Data_Form_Element_Abstract $element) {
$html = '
' . "\n";
$countryId = false;
if ($country = $element->getForm()->getElement('country_id')) {
$countryId = $country->getValue();
}
$regionCollection = false;
if ($countryId) {
if (!isset(self::$_regionCollections[$countryId])) {
self::$_regionCollections[$countryId] = Mage::getModel('directory/country')
->setId($countryId)
->getLoadedRegionCollection()
->toOptionArray();
}
$regionCollection = self::$_regionCollections[$countryId];
}
$regionId = intval($element->getForm()->getElement('region_id')->getValue());
$htmlAttributes = $element->getHtmlAttributes();
foreach ($htmlAttributes as $key => $attribute) {
if ('type' === $attribute) {
unset($htmlAttributes[$key]);
break;
}
}
// Output two elements - for 'region' and for 'region_id'.
// Two elements are needed later upon form post - to properly set data to address model,
// otherwise old value can be left in region_id attribute and saved to DB.
// Depending on country selected either 'region' (input text) or 'region_id' (selectbox) is visible to user
$regionHtmlName = $element->getName();
$regionIdHtmlName = str_replace('region', 'region_id', $regionHtmlName);
$regionHtmlId = $element->getHtmlId();
$regionIdHtmlId = str_replace('region', 'region_id', $regionHtmlId);
if ($regionCollection && count($regionCollection) > 0) {
$elementClass = $element->getClass();
$html.= '
Step 5. Now create the Block class. Suppose we want to rewrite class Mage_Adminhtml_Block_Sales_Order_Grid then we will create one block class in our module in directory app/code/local/ExtendTree/RewriteClasses/Block/Sales/Order by name Grid.php
No comments :
Post a Comment