3

I want to skip payment method step when Grandtotal is zero

Any help would be appreciated.

Thanks!!

4

2 Answers 2

2
+100

Try this!

<?php
require_once(Mage::getModuleDir('controllers','Mage_Checkout').DS.'OnepageController.php');

class SR_MagentoCommunity_OnepageController extends Mage_Checkout_OnepageController
{
    /**
     * Save checkout billing address
     */
    public function saveBillingAction()
    {
        if ($this->_expireAjax()) {
            return;
        }

        if ($this->isFormkeyValidationOnCheckoutEnabled() && !$this->_validateFormKey()) {
            return;
        }

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost('billing', array());
            $customerAddressId = $this->getRequest()->getPost('billing_address_id', false);

            if (isset($data['email'])) {
                $data['email'] = trim($data['email']);
            }
            $result = $this->getOnepage()->saveBilling($data, $customerAddressId);

            if (!isset($result['error'])) {
                if ($this->getOnepage()->getQuote()->isVirtual()) {
                    if ($this->getOnepage()->getQuote()->getGrandTotal() <= 0) {
                        $paymentData = array('method' => 'free');
                        $this->getOnepage()->savePayment($paymentData);
                        $this->loadLayout('checkout_onepage_review');
                        $result['goto_section'] = 'review';
                        $result['update_section'] = array(
                            'name' => 'review',
                            'html' => $this->_getReviewHtml()
                        );
                    } else {
                        $result['goto_section'] = 'payment';
                        $result['update_section'] = array(
                            'name' => 'payment-method',
                            'html' => $this->_getPaymentMethodsHtml()
                        );
                    }
                } elseif (isset($data['use_for_shipping']) && $data['use_for_shipping'] == 1) {
                    $result['goto_section'] = 'shipping_method';
                    $result['update_section'] = array(
                        'name' => 'shipping-method',
                        'html' => $this->_getShippingMethodsHtml()
                    );

                    $result['allow_sections'] = array('shipping');
                    $result['duplicateBillingInfo'] = 'true';
                } else {
                    $result['goto_section'] = 'shipping';
                }
            }

            $this->_prepareDataJSON($result);
        }
    }

    /**
     * Shipping method save action
     */
    public function saveShippingMethodAction()
    {
        if ($this->_expireAjax()) {
            return;
        }

        if ($this->isFormkeyValidationOnCheckoutEnabled() && !$this->_validateFormKey()) {
            return;
        }

        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost('shipping_method', '');
            $result = $this->getOnepage()->saveShippingMethod($data);
            // $result will contain error data if shipping method is empty
            if (!$result) {
                Mage::dispatchEvent(
                    'checkout_controller_onepage_save_shipping_method',
                     array(
                          'request' => $this->getRequest(),
                          'quote'   => $this->getOnepage()->getQuote()));
                $this->getOnepage()->getQuote()->collectTotals();
                $this->_prepareDataJSON($result);
                if ($this->getOnepage()->getQuote()->getGrandTotal() <= 0) {
                    $paymentData = array('method' => 'free');
                    $this->getOnepage()->savePayment($paymentData);
                    $this->loadLayout('checkout_onepage_review');
                    $result['goto_section'] = 'review';
                    $result['update_section'] = array(
                        'name' => 'review',
                        'html' => $this->_getReviewHtml()
                    );
                } else {
                    $result['goto_section'] = 'payment';
                    $result['update_section'] = array(
                        'name' => 'payment-method',
                        'html' => $this->_getPaymentMethodsHtml()
                    );
                }
            }
            $this->getOnepage()->getQuote()->collectTotals()->save();
            $this->_prepareDataJSON($result);
        }
    }

    /**
     * Create order action
     */
    public function saveOrderAction()
    {
        $this->getOnepage()->getQuote()->collectTotals()->save();
        return parent::saveOrderAction();
    }
}

Overwrite Mage_Checkout_Block_Onepage_Payment class:

<blocks>
    <checkout>
        <rewrite>
            <onepage_payment>SR_MagentoCommunity_Block_Onepage_Payment</onepage_payment>
        </rewrite>
    </checkout>
</blocks>

app/code/local/SR/MagentoCommunity/Block/Onepage/Payment.php

<?php

class SR_MagentoCommunity_Block_Onepage_Payment extends Mage_Checkout_Block_Onepage_Payment
{
    /**
     * Retrieve is allow and show block
     *
     * @return bool
     */
    public function isShow()
    {

        if ($this->getGrandTotal() <= 0) {
            return false;
        }

        return true;
    }

    private function getGrandTotal()
    {
        return Mage::getSingleton('checkout/type_onepage')->getQuote()->getGrandTotal();
    }
}

Finally, overwrite

skin/frontend/base/default/js/opcheckout.js

ShippingMethod.prototype.nextStep = function(transport){
    var response = transport.responseJSON || transport.responseText.evalJSON(true) || {};

    if (response.error) {
        alert(response.message.stripTags().toString());
        return false;
    }

    if (response.update_section) {
        $('checkout-'+response.update_section.name+'-load').update(response.update_section.html);
    }

    if (response.goto_section) {
        checkout.gotoSection(response.goto_section, true);
        checkout.reloadProgressBlock();
        return;
    }

    payment.initWhatIsCvvListeners();

    if (response.payment_methods_html) {
        $('checkout-payment-method-load').update(response.payment_methods_html);
    }

    checkout.setShippingMethod();
};

Review.prototype.save = function(){
    if (checkout.loadWaiting!=false) return;
    checkout.setLoadWaiting('review');
    if (!$('#co-payment-form')) {
        var params = 'method=free';
    } else {
        var params = Form.serialize(payment.form);
    }


    if (this.agreementsForm) {
        params += '&'+Form.serialize(this.agreementsForm);
    }
    params.save = true;
    new Ajax.Request(
        this.saveUrl,
        {
            method:'post',
            parameters:params,
            onComplete: this.onComplete,
            onSuccess: this.onSave,
            onFailure: checkout.ajaxFailure.bind(checkout)
        }
    );
};

[Update]

How you overwrite opcheckout.js?

skin/frontend/base/default/js/rewriteopcheckout.js

ShippingMethod.prototype.nextStep = function(transport){
    var response = transport.responseJSON || transport.responseText.evalJSON(true) || {};

    if (response.error) {
        alert(response.message.stripTags().toString());
        return false;
    }

    if (response.update_section) {
        $('checkout-'+response.update_section.name+'-load').update(response.update_section.html);
    }

    if (response.goto_section) {
        checkout.gotoSection(response.goto_section, true);
        checkout.reloadProgressBlock();
        return;
    }

    payment.initWhatIsCvvListeners();

    if (response.payment_methods_html) {
        $('checkout-payment-method-load').update(response.payment_methods_html);
    }

    checkout.setShippingMethod();
};

Review.prototype.save = function(){
    if (checkout.loadWaiting!=false) return;
    checkout.setLoadWaiting('review');
    if (!$('#co-payment-form')) {
        var params = 'method=free';
    } else {
        var params = Form.serialize(payment.form);
    }


    if (this.agreementsForm) {
        params += '&'+Form.serialize(this.agreementsForm);
    }
    params.save = true;
    new Ajax.Request(
        this.saveUrl,
        {
            method:'post',
            parameters:params,
            onComplete: this.onComplete,
            onSuccess: this.onSave,
            onFailure: checkout.ajaxFailure.bind(checkout)
        }
    );
};

Now open checkout/onepage.phtml template in your current theme. Ex. my case is default rwd/default.

app/design/frontend/rwd/default/template/checkout/onepage.phtml

Add following line:

<script type="text/javascript" src="<?php echo $this->getSkinUrl('js/rewriteopcheckout.js') ?>"></script>

enter image description here

I have tested with M1.9.4.1 with the fresh installation with sample data.

14
  • So you dont want to show payment step?
    – Sohel Rana
    May 27, 2019 at 4:35
  • yes right i want to payment step skip when grand total is zero May 27, 2019 at 4:36
  • Check updated answer
    – Sohel Rana
    May 27, 2019 at 5:50
  • you have tested your code? when grand total is zero then above code is working? May 27, 2019 at 5:54
  • Did you checked that? working fine 1.9.4.1
    – Sohel Rana
    May 27, 2019 at 6:17
0

Affected file for above described problem is

{Magento-Root-Folder}/app/design/frontend/{YourTheme}/default/template/checkout/onepage/payment/methods.phtml

note that if folder 'checkout' is not available in your template folder, make sure to copy it from {Magento-Root-Folder}/app/design/frontend/base/default/template/checkout to your template folder; make sure you keep the structure identical

inside this file (methods.phtml) we actually look through available payments (around line 45), this is where we need to adjust a little.

1) place this line anywhere on top of your file to get current quote data (we will need it in step 2)

$quote_data = Mage::helper('checkout')->getQuote()->getData();

it will contain $quote_data[grand_total]

2) search for the payment methods iterating around line 45

foreach ($methods as $_method):
    $_code = $_method->getCode();

we need to check $_code ('free' means Zero Subtotal Checkout) and add a simple if clause inside the same foreach:

if ($_code == "free" && $quote_data[grand_total] < 1) { // if payment method is free and grand total '0'
    // this is where your action should happen
    $class_show_pm = ""; // will be used to show/hide 'dt' tag
    $radio_checked = "checked"; // will be used to enable/disable radio button
    $radio_onclick = "payment.switchMethod(" .$_code .")"; // will be used to load js code
} else {
    $class_show_pm = "none";
    $radio_checked = "";
    $radio_onclick = "";
}

You can check out this Skip Payment Step if Grand Total 0 too

I hope this helps

1
  • Yes you can give it a try
    – surbhi agr
    May 14, 2019 at 11:46

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.