如何在购物车价格规则中添加自定义标签

时间:2015-04-20 11:04:21

标签: php magento

我正在尝试在magento后备促销/购物车价格规则中添加自定义标签。我需要的是当我选择任何规则时,自定义选项卡显示在“管理优惠券代码”下面,当我点击自定义选项卡时,我必须得到一个带有保存和保存并继续编辑按钮的网格表。有谁能建议我这样做的正确方法?

这是我要在“管理优惠券代码”下面添加新自定义标签的窗口: http://awesomescreenshot.com/0d34u825af

1 个答案:

答案 0 :(得分:2)

有关完整说明,请参阅此http://www.newgenray.com/2015/04/22/creating-custom-tab-on-edit-page-in-an-existing-module-of-magento/

首先,你需要创建一个模块,你必须定义两个东西,一个是布局和块

<config>
<modules>
    <Newgenray_Coupon>
        <version>0.0.1</version>
    </Newgenray_Coupon>
 </modules>
 <adminhtml>
    <layout>
        <updates>
            <newgenray_coupon>
                <file>coupon.xml</file>
            </newgenray_coupon>
        </updates>
    </layout>
</adminhtml>
<global>
    <blocks>
        <newgenray_coupon>
            <class>Newgenray_Coupon_Block</class>
        </newgenray_coupon>
    </blocks>
</global>

然后在文件app / code / local / Newgenray / Block / Adminhtml / Promo / Quote / Edit / Tab / Custom.php

class Newgenray_Coupon_Block_Adminhtml_Promo_Quote_Edit_Tab_Custom extends Mage_Adminhtml_Block_Widget_Form
implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
/*public function __construct(){
    $this->setTemplate('newgenray_coupon/custom_tab.phtml');
}*/

/**
 * Mandatory to override as we are implementing Widget Tab interface
 * Return Tab Title
 *
 * @return string
 */
public function getTabTitle(){
    return Mage::helper('salesrule')->__('Custom Tab');
}

/**
 * Mandatory to override as we are implementing Widget Tab interface
 * Return Tab Label
 *
 * @return string
 */
public function getTabLabel(){
    return Mage::helper('salesrule')->__('Custom Tab');
}

/**
 * Mandatory to override as we are implementing Widget Tab interface
 * Can show tab in tabs
 * Here you can write condition when the tab should we shown or not. Like you see when we create shopping cart rule
 * Manage coupon tab doesn't come. If you want that then just make a function and check whether you have information
 * in registry or not
 *
 * @return boolean
 */
public function canShowTab(){
    return true;
}

/**
 * Mandatory to override as we are implementing Widget Tab interface
 * Tab is Hidden
 *
 * @return boolean
 */
public function isHidden(){
    return false;
}

/**
 * Defines after which tab this tab should come like you asked you need it below Manage Coupon codes
 *
 * @return string
 */
public function getAfter(){
    return 'coupons_section';
}

public function _prepareForm(){
     /* To set the data in the form you need to get the data in registry In my example
     * I don't have any registry so I am commenting it. My Form will be blank
     */
    //$model = Mage::registry('custom_tab_form_data');

    $form = new Varien_Data_Form();

    $form->setHtmlIdPrefix('rule_');

    $fieldset = $form->addFieldset('custom_fieldset', array(
        'legend'=>Mage::helper('salesrule')->__('Your Custom Field Set ')
    ));

    $fieldset->addField('name', 'text', array(
        'label'     => Mage::helper('salesrule')->__('Name'),
        'class'     => 'required-entry',
        'required'  => true,
        'name'      => 'name',
        'note'     => Mage::helper('salesrule')->__('The name of the example.'),
    ));

    $fieldset->addField('description', 'text', array(
        'label'     => Mage::helper('salesrule')->__('Description'),
        'class'     => 'required-entry',
        'required'  => true,
        'name'      => 'description',
    ));

    $fieldset->addField('other', 'text', array(
        'label'     => Mage::helper('salesrule')->__('Other'),
        'class'     => 'required-entry',
        'required'  => true,
        'name'      => 'other',
    ));


    //$form->setValues($model->getData());
    $this->setForm($form);

    return parent::_prepareForm();
}
}

然后在文件app / app / design / adminhtml / default / default / layout / coupon.xml

<?xml version="1.0"?>
<layout version="0.1.0">
     <adminhtml_promo_quote_edit>
           <reference name="promo_quote_edit_tabs">
                <action method="addTab">
                     <name>promo_quote_edit_tab_custom</name>
                     <block>newgenray_coupon/adminhtml_promo_quote_edit_tab_custom</block>
                 </action>
            </reference>
      </adminhtml_promo_quote_edit>
  </layout>

最后一步将激活我们刚刚创建的模块

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