woocommerce/includes/gateways/cheque/class-wc-gateway-cheque.php

128 lines
4.1 KiB
PHP
Raw Normal View History

2012-01-12 00:54:45 +00:00
<?php
2013-02-20 17:14:46 +00:00
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2012-01-12 00:54:45 +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.
*
* @class WC_Gateway_Cheque
2012-08-15 18:15:06 +00:00
* @extends WC_Payment_Gateway
* @version 2.1.0
2012-08-15 18:15:06 +00:00
* @package WooCommerce/Classes/Payment
* @author WooThemes
2012-01-12 00:54:45 +00:00
*/
class WC_Gateway_Cheque extends WC_Payment_Gateway {
2012-08-15 18:15:06 +00:00
/**
* Constructor for the gateway.
*/
public function __construct() {
$this->id = 'cheque';
$this->icon = apply_filters('woocommerce_cheque_icon', '');
$this->has_fields = false;
$this->method_title = __( 'Cheque', 'woocommerce' );
$this->method_description = __( 'Allows cheque payments. Why would you take cheques in this day and age? Well you probably wouldn\'t but it does allow you to make test purchases for testing order emails and the \'success\' pages etc.', '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
2012-01-12 00:54:45 +00:00
// Define user set variables
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions', $this->description );
2012-08-15 18:15:06 +00:00
2012-01-12 00:54:45 +00:00
// Actions
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_thankyou_cheque', array( $this, 'thankyou_page' ) );
2012-08-15 18:15:06 +00:00
2012-01-12 00:54:45 +00:00
// Customer Emails
2014-02-28 22:13:13 +00:00
add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
2012-08-15 18:15:06 +00:00
}
/**
2012-01-12 00:54:45 +00:00
* Initialise Gateway Settings Form Fields
*/
public function init_form_fields() {
2012-08-15 18:15:06 +00:00
2012-01-12 00:54:45 +00:00
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable Cheque Payment', 'woocommerce' ),
'default' => 'yes'
),
2012-01-12 00:54:45 +00:00
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Cheque Payment', 'woocommerce' ),
'desc_tip' => true,
),
2012-01-12 00:54:45 +00:00
'description' => array(
'title' => __( 'Description', 'woocommerce' ),
'type' => 'textarea',
'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),
'default' => __( 'Please send your cheque to Store Name, Store Street, Store Town, Store State / County, Store Postcode.', 'woocommerce' ),
'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,
),
);
2012-08-15 18:15:06 +00:00
}
/**
* Output for the order received page.
*/
public function thankyou_page() {
if ( $this->instructions )
echo wpautop( wptexturize( $this->instructions ) );
2012-01-12 00:54:45 +00:00
}
2012-08-15 18:15:06 +00:00
/**
* Add content to the WC emails.
*
* @access public
* @param WC_Order $order
* @param bool $sent_to_admin
2014-02-28 22:13:13 +00:00
* @param bool $plain_text
2012-08-15 18:15:06 +00:00
*/
public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
2014-06-03 09:45:33 +00:00
if ( $this->instructions && ! $sent_to_admin && 'cheque' === $order->payment_method && $order->has_status( 'on-hold' ) ) {
echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
}
2012-01-12 00:54:45 +00:00
}
2012-08-15 18:15:06 +00:00
/**
* Process the payment and return the result
*
* @param int $order_id
* @return array
*/
public function process_payment( $order_id ) {
2012-01-12 00:54:45 +00:00
$order = wc_get_order( $order_id );
2012-01-12 00:54:45 +00:00
// Mark as on-hold (we're awaiting the cheque)
$order->update_status( 'on-hold', __( 'Awaiting cheque payment', 'woocommerce' ) );
2012-08-15 18:15:06 +00:00
2012-01-12 00:54:45 +00:00
// Reduce stock levels
$order->reduce_order_stock();
2012-08-15 18:15:06 +00:00
2012-01-12 00:54:45 +00:00
// Remove cart
WC()->cart->empty_cart();
2012-08-15 18:15:06 +00:00
2012-01-12 00:54:45 +00:00
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
2012-01-12 00:54:45 +00:00
);
}
}