How to create custom price in Magento?

来源:互联网 发布:wish for mac 编辑:程序博客网 时间:2024/05/17 04:10

I recently needed a very specific shopping cart pricerule that I could not create with the existing logic inthe shopping cart price rules form. Programming to therescue.

What I needed to create was a rule that would allow each itembought after a certain qty to be half price.  Inmy case the magic qty was two.  so the rule was:buy two items at full price – get the rest of the qty for that itemat half price.  The percentage off and step qtyare all controlled via the normal admin rule forms.

Here’s how I managed to do it for my needs, but you can use thisprocedure to create any type of rule you that you can’t via thedefault logic rules.

Shopping cart conditionsShopping cart actionsDiscount in cartView of discount during checkout

There are two spots that normally handlethis job. One for creating the rules in admin and the other forvalidating the shopping cart rules when looking at your cart orchecking out.

  • app/code/core/Mage/SalesRule/Model/Validator.php
  • app/code/core/Mage/Adminhtml/Block/Promo/Quote/Edit/Tab/Actions

The good thing is each of these processes has an event we canobserve to handle our custom code.  That way wedon’t have to override any files which is always a bonus.

The two events we want to observe are:

  • adminhtml_block_salesrule_actions_prepareform
  • salesrule_validator_process

Starting with your custom module we’ll need a minimum of twofiles to handle this operation.

  • app/code/local/Namespace/Module/etc/config.xml
  • app/code/local/Namespace/Module/Model/Observer.php
Add two new events in
app/code/local/Namespace/Module/etc/config.xml
<?xml version="1.0"?>
<config>
    <modules>
       <NameSpace_Module>
          <version>0.1.0</version>
       </NameSpace_Module>
    </modules>

    <global>
       <models>
          <module>
              <class>NameSpace_Module_Model</class>
          </module>
       </models>
    </global>
    <adminhtml>
       <events>
          <adminhtml_block_salesrule_actions_prepareform>
              <observers>
                 <module>
                    <class>module/observer</class>
                    <method>prepareForm</method>
                 </module>
              </observers>
          </adminhtml_block_salesrule_actions_prepareform>
       </events>
    </adminhtml>
    <frontend>
       <events>
          <salesrule_validator_process>
              <observers>
                 <module>
                    <class>module/observer</class>
                    <method>process</method>
                 </module>
              </observers>
          </salesrule_validator_process>
       </events>
    </frontend>
</config>
Now create your custom logic in
app/code/local/Namespace/Module/Model/Observer.php
<?php
classNameSpace_Module_Model_Observer
    extends Mage_Core_Model_Abstract
{
    // {{{ prepareForm()

   
    public functionprepareForm(Varien_Event_Observer$observer)
    {
       $form = $observer->getForm();
       $simpleAction= $form->getElement('simple_action');
       $values = $simpleAction->getValues();
       $values[] =array(
          'value' => 'new_rule',
          'label' => 'NewRule',
       );
       $simpleAction->setValues($values);
    }

    // }}}
    // {{{ process()

   
    public functionprocess(Varien_Event_Observer$observer)
    {
       $qty   = $observer->getQty();
       $rule  = $observer->getRule();
       $item  = $observer->getItem();
       $result = $observer->getResult();

       $x = $rule->getDiscountStep();
       $y = $rule->getDiscountAmount();

       if (!$x){
          return;
       }

       $full = $discount= 0;
       while ($full + $discount < $qty) {
          if (++$full< $x) {
              continue;
          }

          ++$discount;
       }

       $itemPrice = $item->getDiscountCalculationPrice();
       if ($itemPrice !== null) {
          $baseItemPrice =$item->getBaseDiscountCalculationPrice();
       } else {
          $itemPrice =$item->getCalculationPrice();
          $baseItemPrice =$item->getBaseCalculationPrice();
       }

       $rulePercent= min(100, $rule->getDiscountAmount());

       $discountAmount    =($discount*$itemPrice- $item->getDiscountAmount())*$rulePercent/ 100;
       $baseDiscountAmount= ($discount* $baseItemPrice - $item->getBaseDiscountAmount())*$rulePercent/ 100;

       $result->setDiscountAmount($discountAmount);
       $result->setBaseDiscountAmount($baseDiscountAmount);
    }

    // }}}
}
原创粉丝点击