public function collect(Mage_Sales_Model_Quote_Address $address) { parent::collect($address); $this->_setAmount(0); $this->_setBaseAmount(0); $items = $this->_getAddressItems($address); if (!count($items)) { return $this; //this makes only address type shipping to come through } $quote = $address->getQuote(); if (Excellence_Fee_Model_Fee::canApply($address)) { $exist_amount = $quote->getFeeAmount(); $fee = Excellence_Fee_Model_Fee::getFee(); $balance = $fee - $exist_amount; // $balance = $fee; //$this->_setAmount($balance); //$this->_setBaseAmount($balance); $address->setFeeAmount($balance); $address->setBaseFeeAmount($balance); $quote->setFeeAmount($balance); $address->setGrandTotal($address->getGrandTotal() + $address->getFeeAmount()); $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getBaseFeeAmount()); } }
public function collect(Mage_Sales_Model_Quote_Address $address) { parent::collect($address); $this->_setAmount(0); $this->_setBaseAmount(0); $items = $this->_getAddressItems($address); if (!count($items)) { return $this; //this makes only address type shipping to come through } $quote = $address->getQuote(); if (Excellence_Fee_Model_Fee::canApply($address)) { $exist_amount = $quote->getFeeAmount(); //$fee = Excellence_Fee_Model_Fee::getFee() $quote = Mage::getModel('checkout/cart')->getQuote(); $items = $quote->getAllVisibleItems(); foreach ($items as $item) { $product = $item->getProduct(); $categories = $product->getCategoryCollection()->addAttributeToSelect('name')->addAttributeToFilter('is_active', array('eq' => 1)); foreach ($categories as $cat) { $catid[] = $cat->getId(); } } //echo "<pre>"; //print_r(array_count_values($catid)); $cat_array = array_count_values($catid); $total_handling_fee = 0; foreach (array_count_values($catid) as $catid => $catcont) { //echo $catid . " == ". $catcont."<pre>"; for ($i = 1; $i <= $catcont; $i++) { $h_fee = Mage::getModel('catalog/category')->load($catid)->getData('handling_fee' . $i); $total_handling_fee += $h_fee; } } //echo "Total Fee: " .$total_handling_fee; //die(); $config_option = Mage::app()->getStore()->getConfig('tab1/general/max_handling_fee'); if ($total_handling_fee > $config_option) { $final_total_handling_fee = $config_option; } else { $final_total_handling_fee = $total_handling_fee; } $fee = $final_total_handling_fee; $balance = $fee - $exist_amount; // $balance = $fee; //$this->_setAmount($balance); //$this->_setBaseAmount($balance); $address->setFeeAmount($balance); $address->setBaseFeeAmount($balance); $quote->setFeeAmount($balance); $address->setGrandTotal($address->getGrandTotal() + $address->getFeeAmount()); $address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getBaseFeeAmount()); } }
public function setDiscount($observer) { try { $array = array(); Mage::log(" we zijn in setDiscount ", null, 'custom.log'); $quote = $observer->getEvent()->getQuote(); $quoteId = $quote->getId(); $cartItems = $quote->getAllVisibleItems(); $obj = new Excellence_Fee_Model_Fee(); $customerId = Mage::getSingleton('customer/session')->getId(); $discountAmount = 0; $discountDescription = "Voor spaarkaart voor"; foreach ($cartItems as $item) { $productPrice = $item->getProduct()->getPrice(); $store = Mage::app()->getStore('default'); $request = Mage::getSingleton('tax/calculation')->getRateRequest(null, null, null, $store); $taxClassId = $item->getData('tax_class_id'); $percent = Mage::getSingleton('tax/calculation')->getRate($request->setProductClassId($taxClassId)); $tax = $percent * 0.01 * $productPrice; $totalAmount = $productPrice + $tax; Mage::log("mogelijk korting {$totalAmount} , product prijs {$productPrice} en btw is {$tax} ", null, 'custom.log'); $this->productId = $item->getProductId(); $_product = Mage::getModel('catalog/product')->load($item->getProductId()); $brand = $_product->getAttributeText('manufacturer'); $couponDao = new CouponDao(); $couponSaving = $couponDao->loadCouponSavingByOrderId($brand, $quoteId, $customerId); if ($couponSaving != null) { $discountAmount = $couponSaving->getDiscountAmount(); Mage::log("discountAmount is {$discountAmount}", null, 'custom.log'); } else { Mage::log("discountAmount is 0", null, 'custom.log'); } // if (!in_array($brand, $array)) { if ($couponSaving != null) { Mage::log("setDiscount : stap voor 1 ", null, 'custom.log'); if ($couponSaving->getDiscountAmount() > 0) { $discountDescription = "{$discountDescription} en {$brand}"; } else { $discountDescription = "{$discountDescription} voor {$brand}"; } //$exist_amount = $couponSaving->getDiscountAmount(); //$discountAmount = $exist_amount; array_push($array, $brand); } else { if ($obj->canApply($item, $quoteId, $totalAmount)) { Mage::log("setDiscount : stap voor 2 ", null, 'custom.log'); $brand = $obj->getBrand(); array_push($array, $brand); Mage::log(" in canApply check for setDiscount {$totalAmount}", null, 'custom.log'); $discountAmount = $discountAmount + $totalAmount; if ($discountAmount > 0) { $discountDescription = "{$discountDescription} en {$brand}"; } else { $discountDescription = "{$discountDescription} voor {$brand}"; } } } //} } if ($quoteId) { Mage::log("setDiscount : stap 1 ", null, 'custom.log'); if ($discountAmount > 0) { Mage::log("setDiscount : stap 2 ", null, 'custom.log'); //we calculate the Ratio of taxes between GrandTotal & Discount Amount to know how much we need to remove. $quote->setGrandTotal($quote->getGrandTotal() - $discountAmount)->setBaseGrandTotal($quote->getBaseGrandTotal() - $discountAmount)->setSubtotalWithDiscount($quote->getBaseSubtotal() - $discountAmount)->setBaseSubtotalWithDiscount($quote->getBaseSubtotal() - $discountAmount)->setBaseGrandTotal((double) $quote->getBaseGrandTotal())->setDiscountAmount($discountAmount)->setDiscountDescription($discountDescription)->setBaseDiscountAmount($discountAmount)->save(); $canAddItems = $quote->isVirtual() ? 'billing' : 'shipping'; foreach ($quote->getAllAddresses() as $address) { $address->setSubtotal(0); $address->setBaseSubtotal(0); $address->setGrandTotal(0); $address->setBaseGrandTotal(0); $address->collectTotals(); if ($address->getAddressType() == $canAddItems) { $address->setSubtotal((double) $quote->getSubtotal()); $address->setBaseSubtotal((double) $quote->getBaseSubtotal()); $address->setSubtotalWithDiscount((double) $quote->getSubtotalWithDiscount()); $address->setBaseSubtotalWithDiscount((double) $quote->getBaseSubtotalWithDiscount()); $address->setGrandTotal((double) $quote->getGrandTotal()); $address->setBaseGrandTotal((double) $quote->getBaseGrandTotal()); $address->setDiscountAmount(-$discountAmount); $address->setDiscountDescription($discountDescription); $address->setBaseDiscountAmount(-$discountAmount); $address->setTaxAmount($tax); $address->setBaseTaxAmount($tax); $address->save(); Mage::log("setDiscount : stap 6 ", null, 'custom.log'); } //end: if } //end: foreach } } Mage::log("setDiscount : stap 8 ", null, 'custom.log'); } catch (Exception $e) { Mage::log("Set discount exception " . $e, null); } }