Php 将新操作添加到现有购物车价格规则操作
我有一个我无法解决的问题。部分原因是我无法用正确的术语解释它。我不熟悉这个问题,很抱歉问了这个笨拙的问题 下面你可以看到我的目标概述 我正在使用Magento CE 1.7.0.2 管理面板->促销->购物车价格规则->添加新规则->操作(选项卡) 在中,使用以下信息更新价格对于应用下拉列表,我想添加我的自定义选项 我该怎么做呢 我如何在不影响核心功能的情况下做到这一点 我为此做了一些研究,但从未找到任何凌驾于促销之上的文章 开始覆盖我发现应该覆盖以下内容的核心文件Php 将新操作添加到现有购物车价格规则操作,php,magento,Php,Magento,我有一个我无法解决的问题。部分原因是我无法用正确的术语解释它。我不熟悉这个问题,很抱歉问了这个笨拙的问题 下面你可以看到我的目标概述 我正在使用Magento CE 1.7.0.2 管理面板->促销->购物车价格规则->添加新规则->操作(选项卡) 在中,使用以下信息更新价格对于应用下拉列表,我想添加我的自定义选项 我该怎么做呢 我如何在不影响核心功能的情况下做到这一点 我为此做了一些研究,但从未找到任何凌驾于促销之上的文章 开始覆盖我发现应该覆盖以下内容的核心文件 /app/code/cor
Mage1/Adminhtml
Mage1/SalesRule
我的新模块文件夹结构
<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>
<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>
Rule.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();
}
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';
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;
}
}
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);
}
Validator.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();
}
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';
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;
}
}
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);
}
由于Actions.php和Rule.php文件的编辑,我应该在下拉列表中获得一个新选项,但我没有得到任何东西。我在这里做错了什么
我希望XML文件中缺少一些东西
有什么想法吗
请帮助我…如果您只想在下拉列表中添加选项,请转到
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'),
),
));
也去
Mage>SalesRule>Model>Rule
在第117行之后添加const Your_Custom_ACTION='Custom'代码>
选项将添加到您的下拉列表中
重写上面提到的文件/类,并以正确的方式执行它我正在使用Magento版本。1.9.0.0
我的文件夹和文件
/app/code/local/Mage1/Adminhtml/Block/Promo/Quote/Edit/Tab/Actions.php
/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>
0.0.1
Mage1\u Adminhtml\u Block\u Promo\u Quote\u Edit\u Tab\u Actions
别忘了在app/etc/modules中启用模块
为我工作
致以最良好的祝愿 有一种更简单的方法可以添加/删除购物车价格规则操作
<?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;
}
}
最好的方法是使用事件adminhtml\u block\u salesrule\u actions\u prepareform。请参见下面的示例:
名称空间/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>
使用父项::\u prepareForm()
它应该是什么:
return $this;
很明显,我只是给了他一个方法it@OSdave谢谢你的回复,请查看我的编辑帖子。不要编辑核心文件。2.不要编辑核心文件。3.如果仍要执行此操作,请不要在core/mage中编辑核心文件,而是在local/mage中执行此操作。但不要这样做,因为如果需要,您通常可以使用覆盖。在这种情况下,您只需观察adminhtml\u block\u salesrule\u actions\u prepareform。。。在不覆盖单个核心文件的情况下完成。伙计,不要碰核心文件夹!你好您的解决方案有效,但我很好奇为什么要返回$this而不是parent::\u prepareForm?为什么要返回$this而不是parent::\u prepareForm()?