简体   繁体   中英

Add new Action to the existing Shopping Cart Price Rules Actions

I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.

Below you can see an overview of my goal.

I'm using Magento CE 1.7.0.2.

Admin Panel -> Promotions -> Shopping Cart Price Rules -> Add New Rule -> Actions (Tab)

In the Update prices using the following information For the Apply Dropdown i Want to add my custom Option.

在此处输入图片说明

How do i do that.

How do i Do that with out effecting the Core Functionalities...

I did some research for this i never find any article for Overriding the Promotions.

Started overriding the Core files for that i found i should override the following things.

  1. /app/code/core/Mage/Adminhtml/Block/Promo/Quote/Edit/Tab/Actions.php
  2. /app/code/core/Mage/SalesRule/Model/Rule.php
  3. /app/code/core/Mage/SalesRule/Model/Validator.php

In my local Codepool created file like Mage1/Adminhtml Mage1/SalesRule

My new module folder structure

  1. /app/code/local/Mage1/Adminhtml/Block/Promo/Quote/Edit/Tab/Actions.php
  2. /app/code/local/Mage1/SalesRule/Model/Rule.php
  3. /app/code/local/Mage1/SalesRule/Model/Validator.php

Mage1/Adminhtml/etc/config.xml

<config>
    <modules>
        <Mage1_Adminhtml>
            <version>1.0.0</version>
        </Mage1_Adminhtml>
    </modules>
    <global>
        <models>
            <adminhtml>
                <rewrite>
                    <rule>Mage1_SalesRule_Model_Rule</rule>
                </rewrite>
            </adminhtml>
        </models>
        <blocks>
            <adminhtml>
                <rewrite>
                    <promo_quote_edit_tab_actions>Mage1_Adminhtml_Block_Promo_Quote_Edit_Tab_Actions</promo_quote_edit_tab_actions>
                </rewrite>
            </adminhtml>
        </blocks>
        <helpers>
            <adminhtml>
                <class>Mage1_Adminhtml_Helper</class>
            </adminhtml>
        </helpers>
    </global>
</config>

Mage1/SalesRule/etc/config.xml

<config>
    <modules>
        <Mage1_SalesRule>
            <version>1.0.0</version>
        </Mage1_SalesRule>
    </modules>
    <global>
        <models>
            <salesrule>
                <rewrite>     
                    <validator>Mage1_SalesRule_Model_Validator</validator>
                </rewrite>
            </salesrule>
        </models>
        <helpers>
            <salesrule>
                <class>Mage1_SalesRule_Helper</class>
            </salesrule>
        </helpers>
    </global>
</config>

Actions.php

