1

I want to offer free shipping on subtotal but after discount is applied. I have added a new price rule called grand total and using this as the condition in price rules but this does not work for me. Here is my code. Wondering where is my mistake.

<?php

class MY_SalesRule_Model_Rule_Condition_Address extends Mage_SalesRule_Model_Rule_Condition_Address {

    public function loadAttributeOptions()
    {
        //added the base_grandtotal
        $attributes = array(
            'base_subtotal' => Mage::helper('salesrule')->__('Subtotal'),
            'base_grandtotal' => Mage::helper('salesrule')->__('Grandtotal'),
            'total_qty' => Mage::helper('salesrule')->__('Total Items Quantity'),
            'weight' => Mage::helper('salesrule')->__('Total Weight'),
            'payment_method' => Mage::helper('salesrule')->__('Payment Method'),
            'shipping_method' => Mage::helper('salesrule')->__('Shipping Method'),
            'postcode' => Mage::helper('salesrule')->__('Shipping Postcode'),
            'region' => Mage::helper('salesrule')->__('Shipping Region'),
            'region_id' => Mage::helper('salesrule')->__('Shipping State/Province'),
            'country_id' => Mage::helper('salesrule')->__('Shipping Country'),
        );

        $this->setAttributeOption($attributes);

        return $this;
    }


    public function getInputType()
    {
        switch ($this->getAttribute()) {
            case 'base_subtotal': case 'base_grandtotal': case 'weight': case 'total_qty':
            return 'numeric';

            case 'shipping_method': case 'payment_method': case 'country_id': case 'region_id':
            return 'select';
        }
        return 'string';
    }


    public function validate(Varien_Object $object)
    {
        $address = $object;
        if (!$address instanceof Mage_Sales_Model_Quote_Address) {
            if ($object->getQuote()->isVirtual()) {
                $address = $object->getQuote()->getBillingAddress();
            }
            else {
                $address = $object->getQuote()->getShippingAddress();
            }
        }

        if ('payment_method' == $this->getAttribute() && ! $address->hasPaymentMethod()) {
            $address->setPaymentMethod($object->getQuote()->getPayment()->getMethod());
        }

        //The below is added for caculating the grandtotal
        $quote = Mage::getModel('checkout/session')->getQuote();
        $quoteData= $quote->getData();
        if( $quote && $quote->getData('base_grandtotal') ){
            $basegrandTotal = $quote->getData('base_grandtotal');
            if ('base_grandtotal' == $this->getAttribute() && ! $address->hasBaseGrandtotal()) {
                $address->setBaseGrandtotal($basegrandTotal);
            }
        }

        return parent::validate($address);
    }

}
1

I did this in Magento 2.2. a short while ago and it seems to be very similar to Magento 1.9 functionality.

From your code change the following lines:

From:

'base_grandtotal' => Mage::helper('salesrule')->__('Grandtotal'),

Change to:

'base_grand_total' => Mage::helper('salesrule')->__('Grandtotal'),

and this:

'case 'base_subtotal': case 'base_grandtotal': case 'weight': case 'total_qty':

to:

'case 'base_subtotal': case 'base_grand_total': case 'weight': case 'total_qty':

Also remove this, because it's not necessary:

$quote = Mage::getModel('checkout/session')->getQuote();
    $quoteData= $quote->getData();
    if( $quote && $quote->getData('base_grandtotal') ){
        $basegrandTotal = $quote->getData('base_grandtotal');
        if ('base_grandtotal' == $this->getAttribute() && ! $address->hasBaseGrandtotal()) {
            $address->setBaseGrandtotal($basegrandTotal);
        }
    }

Reason for the suggested changes is that the $address object already contains the grand total data, you just need to include it as an option, while the correct key is "base_grand_total".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.