0

I have extended the Block Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Main and added the changed code in the new class Acer_Coupon_Block_Adminhtml_Promo_Quote_Edit_Tab_Main. For some reason, the Block override does not work.

\app\etc\modules\Acer_Coupon.xml:

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

\app\code\local\Acer\Coupon\etc\config.xml:

<config>
    <modules>
        <Acer_Coupon>
            <version>0.1.0</version>
        <Acer_Coupon>
    </modules>
    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <promo_quote_edit_tab_main>Acer_Coupon_Block_Adminhtml_Promo_Quote_Edit_Tab_Main</promo_quote_edit_tab_main>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

\app\code\local\Acer\Coupon\Block\Adminhtml\Promo\Quote\Edit\Tab\Main.php:

<?php
class Acer_Coupon_Block_Adminhtml_Promo_Quote_Edit_Tab_Main extends Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Main
{
    protected function _prepareForm()
    {
        $model = Mage::registry('current_promo_quote_rule');

        //$form = new Varien_Data_Form(array('id' => 'edit_form1', 'action' => $this->getData('action'), 'method' => 'post'));
        $form = new Varien_Data_Form();

        $form->setHtmlIdPrefix('rule_');

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

        if ($model->getId()) {
            $fieldset->addField('rule_id', 'hidden', array(
                'name' => 'rule_id',
            ));
        }

        $fieldset->addField('product_ids', 'hidden', array(
            'name' => 'product_ids',
        ));

        $fieldset->addField('name', 'text', array(
            'name' => 'name',
            'label' => Mage::helper('salesrule')->__('Rule Name'),
            'title' => Mage::helper('salesrule')->__('Rule Name'),
            'required' => true,
        ));

        $fieldset->addField('description', 'textarea', array(
            'name' => 'description',
            'label' => Mage::helper('salesrule')->__('Description'),
            'title' => Mage::helper('salesrule')->__('Description'),
            'style' => 'width: 98%; height: 100px;',
        ));

        $fieldset->addField('is_active', 'select', array(
            'label'     => Mage::helper('salesrule')->__('Status'),
            'title'     => Mage::helper('salesrule')->__('Status'),
            'name'      => 'is_active',
            'required' => true,
            'options'    => array(
                '1' => Mage::helper('salesrule')->__('Active'),
                '0' => Mage::helper('salesrule')->__('Inactive'),
            ),
        ));
        if (!$model->getId()) {
            $model->setData('is_active', '1');
        }


        if (!Mage::app()->isSingleStoreMode()) {
            $fieldset->addField('website_ids', 'multiselect', array(
                'name'      => 'website_ids[]',
                'label'     => Mage::helper('catalogrule')->__('Websites'),
                'title'     => Mage::helper('catalogrule')->__('Websites'),
                'required'  => true,
                'values'    => Mage::getSingleton('adminhtml/system_config_source_website')->toOptionArray(),
            ));
        }
        else {
            $fieldset->addField('website_ids', 'hidden', array(
                'name'      => 'website_ids[]',
                'value'     => Mage::app()->getStore(true)->getWebsiteId()
            ));
            $model->setWebsiteIds(Mage::app()->getStore(true)->getWebsiteId());
        }

        $customerGroups = Mage::getResourceModel('customer/group_collection')
            ->load()->toOptionArray();

        $found = false;
        foreach ($customerGroups as $group) {
            if ($group['value']==0) {
                $found = true;
            }
        }
        if (!$found) {
            array_unshift($customerGroups, array('value'=>0, 'label'=>Mage::helper('salesrule')->__('NOT LOGGED IN')));
        }

        $fieldset->addField('customer_group_ids', 'multiselect', array(
            'name'      => 'customer_group_ids[]',
            'label'     => Mage::helper('salesrule')->__('Customer Groups'),
            'title'     => Mage::helper('salesrule')->__('Customer Groups'),
            'required'  => true,
            'values'    => $customerGroups,
        ));

        $couponTypeFiled = $fieldset->addField('coupon_type', 'select', array(
            'name'       => 'coupon_type',
            'label'      => Mage::helper('salesrule')->__('Coupon'),
            'required'   => true,
            'options'    => Mage::getModel('salesrule/rule')->getCouponTypes(),
        ));

        $couponCodeFiled = $fieldset->addField('coupon_code', 'text', array(
            'name' => 'coupon_code',
            'class' => 'validate-coupon-code',
            'label' => Mage::helper('salesrule')->__('Extended Coupon Code'),
            'required' => true,
        ));

        $usesPerCouponFiled = $fieldset->addField('uses_per_coupon', 'text', array(
            'name' => 'uses_per_coupon',
            'label' => Mage::helper('salesrule')->__('Uses per Coupon'),
        ));

        $fieldset->addField('uses_per_customer', 'text', array(
            'name' => 'uses_per_customer',
            'label' => Mage::helper('salesrule')->__('Uses per Customer'),
        ));

        $dateFormatIso = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
        $fieldset->addField('from_date', 'date', array(
            'name'   => 'from_date',
            'label'  => Mage::helper('salesrule')->__('From Date'),
            'title'  => Mage::helper('salesrule')->__('From Date'),
            'image'  => $this->getSkinUrl('images/grid-cal.gif'),
            'input_format' => Varien_Date::DATE_INTERNAL_FORMAT,
            'format'       => $dateFormatIso
        ));
        $fieldset->addField('to_date', 'date', array(
            'name'   => 'to_date',
            'label'  => Mage::helper('salesrule')->__('To Date'),
            'title'  => Mage::helper('salesrule')->__('To Date'),
            'image'  => $this->getSkinUrl('images/grid-cal.gif'),
            'input_format' => Varien_Date::DATE_INTERNAL_FORMAT,
            'format'       => $dateFormatIso
        ));

        $fieldset->addField('sort_order', 'text', array(
            'name' => 'sort_order',
            'label' => Mage::helper('salesrule')->__('Priority'),
        ));

        $fieldset->addField('is_rss', 'select', array(
            'label'     => Mage::helper('salesrule')->__('Public In RSS Feed'),
            'title'     => Mage::helper('salesrule')->__('Public In RSS Feed'),
            'name'      => 'is_rss',
            'options'   => array(
                '1' => Mage::helper('salesrule')->__('Yes'),
                '0' => Mage::helper('salesrule')->__('No'),
            ),
        ));

        if(!$model->getId()){
            //set the default value for is_rss feed to yes for new promotion
            $model->setIsRss(1);
        }

        $form->setValues($model->getData());

        if ($model->isReadonly()) {
            foreach ($fieldset->getElements() as $element) {
                $element->setReadonly(true, true);
            }
        }

        //$form->setUseContainer(true);

        $this->setForm($form);

        // field dependencies
        $this->setChild('form_after', $this->getLayout()->createBlock('adminhtml/widget_form_element_dependence')
            ->addFieldMap($couponTypeFiled->getHtmlId(), $couponTypeFiled->getName())
            ->addFieldMap($couponCodeFiled->getHtmlId(), $couponCodeFiled->getName())
            ->addFieldMap($usesPerCouponFiled->getHtmlId(), $usesPerCouponFiled->getName())
            ->addFieldDependence(
                $couponCodeFiled->getName(),
                $couponTypeFiled->getName(),
                Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
            ->addFieldDependence(
                $usesPerCouponFiled->getName(),
                $couponTypeFiled->getName(),
                Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
        );

        Mage::dispatchEvent('adminhtml_promo_quote_edit_tab_main_prepare_form', array('form' => $form));

        return parent::_prepareForm();
    }
}

I've changed the label of coupon code to "Extended Coupon code" in above file. But it still shows the label as "Coupon code" so it is certain that something is wrong in my procedure.

I've searched through many different links and tried several approaches like putting Block inside Adminhtml and vice-versa, but nothing worked.

I have developed a good habit of clearing cache after every change(mostly xml files).

Can anyone point me in the right direction?

4
  • Can you put a die(); at the top of Acer_Coupon_Block_Adminhtml_Promo_Quote_Edit_Tab_Main::_prepareForm() to see if your rewrite is been execute ... the issue seem to be the fact that your return parent::_prepareForm(); Sep 12 '15 at 15:53
  • putting "die();" at the first did not make any difference.. so basically the rewrite is not executed Sep 14 '15 at 15:51
  • Can you check to see if the module is enable? Did you clear cache? Sep 14 '15 at 15:55
  • Yes, the module is enabled. just re-confirmed it from System->configuration->Advanced. And yes, I cleared Cache before trying Sep 14 '15 at 16:01
2

Because your rewrite return parent::_prepareForm(); which is class Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Main you will need to rewrite it parent class.

In \app\code\local\Acer\Coupon\Block\Adminhtml\Promo\Quote\Edit\Tab\Main.php:

class Acer_Coupon_Block_Adminhtml_Promo_Quote_Edit_Tab_Main
extends Mage_Adminhtml_Block_Widget_Form
implements Mage_Adminhtml_Block_Widget_Tab_Interface
{
  .....

Then copy all the content of Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Main and then make your changes

7
  • Today, I tried your suggestion, but it still does not work. So, it means somehow the rewrite fails Sep 14 '15 at 15:52
  • As you stated the rewrite is not executing. So we need to figure that out first Sep 14 '15 at 15:53
  • Good news is after removing "<modules> tag from the config.xml, rewrite worked :) Sep 14 '15 at 16:09
  • and once rewriting worked, I followed this answer and everything works great :) can you pls edit your answer and mention about modules tag in config,xml? so that I can accept your answer Sep 14 '15 at 16:12
  • The "modules" tag in the config shouldn't affect anyting Sep 14 '15 at 16:19
1

Following Renon Stewart's answer, you could also change the return call to:

return $this->getLayout()->createBlock('adminhtml/widget_form')->_prepareForm();

Instead of rewriting the parent class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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