protected function _prepareForm() {
        $model = Mage::registry('current_promo_quote_rule');
        $form = new Varien_Data_Form();
        $form->setHtmlIdPrefix('rule_');
        $fieldset = $form->addFieldset('action_fieldset', array('legend'=>Mage::helper('salesrule')->__('Update prices using the following information')));
        $fieldset->addField('simple_action', 'select', array(
            'label'     => Mage::helper('salesrule')->__('Apply'),
            'name'      => 'simple_action',
            'options'    => array(
                Mage_SalesRule_Model_Rule::BY_PERCENT_ACTION => Mage::helper('salesrule')->__('Percent of product price discount'),
                Mage_SalesRule_Model_Rule::BY_FIXED_ACTION => Mage::helper('salesrule')->__('Fixed amount discount'),
                Mage_SalesRule_Model_Rule::CART_FIXED_ACTION => Mage::helper('salesrule')->__('Fixed amount discount for whole cart'),
                Mage_SalesRule_Model_Rule::BUY_X_GET_Y_ACTION => Mage::helper('salesrule')->__('Buy X get Y free (discount amount is Y)'),
                Mage_SalesRule_Model_Rule::GET_PERCENT_X_MAX_Y_ACTION => Mage::helper('salesrule')->__('Percent of total cart Max discount'),
            ),
        ));
        $fieldset->addField('discount_amount', 'text', array(
            'name' => 'discount_amount',
            'required' => true,
            'class' => 'validate-not-negative-number',
            'label' => Mage::helper('salesrule')->__('Discount Amount'),
        ));
        $model->setDiscountAmount($model->getDiscountAmount()*1);
        $fieldset->addField('discount_qty', 'text', array(
            'name' => 'discount_qty',
            'label' => Mage::helper('salesrule')->__('Maximum Qty Discount is Applied To'),
        ));
        $model->setDiscountQty($model->getDiscountQty()*1);
        $fieldset->addField('discount_step', 'text', array(
            'name' => 'discount_step',
            'label' => Mage::helper('salesrule')->__('Discount Qty Step (Buy X)'),
        ));
        $fieldset->addField('apply_to_shipping', 'select', array(
            'label'     => Mage::helper('salesrule')->__('Apply to Shipping Amount'),
            'title'     => Mage::helper('salesrule')->__('Apply to Shipping Amount'),
            'name'      => 'apply_to_shipping',
            'values'    => Mage::getSingleton('adminhtml/system_config_source_yesno')->toOptionArray(),
        ));
        $fieldset->addField('simple_free_shipping', 'select', array(
            'label'     => Mage::helper('salesrule')->__('Free Shipping'),
            'title'     => Mage::helper('salesrule')->__('Free Shipping'),
            'name'      => 'simple_free_shipping',
            'options'    => array(
                0 => Mage::helper('salesrule')->__('No'),
                Mage_SalesRule_Model_Rule::FREE_SHIPPING_ITEM => Mage::helper('salesrule')->__('For matching items only'),
                Mage_SalesRule_Model_Rule::FREE_SHIPPING_ADDRESS => Mage::helper('salesrule')->__('For shipment with matching items'),
            ),
        ));
        $fieldset->addField('stop_rules_processing', 'select', array(
            'label'     => Mage::helper('salesrule')->__('Stop Further Rules Processing'),
            'title'     => Mage::helper('salesrule')->__('Stop Further Rules Processing'),
            'name'      => 'stop_rules_processing',
            'options'    => array(
                '1' => Mage::helper('salesrule')->__('Yes'),
                '0' => Mage::helper('salesrule')->__('No'),
            ),
        ));
        $renderer = Mage::getBlockSingleton('adminhtml/widget_form_renderer_fieldset')
            ->setTemplate('promo/fieldset.phtml')
            ->setNewChildUrl($this->getUrl('*/promo_quote/newActionHtml/form/rule_actions_fieldset'));
        $fieldset = $form->addFieldset('actions_fieldset', array(
            'legend'=>Mage::helper('salesrule')->__('Apply the rule only to cart items matching the following conditions (leave blank for all items)')
        ))->setRenderer($renderer);
        $fieldset->addField('actions', 'text', array(
            'name' => 'actions',
            'label' => Mage::helper('salesrule')->__('Apply To'),
            'title' => Mage::helper('salesrule')->__('Apply To'),
            'required' => true,
        ))->setRule($model)->setRenderer(Mage::getBlockSingleton('rule/actions'));
        Mage::dispatchEvent('adminhtml_block_salesrule_actions_prepareform', array('form' => $form));
        $form->setValues($model->getData());
        if ($model->isReadonly()) {
            foreach ($fieldset->getElements() as $element) {
                $element->setReadonly(true, true);
            }
        }
        $this->setForm($form);
        return parent::_prepareForm();
    }

Rule.php

const TO_PERCENT_ACTION = 'to_percent';
const BY_PERCENT_ACTION = 'by_percent';
const TO_FIXED_ACTION   = 'to_fixed';
const BY_FIXED_ACTION   = 'by_fixed';
const CART_FIXED_ACTION = 'cart_fixed';
const BUY_X_GET_Y_ACTION = 'buy_x_get_y';
const GET_PERCENT_X_MAX_Y_ACTION = 'get_x_max_y';

Validator.php

