将新操作添加到现有购物车价格规则操作

时间:2014-08-26 11:41:46

标签: php magento

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

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

我正在使用Magento CE 1.7.0.2。

管理员面板 - >促销活动 - >购物车价格规则 - >添加新规则 - >行动(标签)

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

enter image description here

我该怎么做。

如何在不影响核心功能的情况下实现这一目标......

我为此做了一些研究,我找不到任何覆盖促销活动的文章。

开始覆盖我找到的核心文件我应该覆盖以下内容。

  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
  4. 在我的本地 Codepool创建的文件中,例如Mage1/Adminhtml Mage1/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
    4. 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&amp; Rule.php文件编辑我应该在Dropdown中获得一个新选项但我没有得到任何东西。我在这里做错了什么。

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

      有什么想法吗?

      请帮帮我......

5 个答案:

答案 0 :(得分:2)

最好的方法是使用事件 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);
}

答案 1 :(得分:1)

我正在使用Magento ver。 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 **
  3. 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中启用模块

    为我工作

    最好的问候!

答案 2 :(得分:1)

使用 parent::_prepareForm()

return parent::_prepareForm();

它应该是什么:

return $this;

答案 3 :(得分:0)

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

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

答案 4 :(得分:-3)

如果您只想在下拉列表中添加选项,请转到Core > Mage > Adminhtml > Block > Promo > Quote > Edit > Tab > 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'),

        ),
    ));

还可以去

在第117行添加Mage > SalesRule > Model > Rule之后

const Your_Custom_ACTION = 'custom'; 并且选项将添加到您的下拉列表中

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