FYI -> Actually i tried for Observer, make sure your guide also Observer, if observer means what is my event,
<events> <salesrule_rule_save_before> <observers> <Gta_ExtraFeesRemove_Model_Observer> <type>singleton</type> <class>Gta_ExtraFeesRemove_Model_Observer</class> <method>beforecheckout</method> </Gta_ExtraFeesRemove_Model_Observer> </observers> </salesrule_rule_save_before> </events>
does my events right? <salesrule_rule_save_before>
Shall i use your code into my app/code/local/Gta/ExtraFeesRemove/Model/Observer.php
<?php class Gta_ExtraFeesRemove_Model_Observer extends Mage_Core_Block_Abstract { /** * Get Source Model * * @return mixed */ public function getSource() { return $this->getParentBlock()->getSource(); } /** * Add this total to parent */ public function initTotals() { $current_website = Mage::app()->getWebsite()->getId(); $shipping_method = $this->getSource()->getShippingMethod(); //Check for shipping method and website if($current_website == 2 && $shipping_method == 'flatrate'){ return $this; } /* start custom code */ //if (Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod() === 'flatrate') { //return $this; //} /* end custom code */ if ((float) $this->getSource()->getExtraFeeRuleAmount() <= 0) { return $this; } if ($this->getSource()->getExtraFeeRuleDescription()) { $discountLabel = $this->__('%s (%s)', Mage::helper('voronoy_extrafee')->getExtraFeeRuleLabel(), $this->getSource()->getExtraFeeRuleDescription()); } else { $discountLabel = Mage::helper('voronoy_extrafee')->getExtraFeeRuleLabel(); } $total = new Varien_Object(array( 'code' => 'extra_fee_rule', 'field' => 'extra_fee_rule_amount', 'value' => $this->getSource()->getExtraFeeRuleAmount(), 'label' => $discountLabel )); $this->getParentBlock()->addTotalBefore($total, 'grand_total'); return $this; } }
If i wrong way pls correct me.. Thanks
Hello @Aveeva ,
You can use the below code for observer:
<?php class Gta_ExtraFeesRemove_Model_Observer { public function beforecheckout($observer) { $quote = $observer->getEvent()->getQuote(); $shippingAddress = $quote->getShippingAddress(); if (!$shippingAddress instanceof Mage_Sales_Model_Quote_Address) { return; } $currentShippingMethod = $shippingAddress->getShippingMethod(); $current_website = Mage::app()->getWebsite()->getId(); //Check for shipping method and website if($current_website == 2 && $currentShippingMethod == 'flatrate_flatrate'){ return $this; } } }
Hope it will work.
My complete Observer :
app/etc/modules/Gta_ExtraFeesRemove.xml
<?xml version="1.0"?> <config> <modules> <Gta_ExtraFeesRemove> <codePool>local</codePool> <active>true</active> </Gta_ExtraFeesRemove> </modules> </config>
app/code/local/Gta/ExtraFeesRemove/etc/config.xml
<?xml version="1.0"?> <config> <modules> <Gta_ExtraFeesRemove> <version>1.0.0</version> </Gta_ExtraFeesRemove> </modules> <global> <models> <gta_extrafeesremove> <class>Gta_ExtraFeesRemove_Model</class> </gta_extrafeesremove> </models> </global> <frontend> <events> <salesrule_rule_save_before> <observers> <Gta_ExtraFeesRemove_Model_Observer> <type>singleton</type> <class>Gta_ExtraFeesRemove_Model_Observer</class> <method>beforecheckout</method> </Gta_ExtraFeesRemove_Model_Observer> </observers> </salesrule_rule_save_before> </events> </frontend> </config>
app/code/local/Gta/ExtraFeesRemove/Model/Observer.php
<?php class Gta_ExtraFeesRemove_Model_Observer { public function beforecheckout($observer) { $quote = $observer->getEvent()->getQuote(); $shippingAddress = $quote->getShippingAddress(); if (!$shippingAddress instanceof Mage_Sales_Model_Quote_Address) { return; } $currentShippingMethod = $shippingAddress->getShippingMethod(); $current_website = Mage::app()->getWebsite()->getId(); //Check for shipping method and website if($current_website == 2 && $currentShippingMethod == 'flatrate') { return $this; } } }
FYI -> I found my available shipping method by following way,
<?php require_once('app/Mage.php'); umask(0); Mage::app(); $website_id = Mage::app()->getWebsite(2)->getDefaultGroup()->getDefaultStoreId(); Mage::app()->setCurrentStore($website_id); $methods = Mage::getSingleton('shipping/config')->getActiveCarriers(); $shipMethods = array(); foreach ($methods as $shippigCode=>$shippingModel) { $shippingTitle = Mage::getStoreConfig('carriers/'.$shippigCode.'/title'); $shipMethods[$shippigCode] = $shippingTitle; } Mage::log($shipMethods, null, 'logfile.log'); ?>
output :
( [flatrate] => Pick From Store [m2eproshipping] => M2E Pro Shipping [sendle] => Sendle Shipping Method [customshipprice] => Custom Shipping Price )
@Sarvagya Pandey Anything else i need to update?
Hello @Aveeva ,
Actually salesrule_rule_save_before is not triggered. So please override the file https://github.com/yvoronoy/magento-extension-extra-fee/blob/master/app/code/local/Voronoy/ExtraFee/...
and update the collect function as :
public function collect(Mage_Sales_Model_Quote_Address $address) { if (!Mage::helper('voronoy_extrafee')->isRuleExtraFeeEnabled()) { return $this; } parent::collect($address); $quote = $address->getQuote(); $shipping_method = $quote->getShippingAddress()->getShippingMethod(); /* Flat Rate Condition Start*/ if($shipping_method != 'flatrate_flatrate'): $store = Mage::app()->getStore($quote->getStoreId()); $this->_calculator->reset($address); $items = $this->_getAddressItems($address); if (!count($items)) { return $this; } $this->_calculator->init($store->getWebsiteId(), $quote->getCustomerGroupId(), $quote->getCouponCode()); $this->_calculator->initTotals($items, $address); $items = $this->_calculator->sortItemsByPriority($items); foreach ($items as $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->_calculator->prepareDescription($address); endif; / Flat Rate Condition End / }
I hope this will work for you.
@Sarvagya Pandey Just for clarification, where i put above code? in observer?
No, firstly please apply the code in app/code/local/Voronoy/ExtraFee/Model/Quote/Address/Total/Fee/Rule.php#L44 and test that is it works for you.
If it works then please create a custom module and override this file in you module.
For model rewrite in custom module you can take help from https://code.tutsplus.com/tutorials/understand-overriding-in-magento-models--cms-23354
Hope this will help you.
app/code/local/Voronoy/ExtraFee/Model/Quote/Address/Total/Fee/Rule.php
<?php /** * Magento Extra Fee Extension * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * @copyright Copyright (c) 2015 by Yaroslav Voronoy (y.voronoy@gmail.com) * @license http://www.gnu.org/licenses/ */ class Voronoy_ExtraFee_Model_Quote_Address_Total_Fee_Rule extends Mage_Sales_Model_Quote_Address_Total_Abstract { /** * Discount calculation object * * @var Mage_SalesRule_Model_Validator */ protected $_calculator; /** * Initialize discount collector */ public function __construct() { $this->_calculator = Mage::getSingleton('voronoy_extrafee/salesRule_validator'); } /** * @param Mage_Sales_Model_Quote_Address $address * * @return Mage_Sales_Model_Quote_Address_Total_Abstract */ public function collect(Mage_Sales_Model_Quote_Address $address) { if (!Mage::helper('voronoy_extrafee')->isRuleExtraFeeEnabled()) { return $this; } parent::collect($address); $quote = $address->getQuote(); $shipping_method = $quote->getShippingAddress()->getShippingMethod(); /* Flat Rate Condition Start*/ if($shipping_method != 'flatrate'): $store = Mage::app()->getStore($quote->getStoreId()); $this->_calculator->reset($address); $items = $this->_getAddressItems($address); if (!count($items)) { return $this; } $this->_calculator->init($store->getWebsiteId(), $quote->getCustomerGroupId(), $quote->getCouponCode()); $this->_calculator->initTotals($items, $address); $items = $this->_calculator->sortItemsByPriority($items); foreach ($items as $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->_calculator->prepareDescription($address); endif; // / Flat Rate Condition End / } /** * Fetch Totals * * @param Mage_Sales_Model_Quote_Address $address * * @return Voronoy_ExtraFee_Model_Quote_Address_Total_Fee_Rule */ public function fetch(Mage_Sales_Model_Quote_Address $address) { if (!Mage::helper('voronoy_extrafee')->isRuleExtraFeeEnabled()) { return $this; } $amount = $address->getExtraFeeRuleAmount(); if ($address->getExtraFeeRuleDescription()) { $discountLabel = Mage::helper('voronoy_extrafee')->__('%s (%s)', Mage::helper('voronoy_extrafee')->getExtraFeeRuleLabel(), $address->getExtraFeeRuleDescription()); } else { $discountLabel = Mage::helper('voronoy_extrafee')->getExtraFeeRuleLabel(); } if ($amount > 0) { $address->addTotal(array( 'code' => $this->getCode(), 'title' => $discountLabel, 'value' => $amount )); } return $this; } }
still not working.