drvxnivoqf17568697 2014-02-04 10:11
浏览 43

如何创建自定义扩展使用条件规则函数

I'm planning to build an extension like Shopping Cart, Price Rule, or Catalog Price Rule.

I've already tried to learn something from existing Magento code, that you can see on:

app/code/core/Mage/Adminhtml/Block/Promo/Quote/Edit/Tab/Conditions.php

To show a Conditions Rule field, I've tried to add this script, but it didn't work properly

$fieldset->addField('conditions', 'text', array(
        'name' => 'conditions',
        'label' => Mage::helper('salesrule')->__('Conditions'),
        'title' => Mage::helper('salesrule')->__('Conditions'),
    ))->setRule($model)->setRenderer(Mage::getBlockSingleton('rule/conditions'));

The question is:

  1. How to display the conditional field properly on my custom field?
  2. How to apply rule conditions on the front-end?

Thanks in advance.

update, take a look at my screenshot

https://docs.google.com/file/d/0BwLN4KpQhoGbU181R0ZKanJSdVE/edit?usp=drivesdk

this is my form.php:

<?php
class KS_Kscoba_Block_Adminhtml_Tcoba_Edit_Tab_Form 
extends Mage_Adminhtml_Block_Widget_Form

/*
extends Mage_Adminhtml_Block_Widget_Form
    implements Mage_Adminhtml_Block_Widget_Tab_Interface
*/
{
        protected function _prepareForm()
        {

                $model = Mage::registry('current_promo_quote_rule');
                $form = new Varien_Data_Form();
                $this->setForm($form);
                $fieldset = $form->addFieldset("kscoba_form", array("legend"=>Mage::helper("kscoba")->__("Item information")));


                        $fieldset->addField("kolom1", "text", array(
                        "label" => Mage::helper("kscoba")->__("Kolom 1"),
                        "name" => "kolom1",
                        ));

                         $fieldset->addField('kolom2', 'select', array(
                        'label'     => Mage::helper('kscoba')->__('Kolom 2'),
                        'values'   => KS_Kscoba_Block_Adminhtml_Tcoba_Grid::getValueArray1(),
                        'name' => 'kolom2',
                        ));

                /*
                problem start here
                */      

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

                $fieldset = $form->addFieldset('conditions_fieldset', array(
                    'legend'=>Mage::helper('salesrule')->__('Apply the rule only if the following conditions are met (leave blank for all products)')
                ))->setRenderer($renderer);

                $fieldset->addField('conditions', 'text', array(
                    'name' => 'conditions',
                    'label' => Mage::helper('salesrule')->__('Conditions'),
                    'title' => Mage::helper('salesrule')->__('Conditions'),
                ))->setRule($model)->setRenderer(Mage::getBlockSingleton('rule/conditions'));




                if (Mage::getSingleton("adminhtml/session")->getTcobaData())
                {
                    $form->setValues(Mage::getSingleton("adminhtml/session")->getTcobaData());
                    Mage::getSingleton("adminhtml/session")->setTcobaData(null);
                } 
                elseif(Mage::registry("tcoba_data")) {
                    $form->setValues(Mage::registry("tcoba_data")->getData());
                }
                return parent::_prepareForm();

        }
}

am I missing something?

  • 写回答

1条回答 默认 最新

  • doushai4890 2014-02-14 03:21
    关注

    1. Conditions Field

    I may be overlooking another issue, but when I tested your form.php, the conditions field was missing because Mage::registry('current_promo_quote_rule') was undefined. The conditions field appeared on the page after I populated $model with a Mage_SalesRule_Model_Rule object.

    Magento 1.8 registers the current_promo_quote_rule in _initRule() and editAction() of the Mage_Adminhtml_Promo_QuoteController (app/code/core/Mage/Adminhtml/controllers/Promo/QuoteController.php).


    2. Frontend

    Using shopping cart price rules as an example, the discounted price is applied in the frontend through the checkout module.

    Mage/Checkout/controllers/CartController.php has a couponPostAction() function which is called when the user submits a coupon code from the cart or checkout page. This function gets the cart's Mage_Sales_Model_Quote object, sets the coupon code on that object, and refreshes the totals of each item using the collectTotals() function of Mage_Sales_Model_Quote.

    The quote object's collectTotals() gets the related Mage_Sales_Model_Quote_Address objects and calls their collectTotals() functions. Those functions get each of the collector objects associated with the address and call its collect() method.

    One of those collector objects is a Mage_SalesRule_Model_Quote_Discount, whose collect() method gets each Mage_Sales_Model_Quote_Item associated with this address, then calculates and stores its discount using a Mage_SalesRule_Model_Validator.

    The specific logic in the conditions is read and applied deeper in the SalesRule module.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示