cancel
Showing results for 
Search instead for 
Did you mean: 

How to apply magento default function to third party application?

SOLVED

How to apply magento default function to third party application?

I am using this extension for extra fees collect from customer. This extension done with shopping cart pricing rule. Here i need to apply my condition.

 

My condition :

If shipping method is flate rate & website = 2 the extra fees option not showing to customer.

 

how can i apply the above condition in app/code/local/Voronoy/ExtraFee/Block/Sales/Order/Totals/Rule.php

 

Code for getting shipping method & set website = 2

 

$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');

 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to apply magento default function to third party application?

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.

 

View solution in original post

26 REPLIES 26

Re: How to apply magento default function to third party application?

Hi @Aveeva 

You can first get the website id like this
$websiteId = Mage::app()->getStore()->getWebsiteId();
and in your file app/code/local/Voronoy/ExtraFee/Block/Sales/Order/Totals/Rule.php
$this->getSource() is returning the current order, so we can fetch the shipping method from here

$shippingMethod = $this->getSource()->getShippingMethod();



now you can add if condition

if($shippingMethod=='flatrate_flatrate' && $websiteId=="2")
     return $this;



this will skip adding extra fee for above mentioned condition

use this code in initTotals() of your file and add this in very first line of your code
you can check the exact string of shipping method it may be either flatrate or flatrate_flatrate

Re: How to apply magento default function to third party application?

@Govinda Sharma Here is my Rule.php

 

    public function initTotals()
    {
        ### start custom condition ###

        $websiteId = Mage::app()->getStore()->getWebsiteId();
        $shippingMethod = $this->getSource()->getShippingMethod();

        if($shippingMethod=='flatrate' && $websiteId=="2") {
        return $this;
        }

        ### end custom condition ###

        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;
    }

Is above workout right?

Re: How to apply magento default function to third party application?

Hello @Aveeva ,

You can override the file app/code/local/Voronoy/ExtraFee/Block/Sales/Order/Totals/Rule.php

in your custom module and use the below code for it :

<?php

class VendorName_CustomModule_Block_Sales_Order_Totals_Rule 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;
        }
        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;
    }
}

I hope this will you if still you face any issue, please revert us.

Thank you.

Re: How to apply magento default function to third party application?

I am new to module development, how can i achieve this?

Re: How to apply magento default function to third party application?

If i wrong, pls correct me, 

 

Put your last code to -> app/code/local/MyCompanyName/MyModuleName/Block/Sale/Order/Total/Rule/Rule.php

 

and paste ->

 

<?php

class MyCompanyName_MyModuleName_Block_Sales_Order_Totals_Rule 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;
        }
        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;
    }
}

am i right?

Re: How to apply magento default function to third party application?

yes it is right.

Re: How to apply magento default function to third party application?

Yes it is correct. You can also check this link https://magento.stackexchange.com/questions/76804/problems-overriding-3rd-party-extension-block-clas...

I hope it will help you Smiley Happy

Re: How to apply magento default function to third party application?

Finally,

 

app/code/local/Gta/ExtraFeesRemoveAus/Block/Sales/Order/Totals/Rule/Rule.php

 

 

<?php

class Gta_ExtraFeesRemoveAus_Block_Sales_Order_Totals_Rule 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;
        }
        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 choose pick from store [ Flate Rate ] still showing extra fees,

 

screenshot : https://snag.gy/X6gQiu.jpg

 

How can i solve the issue?

Re: How to apply magento default function to third party application?

Hello @Aveeva ,

I hope you have already create the following file (as provide in the link https://magento.stackexchange.com/questions/76804/problems-overriding-3rd-party-extension-block-clas...) :

  • Activation File: app/etc/modules/Gta_ExtraFeesRemoveAus.xml
  • Config file: app/code/local/Gta/ExtraFeesRemoveAus/etc/config.xml

After that you need to flush the cache of magento

You can either flush the cache from admin section (admin ->System -> Cache Management -> Flush Cache Storage)

Or you can simply remove the var/cache folder simply by command line(from magento root directory):

rm -rf var/cache/*

Then you can check the installed module/extension in backend system->advance

After that check it again on frontend.

Hope this will help you