How to create custom price in Magento?
2012-12-26 18:53
501 查看
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.
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
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);
}
// }}}
}
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.
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
![](http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif)
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);
}
// }}}
}
相关文章推荐
- How to create custom methods for use in spring security expression language annotations
- How to create your own custom 404 error page and handle redirect in SharePoint 2007 (MOSS)?
- 转:How to create your own custom 404 error page and handle redirect in SharePoint 2007 (MOSS)?
- How to add custom fields to Customer Registration and Account page in magento
- magento如何获取产品属性值 How to Obtain Custom Attribute Values in Magento
- How to create custom snippet in Visual studio
- How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/
- magento如何获取产品属性值 How to Obtain Custom Attribute Values in Magento
- How to create your own api with ACL in Magento
- How to create your own custom 404 error page and handle redirect in SharePoint
- How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/
- How to Create Custom Filters in AngularJs
- How to create custom navigation menu in SharePoint with XML data source 使用XML数据源在SharePoint创建自定义导航菜单
- How to create custom html helper in Asp.net MVC 3 or 4
- How to create custom navigation menu in SharePoint with XML data source 使用XML数据源在SharePoint创建自定义导航菜单
- How to create custom shortcut in visual studio
- [转]How to Create Custom Filters in AngularJs
- How to change the price range in layered navigation in Magento? - See more at: http://www.expertweba
- How to create coupon codes in Magento – Beginner’s Tutorial
- How to Create Custom Window Title in Android