public function process(Mage_Sales_Model_Quote_Item_Abstract $item) {
    Mage::log('I'm Inside Process Function');
    switch ($rule->getSimpleAction()) {
        case Mage_SalesRule_Model_Rule::GET_PERCENT_X_MAX_Y_ACTION:
            Mage::log('Helllll..');
            break;
    }
}

Because of Actions.php & Rule.php files editing i should get a new option in Dropdown But i'm not getting anything. Anything wrong i did here.

I Hope Some thing i'm missing in XML files.

Any ideas ?

Help me please...

The best way is using event adminhtml_block_salesrule_actions_prepareform . See the example below:

Namespace/Module/etc/config.xml:

 <adminhtml>
    <events>
        <adminhtml_block_salesrule_actions_prepareform>
            <observers>
                <add_custom_salesrule_actions>
                    <class>namespace_module/observer</class>
                    <method>addCustomSalesRuleAction</method>
                </add_custom_salesrule_actions>
            </observers>
        </adminhtml_block_salesrule_actions_prepareform>
    </events>
</adminhtml>

And them:

Namespace_Module_Model_Observer.php

public function addCustomSalesRuleAction(Varien_Event_Observer $observer)
{
    $form = $observer->getEvent()->getForm();
    $element = $form->getElement('simple_action');
    $values = $element->getValues();
    $values[Namespace_Module_Model_Rule::CUSTOM_ACTION] = 'Your custom action Label';
    $element->setValues($values);
}

I'm using Magento ver. 1.9.0.0

my folders and files

  1. /app/code/local/Mage1/Adminhtml/Block/Promo/Quote/Edit/Tab/Actions.php
  2. /app/code/local/Mage1/Adminhtml/etc/config.xml**

Actions.php

class Mage1_Adminhtml_Block_Promo_Quote_Edit_Tab_Actions
    extends Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Actions
{
    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('action_fieldset', array('legend'=>Mage::helper('salesrule')->__('Update prices using the following information')));

        $fieldset->addField('simple_action', 'select', array(
            'label'     => Mage::helper('salesrule')->__('Apply'),
            'name'      => 'simple_action',
            'options'    => array(
                Mage_SalesRule_Model_Rule::BY_PERCENT_ACTION => Mage::helper('salesrule')->__('Percent of product price discount'),
                Mage_SalesRule_Model_Rule::BY_FIXED_ACTION => Mage::helper('salesrule')->__('Fixed amount discount'),
                Mage_SalesRule_Model_Rule::CART_FIXED_ACTION => Mage::helper('salesrule')->__('Fixed amount discount for whole cart'),
                Mage_SalesRule_Model_Rule::BUY_X_GET_Y_ACTION => Mage::helper('salesrule')->__('Buy X get Y free (discount amount is Y)'),
                Mage_SalesRule_Model_Rule::Your_Action => Mage::helper('salesrule')->__('Your_Action)'), // Add your action
            ),
        ));
        $fieldset->addField('discount_amount', 'text', array(
            'name' => 'discount_amount',
            'required' => true,
            'class' => 'validate-not-negative-number',
            'label' => Mage::helper('salesrule')->__('Discount Amount'),
        ));
        $model->setDiscountAmount($model->getDiscountAmount()*1);

        $fieldset->addField('discount_qty', 'text', array(
            'name' => 'discount_qty',
            'label' => Mage::helper('salesrule')->__('Maximum Qty Discount is Applied To'),
        ));
        $model->setDiscountQty($model->getDiscountQty()*1);

        $fieldset->addField('discount_step', 'text', array(
            'name' => 'discount_step',
            'label' => Mage::helper('salesrule')->__('Discount Qty Step (Buy X)'),
        ));

        $fieldset->addField('apply_to_shipping', 'select', array(
            'label'     => Mage::helper('salesrule')->__('Apply to Shipping Amount'),
            'title'     => Mage::helper('salesrule')->__('Apply to Shipping Amount'),
            'name'      => 'apply_to_shipping',
            'values'    => Mage::getSingleton('adminhtml/system_config_source_yesno')->toOptionArray(),
        ));

        $fieldset->addField('simple_free_shipping', 'select', array(
            'label'     => Mage::helper('salesrule')->__('Free Shipping'),
            'title'     => Mage::helper('salesrule')->__('Free Shipping'),
            'name'      => 'simple_free_shipping',
            'options'    => array(
                0 => Mage::helper('salesrule')->__('No'),
                Mage_SalesRule_Model_Rule::FREE_SHIPPING_ITEM => Mage::helper('salesrule')->__('For matching items only'),
                Mage_SalesRule_Model_Rule::FREE_SHIPPING_ADDRESS => Mage::helper('salesrule')->__('For shipment with matching items'),
            ),
        ));

        $fieldset->addField('stop_rules_processing', 'select', array(
            'label'     => Mage::helper('salesrule')->__('Stop Further Rules Processing'),
            'title'     => Mage::helper('salesrule')->__('Stop Further Rules Processing'),
            'name'      => 'stop_rules_processing',
            'options'    => array(
                '1' => Mage::helper('salesrule')->__('Yes'),
                '0' => Mage::helper('salesrule')->__('No'),
            ),
        ));

        $renderer = Mage::getBlockSingleton('adminhtml/widget_form_renderer_fieldset')
            ->setTemplate('promo/fieldset.phtml')
            ->setNewChildUrl($this->getUrl('*/promo_quote/newActionHtml/form/rule_actions_fieldset'));

        $fieldset = $form->addFieldset('actions_fieldset', array(
            'legend'=>Mage::helper('salesrule')->__('Apply the rule only to cart items matching the following conditions (leave blank for all items)')
        ))->setRenderer($renderer);

        $fieldset->addField('actions', 'text', array(
            'name' => 'actions',
            'label' => Mage::helper('salesrule')->__('Apply To'),
            'title' => Mage::helper('salesrule')->__('Apply To'),
            'required' => true,
        ))->setRule($model)->setRenderer(Mage::getBlockSingleton('rule/actions'));

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

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

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

        $this->setForm($form);

        return $this; // replace parent::_prepareForm();
    }

}

