1

i want to add a custom fields in cart rules's general information tab, the data will be stored in additional table with corresponding cart rules id.

enter image description here

what is the best practice to do this? should i use rewrite to do this? so rewrite which class? and what observer should be used?

1

1 Answer 1

0

To me you should definitely use observers.

I'm not gonna go through the process of creating an additional database table. Just don't forget to create the related model/resource models/collection classes for your new entity.

Then you can observe the following adminhtml events:

  • adminhtml_promo_quote_edit_tab_main_prepare_form: this one is called during the shopping cart rule form preparation and will be used to add your new field
  • controller_action_postdispatch_adminhtml_promo_quote_save: this one is called after you saved the shopping cart rule.

Example:

<adminhtml>
    <events>
        <!-- Called during the form preparation -->
        <adminhtml_promo_quote_edit_tab_main_prepare_form>
            <observers>
                <custom_prepare_quote_form>
                    <type>singleton</type>
                    <class>module/admin_observer</class>
                    <method>prepareQuoteForm</method>
                </custom_prepare_quote_form>
            </observers>
        </adminhtml_promo_quote_edit_tab_main_prepare_form>
        <!-- Called once the save customer group action has been called -->
        <controller_action_postdispatch_adminhtml_promo_quote_save>
            <observers>
                <custom_promo_quote_save>
                    <type>singleton</type>
                    <class>module/admin_observer</class>
                    <method>promoQuoteSave</method>
                </custom_promo_quote_save>
            </observers>
        </controller_action_postdispatch_adminhtml_promo_quote_save>
    </events>
</adminhtml>

Then in your Admin/Observer.php you can do the following:

public function prepareQuoteForm(Varien_Event_Observer $observer)
{
    // Get form
    /** @var Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Main $form */
    $form = $observer->getEvent()->getForm();
    // Get fieldset
    $fieldset = $form->getElement('base_fieldset');
    // Add the field
    $fieldset->addField('my_custom_field', 'text',
        [
            'name'      => 'my_custom_field',
            'label'     => Mage::helper('module')->__('My Custom Field'),
            'title'     => Mage::helper('module')->__('My Custom Field'),
            'required'  => false
        ]
    );
}

This method will add your field to the form. After that code I suggest you load your custom entity corresponding to the shopping cart rule and use $form->addValues() to add the values to the form so it reflects data from the DB

The second method:

public function promoQuoteSave(Varien_Event_Observer $observer)
{
    // Get the action
    $action = $observer->getEvent()->getControllerAction();
    // Get the data
    $data = $action->getRequest()->getParams();
    // Add your custom entity data to the DB
}

This method needs to be used to add your custom data to your additional DB table so you'll have to write that code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.