2012-01-12 00:54:45 +00:00
< ? php
2018-03-09 18:44:14 +00:00
/**
* Class WC_Gateway_Cheque file .
*
* @ package WooCommerce\Gateways
*/
2013-02-20 17:14:46 +00:00
2014-09-20 19:42:58 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2018-03-09 18:44:14 +00:00
exit ; // Exit if accessed directly.
2014-09-20 19:42:58 +00:00
}
2013-02-20 17:14:46 +00:00
2012-01-12 00:54:45 +00:00
/**
2015-11-03 13:31:20 +00:00
* Cheque Payment Gateway .
2012-08-15 18:15:06 +00:00
*
2012-01-12 00:54:45 +00:00
* Provides a Cheque Payment Gateway , mainly for testing purposes .
*
2018-03-09 12:53:10 +00:00
* @ class WC_Gateway_Cheque
* @ extends WC_Payment_Gateway
* @ version 2.1 . 0
* @ package WooCommerce / Classes / Payment
2012-01-12 00:54:45 +00:00
*/
2012-12-31 18:25:09 +00:00
class WC_Gateway_Cheque extends WC_Payment_Gateway {
2012-08-15 18:15:06 +00:00
2016-07-11 14:56:35 +00:00
/**
* Constructor for the gateway .
*/
2012-08-15 18:15:06 +00:00
public function __construct () {
2013-08-05 15:34:48 +00:00
$this -> id = 'cheque' ;
2016-04-25 13:25:39 +00:00
$this -> icon = apply_filters ( 'woocommerce_cheque_icon' , '' );
2013-08-05 15:34:48 +00:00
$this -> has_fields = false ;
2016-10-12 10:16:30 +00:00
$this -> method_title = _x ( 'Check payments' , 'Check payment method' , 'woocommerce' );
2017-04-26 12:51:53 +00:00
$this -> method_description = __ ( 'Allows check payments. Why would you take checks in this day and age? Well you probably would not, but it does allow you to make test purchases for testing order emails and the success pages.' , 'woocommerce' );
2012-08-15 18:15:06 +00:00
2012-01-12 00:54:45 +00:00
// Load the settings.
2013-01-02 13:38:33 +00:00
$this -> init_form_fields ();
2012-01-12 00:54:45 +00:00
$this -> init_settings ();
2012-08-15 18:15:06 +00:00
2018-03-09 18:44:14 +00:00
// Define user set variables.
2013-08-02 10:40:40 +00:00
$this -> title = $this -> get_option ( 'title' );
$this -> description = $this -> get_option ( 'description' );
2017-01-17 18:11:49 +00:00
$this -> instructions = $this -> get_option ( 'instructions' );
2012-08-15 18:15:06 +00:00
2018-03-09 18:44:14 +00:00
// Actions.
2012-12-31 18:25:09 +00:00
add_action ( 'woocommerce_update_options_payment_gateways_' . $this -> id , array ( $this , 'process_admin_options' ) );
2016-07-11 14:56:35 +00:00
add_action ( 'woocommerce_thankyou_cheque' , array ( $this , 'thankyou_page' ) );
2012-08-15 18:15:06 +00:00
2018-03-09 18:44:14 +00:00
// Customer Emails.
2016-07-11 14:56:35 +00:00
add_action ( 'woocommerce_email_before_order_table' , array ( $this , 'email_instructions' ), 10 , 3 );
}
2012-08-15 18:15:06 +00:00
2016-07-11 14:56:35 +00:00
/**
* Initialise Gateway Settings Form Fields .
*/
public function init_form_fields () {
2012-08-15 18:15:06 +00:00
2016-07-11 14:56:35 +00:00
$this -> form_fields = array (
2018-03-09 12:53:10 +00:00
'enabled' => array (
2013-08-02 10:40:40 +00:00
'title' => __ ( 'Enable/Disable' , 'woocommerce' ),
'type' => 'checkbox' ,
2016-10-12 10:16:30 +00:00
'label' => __ ( 'Enable check payments' , 'woocommerce' ),
2018-01-19 07:23:55 +00:00
'default' => 'no' ,
2013-08-02 10:40:40 +00:00
),
2018-03-09 12:53:10 +00:00
'title' => array (
2013-08-02 10:40:40 +00:00
'title' => __ ( 'Title' , 'woocommerce' ),
'type' => 'text' ,
'description' => __ ( 'This controls the title which the user sees during checkout.' , 'woocommerce' ),
2016-10-12 10:16:30 +00:00
'default' => _x ( 'Check payments' , 'Check payment method' , 'woocommerce' ),
2013-08-02 10:40:40 +00:00
'desc_tip' => true ,
),
2018-03-09 12:53:10 +00:00
'description' => array (
2013-08-02 10:40:40 +00:00
'title' => __ ( 'Description' , 'woocommerce' ),
'type' => 'textarea' ,
'description' => __ ( 'Payment method description that the customer will see on your checkout.' , 'woocommerce' ),
2016-04-25 13:25:39 +00:00
'default' => __ ( 'Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.' , 'woocommerce' ),
2013-08-02 10:40:40 +00:00
'desc_tip' => true ,
),
'instructions' => array (
'title' => __ ( 'Instructions' , 'woocommerce' ),
'type' => 'textarea' ,
'description' => __ ( 'Instructions that will be added to the thank you page and emails.' , 'woocommerce' ),
'default' => '' ,
'desc_tip' => true ,
),
);
2016-07-11 14:56:35 +00:00
}
2012-08-15 18:15:06 +00:00
2016-07-11 14:56:35 +00:00
/**
* Output for the order received page .
*/
2013-08-05 15:34:48 +00:00
public function thankyou_page () {
2017-03-07 20:24:24 +00:00
if ( $this -> instructions ) {
2018-03-09 18:44:14 +00:00
echo wp_kses_post ( wpautop ( wptexturize ( $this -> instructions ) ) );
2017-03-07 20:24:24 +00:00
}
2012-01-12 00:54:45 +00:00
}
2012-08-15 18:15:06 +00:00
2016-07-11 14:56:35 +00:00
/**
* Add content to the WC emails .
*
* @ access public
2018-03-09 18:44:14 +00:00
* @ param WC_Order $order Order object .
* @ param bool $sent_to_admin Sent to admin .
* @ param bool $plain_text Email format : plain text or HTML .
2016-07-11 14:56:35 +00:00
*/
2014-03-06 11:57:47 +00:00
public function email_instructions ( $order , $sent_to_admin , $plain_text = false ) {
2016-08-05 15:04:41 +00:00
if ( $this -> instructions && ! $sent_to_admin && 'cheque' === $order -> get_payment_method () && $order -> has_status ( 'on-hold' ) ) {
2018-03-09 18:44:14 +00:00
echo wp_kses_post ( wpautop ( wptexturize ( $this -> instructions ) ) . PHP_EOL );
2014-05-07 14:54:22 +00:00
}
2012-01-12 00:54:45 +00:00
}
2012-08-15 18:15:06 +00:00
2016-07-11 14:56:35 +00:00
/**
* Process the payment and return the result .
*
2018-03-09 18:44:14 +00:00
* @ param int $order_id Order ID .
2016-07-11 14:56:35 +00:00
* @ return array
*/
2013-08-05 15:34:48 +00:00
public function process_payment ( $order_id ) {
2012-01-12 00:54:45 +00:00
2014-08-15 12:29:21 +00:00
$order = wc_get_order ( $order_id );
2012-01-12 00:54:45 +00:00
2017-11-30 03:31:10 +00:00
if ( $order -> get_total () > 0 ) {
2018-03-09 18:44:14 +00:00
// Mark as on-hold (we're awaiting the cheque).
2017-11-30 03:31:10 +00:00
$order -> update_status ( 'on-hold' , _x ( 'Awaiting check payment' , 'Check payment method' , 'woocommerce' ) );
} else {
$order -> payment_complete ();
}
2012-08-15 18:15:06 +00:00
2018-03-09 18:44:14 +00:00
// Reduce stock levels.
2016-08-09 13:15:04 +00:00
wc_reduce_stock_levels ( $order_id );
2012-08-15 18:15:06 +00:00
2018-03-09 18:44:14 +00:00
// Remove cart.
2013-08-05 15:34:48 +00:00
WC () -> cart -> empty_cart ();
2012-08-15 18:15:06 +00:00
2018-03-09 18:44:14 +00:00
// Return thankyou redirect.
2012-01-12 00:54:45 +00:00
return array (
2018-03-09 12:53:10 +00:00
'result' => 'success' ,
'redirect' => $this -> get_return_url ( $order ),
2012-01-12 00:54:45 +00:00
);
}
2014-09-20 19:42:58 +00:00
}