config.xml

<?xml version="1.0" ?>
<config>
    <modules>
        <Mage1_Adminhtml>
            <version>0.0.1</version>
        </Mage1_Adminhtml>
    </modules>

    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <promo_quote_edit_tab_actions>Mage1_Adminhtml_Block_Promo_Quote_Edit_Tab_Actions</promo_quote_edit_tab_actions>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

don't forget enable you module in app/etc/modules

work for me

best regards!

Using parent::_prepareForm()

return parent::_prepareForm();

What it Should Be:

return $this;

There's an easier way to add/remove shopping cart price rule Actions.

<?php

class Custom_Adminhtml_Block_Promo_Quote_Edit_Tab_Actions
    extends Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Actions
{

    protected function _prepareForm()
    {
        parent::_prepareForm();
        $form = $this->getForm()->getElements();
        $fieldset = $form[0];
        $elements = $fieldset->getSortedElements();
        foreach($elements as $element) {
            if ($element->getId() == "simple_action") {
                $options = $element->getOptions();
                $new_options = array(
                    Mage_SalesRule_Model_Rule::BY_PERCENT_ACTION => Mage::helper('salesrule')->__('Text 1'),
                    Mage_SalesRule_Model_Rule::TO_PERCENT_ACTION => Mage::helper('salesrule')->__('Text 2'),
                    Mage_SalesRule_Model_Rule::BY_FIXED_ACTION => Mage::helper('salesrule')->__('Text 3'),
                    Mage_SalesRule_Model_Rule::TO_FIXED_ACTION => Mage::helper('salesrule')->__('Text 4'),
                    Mage_SalesRule_Model_Rule::CART_FIXED_ACTION => Mage::helper('salesrule')->__('Text 5'),
                    Mage_SalesRule_Model_Rule::BUY_X_GET_Y_ACTION => Mage::helper('salesrule')->__('Text 6'),
                );
                $element->setValues($new_options);
                break;
            }
        }
        return $this;
    }
}

If you just want to add an option in dropdown go to Core > Mage > Adminhtml > Block > Promo > Quote > Edit > Tab > Actions.php and add an option to the array

$fieldset->addField('simple_action', 'select', array(
        'label'     => Mage::helper('salesrule')->__('Apply'),
        'name'      => 'simple_action',
        'options'    => array(
            Mage_SalesRule_Model_Rule::BY_PERCENT_ACTION => Mage::helper('salesrule')->__('Percent of product price discount'),
            Mage_SalesRule_Model_Rule::BY_FIXED_ACTION => Mage::helper('salesrule')->__('Fixed amount discount'),
            Mage_SalesRule_Model_Rule::CART_FIXED_ACTION => Mage::helper('salesrule')->__('Fixed amount discount for whole cart'),
            Mage_SalesRule_Model_Rule::BUY_X_GET_Y_ACTION => Mage::helper('salesrule')->__('Buy X get Y free (discount amount is Y)'),
            Mage_SalesRule_Model_Rule::Your_Custom_ACTION => Mage::helper('salesrule')->__('Custom Action Text'),

        ),
    ));

And also go to

Mage > SalesRule > Model > Rule after line 117 add const Your_Custom_ACTION = 'custom'; And option will be added to your dropdown

Override the above mentioned file/class, and do it a proper way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM