从客户组保存自定义文本框值

时间:2016-03-28 20:58:50

标签: php magento-1.9

我已使用自定义扩展程序在客户组编辑表单中添加了自定义文本框。 我有覆盖Block_Adminhtml_Customer_Group_Edit_Form块和控制器。 这是我的模块的config.xml。

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Customer>
            <version>1.0.1</version>
        </Namespace_Customer>
    </modules>
    <frontend>
        <routers>
            <customer>
                <use>standard</use>
                <args>
                    <module>Namespace_Customer</module>
                    <frontName>customer</frontName>
                </args>
            </customer>
        </routers>

    </frontend>
   <admin>
    <routers>
      <customer>
        <use>admin</use>
        <args>
          <module>Namespace_Customer</module>
          <frontName>customer</frontName>
        </args>
      </customer>
      <adminhtml>
        <args>
          <modules>
            <Namespace_Customer before="Mage_Adminhtml">Namespace_Customer_Adminhtml</Namespace_Customer>
          </modules>
        </args>
      </adminhtml>
    </routers>
  </admin>
  <adminhtml>
    </adminhtml>
     <global>
        <blocks>
            <Namespace_Customer>
                <class>Namespace_Customer_Block</class>
            </Namespace_Customer>
            <adminhtml>
                <rewrite>
                <customer_group_edit_form>Namespace_Customer_Block_Adminhtml_Customer_Group_Edit_Form</customer_group_edit_form>
                </rewrite>
            </adminhtml>
        </blocks>
         <helpers>
            <customer>
                <class>Namespace_Customer_Helper</class>
            </customer>
        </helpers>
    </global>
</config>

这是我的表格代码。

class Namespace_Customer_Block_Adminhtml_Customer_Group_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    /**
     * Prepare form for render
     */
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        $form = new Varien_Data_Form();
        $customerGroup = Mage::registry('current_group');

        $fieldset = $form->addFieldset('base_fieldset', array('legend'=>Mage::helper('customer')->__('Group Information')));

        $validateClass = sprintf('required-entry validate-length maximum-length-%d',
            Mage_Customer_Model_Group::GROUP_CODE_MAX_LENGTH);
        $name = $fieldset->addField('customer_group_code', 'text',
            array(
                'name'  => 'code',
                'label' => Mage::helper('customer')->__('Group Name'),
                'title' => Mage::helper('customer')->__('Group Name'),
                'note'  => Mage::helper('customer')->__('Maximum length must be less then %s symbols', Mage_Customer_Model_Group::GROUP_CODE_MAX_LENGTH),
                'class' => $validateClass,
                'required' => true,
            )
        );

        if ($customerGroup->getId()==0 && $customerGroup->getCustomerGroupCode() ) {
            $name->setDisabled(true);
        }

        $fieldset->addField('tax_class_id', 'select',
            array(
                'name'  => 'tax_class',
                'label' => Mage::helper('customer')->__('Tax Class'),
                'title' => Mage::helper('customer')->__('Tax Class'),
                'class' => 'required-entry',
                'required' => true,
                'values' => Mage::getSingleton('tax/class_source_customer')->toOptionArray()
            )
        );

        $fieldset->addField('price_margin', 'text',
      array(
        'name'     => 'price_margin',
        'label'    => Mage::helper('customer')->__('Price Margin'),
        'title'    => Mage::helper('customer')->__('Price Margin'),
        'note'     => Mage::helper('customer')->__('Enter % price margin: >=0%'),
        'class'    => 'validate-number validate-zero-or-greater validate-number-range number-range-0-1000000000', //delete this if you dont want this attribute to be required
        'required' => FALSE,
      )
    );


        if (!is_null($customerGroup->getId())) {
            // If edit add id
            $form->addField('id', 'hidden',
                array(
                    'name'  => 'id',
                    'value' => $customerGroup->getId(),
                )
            );
        }

        if( Mage::getSingleton('adminhtml/session')->getCustomerGroupData() ) {
            $form->addValues(Mage::getSingleton('adminhtml/session')->getCustomerGroupData());
            Mage::getSingleton('adminhtml/session')->setCustomerGroupData(null);
        } else {
            $form->addValues($customerGroup->getData());
        }

        $form->setUseContainer(true);
        $form->setId('edit_form');
        $form->setAction($this->getUrl('*/*/save'));
        $this->setForm($form);
    }
}

这是我的控制者。

require_once 'Mage/Adminhtml/controllers/Customer/GroupController.php';

class Namespace_Customer_Adminhtml_Customer_GroupController extends Mage_Adminhtml_Controller_Action
{
 public function saveAction() {
    $customerGroup = Mage::getModel('customer/group');
    $id = $this->getRequest()->getParam('id');
    if (!is_null($id)) {
      $customerGroup->load((int)$id);
    }
    $taxClass = (int)$this->getRequest()->getParam('tax_class');
    $priceMargin = $this->getRequest()->getParam('price_margin');

    if ($taxClass) {
      try {
        $customerGroupCode = (string)$this->getRequest()->getParam('code');
        if (!empty($customerGroupCode)) {
          $customerGroup->setCode($customerGroupCode);
        }

        // set price margin
        if (is_numeric($priceMargin)) {
          $customerGroup->setPriceMargin(number_format($priceMargin, 2));
        } else {
          $customerGroup->setPriceMargin('');
        }
        $customerGroup->setTaxClassId($taxClass)->save();
        Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('customer')->__('The customer group has been saved.'));
        $this->getResponse()->setRedirect($this->getUrl('*/customer_group'));
        return;
      } catch (Exception $e) {
        Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        Mage::getSingleton('adminhtml/session')->setCustomerGroupData($customerGroup->getData());
        $this->getResponse()->setRedirect($this->getUrl('*/customer_group/edit', array('id' => $id)));
        return;
      }
    } else {
      $this->_forward('new');
    }
  }
}

我在控制器中获得了价值,但是当我保存价值控制器并没有保存这个价值时,因为当我打开客户群表时,它并没有显示我的价值。 请帮忙

0 个答案:

没有答案