Skip to content

Commit

Permalink
Implemented Extra Fee Rule for Whole Cart
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroslav Voronoy committed Sep 24, 2015
1 parent 520e3bb commit 9cff1ca
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
5 changes: 4 additions & 1 deletion app/code/local/Voronoy/ExtraFee/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ public function prepareFormSalesRuleEdit($observer)
->setChild('form_after', Mage::app()->getLayout()->createBlock('adminhtml/widget_form_element_dependence')
->addFieldMap('rule_extra_fee_amount', 'extra_fee_amount')
->addFieldMap('rule_simple_action', 'simple_action')
->addFieldDependence('extra_fee_amount', 'simple_action', array('by_percent', 'by_fixed'))
->addFieldDependence('extra_fee_amount', 'simple_action', array(
Mage_SalesRule_Model_Rule::BY_PERCENT_ACTION,
Mage_SalesRule_Model_Rule::BY_FIXED_ACTION,
Mage_SalesRule_Model_Rule::CART_FIXED_ACTION))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,23 @@ public function collect(Mage_Sales_Model_Quote_Address $address)
$this->_calculator->initTotals($items, $address);

$items = $this->_calculator->sortItemsByPriority($items);

foreach ($items as $item) {
$this->_calculator->process($item);
if ($item->getParentItemId()) {
continue;
}
if ($item->getHasChildren() && $item->isChildrenCalculated()) {
foreach ($item->getChildren() as $child) {
$this->_calculator->process($child);
$this->_addAmount($child->getExtraFeeRuleAmount());
$this->_addBaseAmount($child->getBaseExtraFeeRuleAmount());
}
} else {
$this->_calculator->process($item);
$this->_addAmount($item->getExtraFeeRuleAmount());
$this->_addBaseAmount($item->getBaseExtraFeeRuleAmount());
}
}

$this->_addAmount($item->getExtraFeeRuleAmount());
$this->_addBaseAmount($item->getBaseExtraFeeRuleAmount());
$this->_calculator->prepareDescription($address);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Voronoy_ExtraFee_Model_SalesRule_Discount_Validator extends Mage_SalesRule
*/
protected function _canProcessRule($rule, $address)
{
if (!Mage::helper('voronoy_extrafee')->isRuleExtraFeeEnabled()) {
return parent::_canProcessRule($rule, $address);
}
if ($rule->getDiscountAmount() == 0) {
return false;
}
Expand Down
44 changes: 44 additions & 0 deletions app/code/local/Voronoy/ExtraFee/Model/SalesRule/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,50 @@ public function process(Mage_Sales_Model_Quote_Item_Abstract $item)
$quoteAmount = $quote->getStore()->convertPrice($rule->getExtraFeeAmount());
$extraFeeAmount = $qty * $quoteAmount;
$baseExtraFeeAmount = $qty * $rule->getExtraFeeAmount();
break;
case Mage_SalesRule_Model_Rule::CART_FIXED_ACTION:
if (empty($this->_rulesItemTotals[$rule->getId()])) {
Mage::throwException(Mage::helper('salesrule')->__('Item totals are not set for rule.'));
}

/**
* prevent applying whole cart discount for every shipping order, but only for first order
*/
if ($quote->getIsMultiShipping()) {
$usedForAddressId = $this->getCartFixedRuleUsedForAddress($rule->getId());
if ($usedForAddressId && $usedForAddressId != $address->getId()) {
break;
} else {
$this->setCartFixedRuleUsedForAddress($rule->getId(), $address->getId());
}
}
$cartRules = $address->getCartFixedRules();
if (!isset($cartRules[$rule->getId()])) {
$cartRules[$rule->getId()] = $rule->getExtraFeeAmount();
}

if ($cartRules[$rule->getId()] > 0) {
if ($this->_rulesItemTotals[$rule->getId()]['items_count'] <= 1) {
$quoteAmount = $quote->getStore()->convertPrice($cartRules[$rule->getId()]);
$baseExtraFeeAmount= min($baseItemPrice * $qty, $cartRules[$rule->getId()]);
} else {
$discountRate = $baseItemPrice * $qty /
$this->_rulesItemTotals[$rule->getId()]['base_items_price'];
$maximumItemDiscount = $rule->getDiscountAmount() * $discountRate;
$quoteAmount = $quote->getStore()->convertPrice($maximumItemDiscount);

$baseExtraFeeAmount = min($baseItemPrice * $qty, $maximumItemDiscount);
$this->_rulesItemTotals[$rule->getId()]['items_count']--;
}

$extraFeeAmount = min($itemPrice * $qty, $quoteAmount);
$extraFeeAmount = $quote->getStore()->roundPrice($extraFeeAmount);
$baseExtraFeeAmount = $quote->getStore()->roundPrice($baseExtraFeeAmount);

$cartRules[$rule->getId()] += $baseExtraFeeAmount;
}
$address->setCartFixedRules($cartRules);

break;
}

Expand Down

0 comments on commit 9cff1ca

Please sign in to comment.