Add a Dropdown Field In Admin Section

Create the configuration field

Without much effort, you can create your own fields. All you need is:

    >>your own module
    >>a system.xml file inside this module

I assume that you are familiar with creating your own module and new (text) fields in the system.xml.

It will look like this when it’s finished:














In Your Custom module system.xml file
Add this line

    <shipping_costs translate="label">
        <label>Zipcode Range,Weight with Price</label>
        <frontend_model>dao_customshippingmethod_block_linkedAttributes</frontend_model>
        <backend_model>adminhtml/system_config_backend_serialized_array</backend_model>
        <sort_order>22</sort_order>
        <show_in_default>1</show_in_default>
        <show_in_website>1</show_in_website>
        <show_in_store>1</show_in_store>
    </shipping_costs>


As you can see, the code only adds a frontend model and a backend model to the usual definition of new fields.

  • The backend model is used to modify saving the field value to and loading from the database. Always use adminhtml/system_config_backend_serialized_array here.
  • The frontend model is in fact a block, despite its name. It is used to render the table with the defined columns. The frontend models always inherits from the class Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract.



In Your Custom module config.xml file
Add this line

    <blocks>
      <dao_customshippingmethod>
        <class>dao_customshippingmethod_Blocks</class>
      </dao_customshippingmethod>
    </blocks>

Here I have Created my Module name as dao_customshippingmethod



In my example the frontend model class looks like this:
Place this code in your module File:
Block
    LinkedAttributes.php






<?php
class Dao_Customshippingmethod_Block_LinkedAttributes extends Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract
{
    protected $_itemRenderer;

    public function _prepareToRender()
    {
        $this->addColumn('zip_from', array(
            'label' => Mage::helper('dao_customshippingmethod')->__('Zip From'),
            'style' => 'width:100px',
        ));
        $this->addColumn('zip_to', array(
            'label' => Mage::helper('dao_customshippingmethod')->__('Zip To'),
            'style' => 'width:100px',
        ));
        $this->addColumn('weight_from', array(
            'label' => Mage::helper('dao_customshippingmethod')->__('Weight From'),
            'style' => 'width:100px',
        ));
        $this->addColumn('weight_to', array(
            'label' => Mage::helper('dao_customshippingmethod')->__('Weight To'),
            'style' => 'width:100px',
        ));
        $this->addColumn('cost', array(
            'label' => Mage::helper('dao_customshippingmethod')->__('Shipping Cost'),
            'style' => 'width:100px',
        ));

        $this->_addAfter = false;
        $this->_addButtonLabel = Mage::helper('dao_customshippingmethod')->__('Add');
    }
?>


I have created One shipping module,It will make it clear about getting the values

<?php
// app/code/local/Dao/Customshippingmethod/Model
class Dao_Customshippingmethod_Model_Demo
extends Mage_Shipping_Model_Carrier_Abstract
implements Mage_Shipping_Model_Carrier_Interface
{
  protected $_code = 'dao_customshippingmethod';

  public function collectRates(Mage_Shipping_Model_Rate_Request $request)
  {
    $result = Mage::getModel('shipping/rate_result');
    $result->append($this->_getDefaultRate());

    return $result;
  }

  public function getAllowedMethods()
  {
    return array(
      'dao_customshippingmethod' => $this->getConfigData('name'),
    );
  }

  protected function _getDefaultRate()
  {
    $rate = Mage::getModel('shipping/rate_result_method'); 
    $getCurrentPostCode = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getPostcode();
    $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();
    $weight = 0;
    foreach($items as $item)
    {
        $weight += ($item->getWeight() * $item->getQty()) ;
    }
    $rawData = Mage::getStoreConfig('carriers/dao_customshippingmethod/shipping_costs');
    $unserializeData=unserialize($rawData);
    foreach($unserializeData as $unserializeData01)
    {
    $hb[]=$unserializeData01;
    }
    $zipcode=$getCurrentPostCode;
    //$zipcode=7100;
    foreach($hb as $cc)
    {
    $zipto=    $cc['zip_to'];
    $zipfrom=    $cc['zip_from'];
    $weightto=    $cc['weight_to'];
    $weightfrom=    $cc['weight_from'];
    $ziprange = range($zipto, $zipfrom);
    $weightrange = range($weightto, $weightfrom);
        if(in_array($zipcode, $ziprange) && in_array($weight, $weightrange))
        {
            $rate->setPrice($cc['cost']);
            $rate->setCarrier($this->_code);
            $rate->setCarrierTitle($this->getConfigData('title'));
            $rate->setMethod($this->_code);
            $rate->setMethodTitle($this->getConfigData('name'));
            $rate->setShopid($this->getConfigData('shopid'));
            $rate->setTransittime($this->getConfigData('transittime'));
            $rate->setZipfrom($this->getConfigData('zipfrom'));
            $rate->setZipto($this->getConfigData('zipto'));
            $rate->setWeightfrom($this->getConfigData('weightfrom'));
            $rate->setWeightto($this->getConfigData('weightto'));
            $rate->setApikey($this->getConfigData('apikey'));
            $rate->setLatitude($this->getConfigData('latitude'));
            $rate->setLongitude($this->getConfigData('longitude'));
            $rate->setCost(0);
        }
        else
        {
           
            $error = Mage::getModel('shipping/rate_result_error');
            $error->setCarrier($this->_code);
            $error->setCarrierTitle($this->getConfigData('name'));
            $error->setErrorMessage($this->getConfigData('name'));
           // $rate->append($error);
          
        }
    }
   
   
    
   
    //Mage::log($zipcode); 

    return $rate;
    //print_r($rate);
  }
 
}







Setting a default value

As for every other configuration field, you can set a default value in your config.xml file. You have to enter a serialized array there. In my experience, the best way to generate that serialized array is to fill in the default data in the backend, save it and then copy the value from the database to the config.xml. In my example it looks like this:


<dao_customshippingmethod>
        <settings>
            <shipping_costs>a:1:{s:18:"_1425462208957_957";a:2:{s:10:"from_price";s:1:"0";s:4:"cost";s:4:"4.90";}}</shipping_costs>
        </setting>
    </dao_customshippingmethod>
</default>











Comments

Popular Posts

Magmi Single and Multiple Category Importer