Adds a Region Selector to custom Magento entity types that have standard address attributes.
Last active
July 22, 2016 20:57
-
-
Save dfelton/d542bfbb63f28ced8789aeed8d81f45c to your computer and use it in GitHub Desktop.
Magento - Add region selector to custom entities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // File: app/design/adminhtml/default/default/template/moduleName/entityName/edit.phtml | |
| // Variables: | |
| // - moduleName | |
| // - entityName | |
| /** | |
| * Adds region updater functionality to add/edit entityName form. | |
| * | |
| * @category design | |
| * @package default_default | |
| * @author ${user} | |
| * @version ${date} | |
| */ | |
| /* @var $this Mage_Core_Block_Template */ | |
| $entity = Mage::registry('current_entityName'); | |
| ?> | |
| <script> | |
| //<![CDATA[ | |
| jQuery(function(){ | |
| regionIdParent = jQuery('#entityName_region_id').parent().parent(); | |
| regionTextParent = jQuery('#entityName_region').parent(); | |
| jQuery('#entityName_region_id').appendTo(regionTextParent); | |
| regionIdParent.remove(); | |
| }); | |
| $('entityName_region_id').setAttribute('defaultValue', "<?php echo $entity->getRegionId() ?>"); | |
| new RegionUpdater('entityName_country_id', 'entityName_region', 'entityName_region_id', <?php echo $this->helper('directory')->getRegionJson() ?>, undefined, 'entityName_postal_code'); | |
| //]]> | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Required Entity Attributes: | |
| // region - text | |
| // region_id - dropdown (empty values) | |
| // postal_code - text | |
| // country_id - country dropdown | |
| // Variables: | |
| // ModuleName | |
| // EntityName | |
| // app/code/codePool/PackageName/ModuleName/Model/EntityName.php | |
| ## | |
| # STEP 1: Copy the following logic into the model class | |
| ## | |
| /** | |
| * Directory region models | |
| * | |
| * @var array | |
| */ | |
| static protected $_regionModels = array(); | |
| /** | |
| * Retrieve region name | |
| * | |
| * @return string | |
| */ | |
| public function getRegion() | |
| { | |
| $regionId = $this->getData('region_id'); | |
| $region = $this->getData('region'); | |
| if ($regionId) { | |
| if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) { | |
| $region = $this->getRegionModel($regionId)->getName(); | |
| $this->setData('region', $region); | |
| } | |
| } | |
| if (!empty($region) && is_string($region)) { | |
| $this->setData('region', $region); | |
| } | |
| elseif (!$regionId && is_numeric($region)) { | |
| if ($this->getRegionModel($region)->getCountryId() == $this->getCountryId()) { | |
| $this->setData('region', $this->getRegionModel($region)->getName()); | |
| $this->setData('region_id', $region); | |
| } | |
| } | |
| elseif ($regionId && !$region) { | |
| if ($this->getRegionModel($regionId)->getCountryId() == $this->getCountryId()) { | |
| $this->setData('region', $this->getRegionModel($regionId)->getName()); | |
| } | |
| } | |
| return $this->getData('region'); | |
| } | |
| /** | |
| * Retrieve country model | |
| * | |
| * @return Mage_Directory_Model_Country | |
| */ | |
| public function getRegionModel($region=null) | |
| { | |
| if(is_null($region)) { | |
| $region = $this->getRegionId(); | |
| } | |
| if(!isset(self::$_regionModels[$region])) { | |
| self::$_regionModels[$region] = Mage::getModel('directory/region')->load($region); | |
| } | |
| return self::$_regionModels[$region]; | |
| } | |
| ## | |
| # STEP 2: Add "$this->getRegion();" into your model's _beforeSave() method. | |
| # | |
| # If this method does not exist, copy/paste the following: | |
| ## | |
| protected function _beforeSave() | |
| { | |
| parent::_beforeSave(); | |
| $this->getRegion(); | |
| return $this; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0"?> | |
| <!-- | |
| File: app/design/adminhtml/default/default/layout/moduleName.xml (Filename will vary) | |
| Instructions: Add the Following into the XML File. Find/Replace the variables | |
| Variables: | |
| - moduleName | |
| - entityName | |
| --> | |
| <layout> | |
| <adminhtml_moduleName_entityName_edit> | |
| <reference name="head"> | |
| <action method="addItem"><type>js</type><file>lib/jquery/jquery-1.10.2.min.js</file></action> | |
| <action method="addItem"><type>js</type><file>lib/jquery/noconflict.js</file></action> | |
| </reference> | |
| <reference name="content"> | |
| <block type="core/template" name="moduleName_entityName_edit" template="moduleName/entityName/edit.phtml" /> | |
| </reference> | |
| </adminhtml_moduleName_entityName_edit> | |
| </layout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment