I want to skip payment method step when Grandtotal is zero
Any help would be appreciated.
Thanks!!
-
If your Grand Total is zero, surely your Sub Total would be zero too correct? That being the case, can you not simply enable Zero Subtotal Checkout in System > Configuration, then in the Payment Methods under the Sales tab in the left hand menu choose Zero Subtotal Checkout? I appreciate it doesnt "skip" the step, but that in itself is a re-confirmation to the user that they do not have to pay anything. – Tony PollardMay 21, 2019 at 22:45
-
yes i know that but here payment method not skip– Rakesh DongaMay 22, 2019 at 4:37
-
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review– Teja Bhagavan KolleparaMay 23, 2019 at 8:16
-
At what scenario the Grand Total will be zero?. Like based on product item or sub total?– Ejilarasan JMay 26, 2019 at 11:02
2 Answers
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>
I have tested with M1.9.4.1 with the fresh installation with sample data.
-
-
-
-
you have tested your code? when grand total is zero then above code is working? May 27, 2019 at 5:54
-
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