Magento 0 RMB Order Payment Method--Magento 0 Subtotal Payment method

Source: Internet
Author: User

Demand

Existing shopping sites support 2 payment methods, but consider the following: In a promotional activity, if there are some orders in the total amount of 0, then these orders do not have to jump to the payment gateway, the existing payment method can not handle this situation.

Analysis

When the customer enters the order receipt information, click the Confirm button, the page will jump to select the Payment Method page, the default process is to give all payment methods directly to customer selection, but for the above requirements mentioned in the situation: if the total amount of the order is 0, Then you should not give all the payment methods directly, because there is no need to pay for it at all. the modified process should be to detect the total amount of the order first, if the total order amount equals 0, then only one payment method--free Payment payment method, for customer choice, free payment payment method without payment, The order information is sent directly to the server after the user agrees and clicks Checkout-agreements , and if the total order amount is greater than 0, all original payment methods are returned for customer's choice (the new free payment payment method should be removed).

Solution Solutions

After reviewing the source code of Magento's payment module, I found that Magento1.9 native support for 0 amount order payment (if you have experience, you do not need to look at the source), in the background system->configration->sales- >payment Methods->zero Subtotal Checkout can be configured to turn on this payment method.

The configuration options are as follows:

    • Name of the "Title" control front-end display
    • "New order Status" means the order status after payment is made in this manner
    • "Automatically Invoice all items": Automatically Invoice all items included in the order

By configuring these parameters, you can implement the requirements presented above.

SOURCE Analysis

The following is a record of my view of the Magento Payment module (Payment module) source code process:

Magento return payment method is in the URL is base_url/checkout/onepage/saveshippingmethod/, can be seen, This action is done in the Saveshippingmethodaction () method in Onepagecontroller.

1 //file:base_dir/app/code/core/mage/checkout/controllers/onepagecontroller.php2 /**3 * Shipping method Save action4  */5  Public functionsaveshippingmethodaction ()6 {7     if($this-_expireajax ()) {8         return;9     }Ten     if($this->getrequest ()IsPost ()) { One         $data=$this->getrequest ()->getpost (' Shipping_method ', '); A         $result=$this->getonepage ()->saveshippingmethod ($data); -         //$result would contain error data if shipping method is empty -         if(!$result) { theMage::dispatchevent ( -' Checkout_controller_onepage_save_shipping_method ', -                  Array( -' Request ' =$this->getrequest (), +' Quote ' =$this->getonepage ()getquote ())); -             $this->getonepage ()->getquote ()collecttotals (); +             $this->getresponse ()->setbody (Mage::helper (' core ')->jsonencode ($result)); A  at             $result[' goto_section '] = ' payment '; -             $result[' update_section '] =Array( -' Name ' = ' Payment-method ', -' HTML ' =$this-_getpaymentmethodshtml () -             ); -         } in         $this->getonepage ()->getquote ()->collecttotals ()save (); -         $this->getresponse ()->setbody (Mage::helper (' core ')->jsonencode ($result)); to     } +}

In this method, first obtains the post "Shipping_method" data, detects whether there is an error, if there is no error generated $result array, and finally in Mage_core_helper_data class Jsonencode () The $result array is encoded in JSON format and finally sent to the browser.

The method $this->_getpaymentmethodshtml () code for generating $result[' update_section ' [' HTML '] is as follows:

1 //file:base_dir/app/code/core/mage/checkout/controllers/onepagecontroller.php2 /**3 * Get Payment Method Step HTML4  *5 * @return String6  */7 protected function_getpaymentmethodshtml ()8 {9     $layout=$this-getlayout ();Ten     $update=$layout-getUpdate (); One     $update->load (' Checkout_onepage_paymentmethod '); A     $layout-generatexml (); -     $layout-generateblocks (); -  the     //---------only for debug------------------ -     /** @var mage_core_block_template $block*/ -     foreach($layout->getallblocks () as $block) { -Mage::Log($block-gettemplate ()); +     } -     //---------only for debug------------------ +  A     $output=$layout-getoutput (); at     return $output; -}

This method generates HTML for use by the foreground page. You can quickly locate PaymentMethod block's template by doing a log. Where $layout is an instance of Mage_core_model_layout, view its GetOutput () method:

1 //file:base_url/app/code/core/mage/core/model/layout.php2 /**3 * Get All blocks marked for output4  *5 * @return String6  */7  Public functiongetoutput ()8 {9     $out= ' ';Ten     if(!Empty($this-_output)) { One         foreach($this->_output as $callback) { A             //---------only for debug------------------ -Mage::Log($callback); -Mage::Log(Get_class($this->getblock ($callback[0]))); theMage::Log($this->getblock ($callback[0]) -gettemplate ()); -             //---------only for debug------------------ -             $out.=$this->getblock ($callback[0]) -$callback[1](); -         } +     } -  +     return $out; A}

By making a log of $callback, you can know the structure of $callback as follows:

1 //Mage::log ($callback);2 Array3 (4[0] = =Root5[1] = =toHtml6 )7 //Mage::log (Get_class ($this->getblock ($callback [0] ));8 Mage_checkout_block_onepage_payment_methods9 //Mage::log ($this->getblock ($callback [0])->gettemplate ());TenCheckout/onepage/payment/methods.phtml One*/

Then you know that the block of Payment Methods is mage_checkout_block_onepage_payment_methods (if you have experience, you should have guessed it was this block, Magento are rendered by combining block and template files). Combining Mage_checkout_block_onepage_payment_methods and its template file, you can be sure that it is getmethods () this way to get the right payment method.

1 //file:base_dir/app/code/core/mage/payment/block/form/container.php2 /**3 * Retrieve Available Payment Methods4  *5 * @return Array6  */7  Public functionGetMethods ()8 {9     $methods=$this->getdata (' Methods ');Ten     if($methods===NULL) { One         $quote=$this-getquote (); A         $store=$quote?$quote->getstoreid ():NULL; -         $methods=Array(); -         foreach($this->helper (' payment ')->getstoremethods ($store,$quote) as $method) { the             if($this->_canusemethod ($method) &&$method-Isapplicabletoquote ( -                 $quote, -Mage_payment_model_method_abstract::Check_zero_total -             )) { +                 $this->_assignmethod ($method); -                 $methods[] =$method; +             } A         } at         $this->setdata (' Methods ',$methods); -     } -     return $methods; -}

In GetMethods (), there are three ways to control the payment method for a quote:

    • The first is the $this->helper (' Payment ')->getstoremethods ($store, $quote), which is the "source";
    • The second one is $this->_canusemethod ($method);
    • The third one is $method->isapplicabletoquote ($quote, Mage_payment_model_method_abstract::checkZEROtotal);

By combining these three ways, you can filter out the appropriate payment methods. If you add a new payment method, but the front desk does not show up, you should start with these three screening methods.

This is the Magento source "Discovery Tour" is here:D

This article copyright belongs to the author, reproduced please indicate the source: http://www.cnblogs.com/jpdoutop/p/Magento-Zero-Subtotal-Payment-Method.html

Magento 0 RMB Order Payment Method--Magento 0 Subtotal Payment method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.