How to make Payment and Shipping methods
Payment and Shipping methods are necessary to checkout the orders. TheCartPress has some default payment and shipping methods (reemboursement, PayPal gateway, Free Shipping, etc), but you can develop more and most valuables plugins. You can manage them from TheCartPress / Plugins menu.
To make a Payment/Shipping method you must create a class that extends the TCP_Plugin class.
There are few functions your plugin must implement:
- getTitle: It returns the title of the plugin.
- getIcon (since 1.2): It returns the icon url of the plugin.
- getDescription: It returns the description of the plugins. This description will be showed in the plugins list.
- showEditFields: This function allows to add input fields to configure the plugin.
- saveEditFields: Allows to take control before saving the configurable plugin values.
- getCheckoutMethodLabel: In this function the plugin can echo the text in the payment/shipping selected list.
- isApplicable: true if the plugin can be applicable for the shopping cart (default true).
- sendPurchaseMail (since 1.2.3): true if the plugin allows to send the “purchase” eMail (default true). By default, only “Trasnference”, “Cash on Delivery” and “No Payment”(debug) return false.
- getCost: Returns the cost of the payment/Shipping method.
- getNotice (since 1.2.6): Returns a notice about the plugin. It’s stored in orders, to be displayed and mailed to customers.
- showPayForm: Only for payment methods, allow to echo anything after the checkout has ended. For example a bank payment shows the button to go to the bank website.
We have publish a skeleton project in github: https://github.com/tcp-sensei/TheCartPress-Skeleton-Plugin
See the next example code:
<?php /* Plugin Name: MyPluginForTheCartPress Plugin URI: http://MyPluginForTheCartPress.com My Plugin For TheCartPRess description Version: 1.0 Author: me Author URI: http://MyPluginForTheCartPRess.com License: GPL */ // Exit if accessed directly if ( !defined( 'ABSPATH' ) ) exit; if ( ! class_exists( 'TCPSkeletonLoader' ) ) { /** * My Plugin For TheCartPress Loader * * Loads My Plugin For TheCartPress, only if TheCartPress is activated * @since 1.0 */ class MyPluginForTheCartPressLoader { /** * Checks if TheCartPress is activated * * @since 1.0 */ static function init() { if ( ! function_exists( 'is_plugin_active' ) ) { require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); } if ( ! is_plugin_active( 'thecartpress/TheCartPress.class.php' ) ) { add_action( 'admin_notices', array( __CLASS__, 'admin_notices' ) ); } } /** * Displays a message if TheCartPress is not activated * * @since 1.0 */ static function admin_notices() { echo '<div class="error"><p>', __( '<strong>My Plugin for TheCartPress</strong> requires TheCartPress plugin activated.', 'tcp-skeleton' ), '</p></div>'; } /** * Loads the plugin itself * * @since 1.0 */ static function tcp_init() { tcp_load_mypluginforthecartpress_plugin(); } } //WordPress hooks add_action( 'init' , array( 'MyPluginForTheCartPressLoader', 'init' ) ); //TheCartPress hooks add_action( 'tcp_init' , array( 'MyPluginForTheCartPressLoader', 'tcp_init' ) ); /** * Loads the skeleton payment/shipping plugin * * @since 1.0 */ function tcp_load_mypluginforthecartpress_plugin() { /** * Skeleton payment/shipping plugin * * @since 1.0 */ class MyPluginForTheCartPress extends TCP_Plugin { function getTitle() { return 'My Plugin For TheCartPress'; } function getDescription() { return 'The description of MyPluginForTheCartPress.<br>Author: <a href="http://MyPluginForTheCartPress.com" target="_blank">me</a>'; } function showEditFields( $data ) {?> <tr valign="top"> <th scope="row"> <label for="notice">Notice:</label> </th><td> <input type="text" id="notice" name="notice" size="25" maxlength="25" value="<?php echo isset( $data['notice'] ) ? $data['notice'] : '';?>" /> </td> </tr><?php } function getCheckoutMethodLabel( $instance, $shippingCountry, $shoppingCart = false) { $data = tcp_get_payment_plugin_data( 'MyPluginForTheCartPress', $instance ); $title = isset( $data['title'] ) ? $data['title'] : $this->getTitle(); return tcp_string( 'TheCartPress', 'pay_MyPluginForTheCartPress-title', $title ); //multilanguage } function getCost( $instance, $shippingCountry, $shoppingCart = false ) { //If you need to access to the shopping cart to calculate the cost //if ( $shoppingCart === false ) $shoppingCart = TheCartPress::getShoppingCart(); return $shoppingCart->getCount(); //One euro (or dolar or ...) for item in the shopping cart } function getNotice( $instance, $shippingCountry, $shoppingCart, $order_id = 0 ) { $data = tcp_get_payment_plugin_data( get_class( $this ), $instance ); return isset( $data['notice'] ) ? $data['notice'] : ''; } function showPayForm( $instance, $shippingCountry, $shoppingCart, $order_id ) { $data = tcp_get_payment_plugin_data( get_class( $this ), $instance ); $notice = $this->getNotice( $instance, $shippingCountry, $shoppingCart, $order_id ); echo '<p>', $notice, '</p>'; Orders::editStatus( $order_id, $data['new_status'] ); require_once( TCP_CHECKOUT_FOLDER . 'ActiveCheckout.class.php' ); ActiveCheckout::sendMails( $order_id ); } } if ( function_exists( 'tcp_register_payment_plugin' ) ) tcp_register_payment_plugin( 'MyPluginForTheCartPress' ); } }// class_exists check ?>
With the last function we are telling to TheCartPress that the plugin is a payment plugin. There are another function, tcp_register_shipping_plugin, to indicate a shipping method.
This payment method only shows a notice at the end of the checkout and sets the payment cost to one euro (the store currency) per item.
Finally, it updates the status of the order to the status defining in the management page of the plugin.