【问题标题】:Add new Action to the existing Shopping Cart Price Rules Actions向现有购物车价格规则操作添加新操作
【发布时间】:2014-10-19 17:31:48
【问题描述】:

我遇到了无法解决的问题。部分原因是我无法用正确的术语来解释它。我是新手,很抱歉这个笨拙的问题。

您可以在下面看到我的目标概览。

我正在使用 Magento CE 1.7.0.2。

管理面板 -> 促销 -> 购物车价格规则 -> 添加新规则 -> 操作(选项卡)

使用以下信息更新价格对于应用下拉菜单,我想添加我的自定义选项。

我该怎么做。

如何在不影响核心功能的情况下做到这一点...

我对此进行了一些研究,但从未找到任何关于覆盖促销的文章。

开始覆盖核心文件,我发现我应该覆盖以下内容。

  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

在我的本地代码池中创建了类似Mage1/AdminhtmlMage1/SalesRule

的文件

我的新模块文件夹结构

  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;
    }
}

由于 Actions.php 和 Rule.php 文件编辑,我应该在下拉菜单中获得一个新选项,但我什么也没得到。我在这里做错了什么。

我希望 XML 文件中缺少一些东西。

有什么想法吗?

请帮帮我...

【问题讨论】:

标签: php magento


【解决方案1】:

最好的方法是使用事件 adminhtml_block_salesrule_actions_prepareform。请看下面的例子:

命名空间/模块/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>

还有他们:

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);
}

【讨论】:

    【解决方案2】:

    我正在使用 Magento 版本。 1.9.0.0

    我的文件夹和文件

    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>
    

    别忘了在 app/etc/modules 中启用你的模块

    为我工作

    最好的问候!

    【讨论】:

    • 嗨。您的解决方案有效,但我很好奇您为什么要返回 $this 而不是 parent::_prepareForm ?
    【解决方案3】:

    使用 parent::_prepareForm()

    return parent::_prepareForm();
    

    应该是什么:

    return $this;
    

    【讨论】:

    • 为什么要返回 $this 而不是 parent::_prepareForm()?
    【解决方案4】:

    有一种更简单的方法可以添加/删除购物车价格规则操作。

    <?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;
        }
    }
    

    【讨论】:

      【解决方案5】:

      如果您只想在下拉列表中添加一个选项,请转到 Core &gt; Mage &gt; Adminhtml &gt; Block &gt; Promo &gt; Quote &gt; Edit &gt; Tab &gt; Actions.php 并向数组添加一个选项

      $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'),
      
              ),
          ));
      

      还有去

      Mage &gt; SalesRule &gt; Model &gt; Rule 在第 117 行后添加 const Your_Custom_ACTION = 'custom'; 选项将添加到您的下拉列表中

      覆盖上述文件/类,并以正确的方式进行

      【讨论】:

      • @OSdave 是的,这很明显,我只是给了他一个想法
      • @OSdave,Ahmed 感谢您的回复,请查看我的编辑帖子。
      • 1.不要编辑核心文件。 2. 不要编辑核心文件。 3.如果你无论如何都要这样做,不要在core/mage中编辑core文件,在local/mage中进行。但不要因为您通常可以在需要时使用覆盖。在这种情况下,您只需观察 adminhtml_block_salesrule_actions_prepareform... 并且无需覆盖单个核心文件即可。
      • 伙计,不要触摸核心文件夹!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 2013-02-18
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多