2012-05-26 16:25:07 +00:00
|
|
|
<?php
|
2013-02-20 17:14:46 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
/**
|
|
|
|
* Cash on Delivery Gateway
|
2012-08-15 18:15:06 +00:00
|
|
|
*
|
2012-05-26 16:25:07 +00:00
|
|
|
* Provides a Cash on Delivery Payment Gateway.
|
|
|
|
*
|
2012-12-31 18:25:09 +00:00
|
|
|
* @class WC_Gateway_COD
|
2012-08-15 18:15:06 +00:00
|
|
|
* @extends WC_Payment_Gateway
|
2012-12-31 18:25:09 +00:00
|
|
|
* @version 2.0.0
|
2012-08-15 18:15:06 +00:00
|
|
|
* @package WooCommerce/Classes/Payment
|
|
|
|
* @author Patrick Garman
|
2012-05-26 16:25:07 +00:00
|
|
|
*/
|
2012-12-31 18:25:09 +00:00
|
|
|
class WC_Gateway_COD extends WC_Payment_Gateway {
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for the gateway.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-05-26 16:25:07 +00:00
|
|
|
function __construct() {
|
2012-12-31 12:07:43 +00:00
|
|
|
$this->id = 'cod';
|
|
|
|
$this->icon = apply_filters( 'woocommerce_cod_icon', '' );
|
2012-10-16 09:45:33 +00:00
|
|
|
$this->method_title = __( 'Cash on Delivery', 'woocommerce' );
|
2012-12-31 12:07:43 +00:00
|
|
|
$this->has_fields = false;
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2012-12-31 12:07:43 +00:00
|
|
|
// Load the settings
|
2013-01-02 13:38:33 +00:00
|
|
|
$this->init_form_fields();
|
2012-05-26 16:25:07 +00:00
|
|
|
$this->init_settings();
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2012-12-31 12:07:43 +00:00
|
|
|
// Get settings
|
|
|
|
$this->title = $this->get_option( 'title' );
|
|
|
|
$this->description = $this->get_option( 'description' );
|
|
|
|
$this->instructions = $this->get_option( 'instructions' );
|
|
|
|
$this->enable_for_methods = $this->get_option( 'enable_for_methods', array() );
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2012-12-31 18:25:09 +00:00
|
|
|
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
|
2012-12-15 11:53:32 +00:00
|
|
|
add_action( 'woocommerce_thankyou_cod', array( $this, 'thankyou' ) );
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Admin Panel Options
|
|
|
|
* - Options for bits like 'title' and availability on a country-by-country basis
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-05-26 16:25:07 +00:00
|
|
|
function admin_options() {
|
|
|
|
?>
|
|
|
|
<h3><?php _e('Cash on Delivery','woocommerce'); ?></h3>
|
|
|
|
<p><?php _e('Have your customers pay with cash (or by other means) upon delivery.', 'woocommerce' ); ?></p>
|
|
|
|
<table class="form-table">
|
|
|
|
<?php $this->generate_settings_html(); ?>
|
|
|
|
</table> <?php
|
|
|
|
}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise Gateway Settings Form Fields
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-05-26 16:25:07 +00:00
|
|
|
function init_form_fields() {
|
2012-09-07 09:49:31 +00:00
|
|
|
global $woocommerce;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-07 09:49:31 +00:00
|
|
|
$shipping_methods = array();
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-12-31 12:07:43 +00:00
|
|
|
if ( is_admin() )
|
|
|
|
foreach ( $woocommerce->shipping->load_shipping_methods() as $method ) {
|
|
|
|
$shipping_methods[ $method->id ] = $method->get_title();
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
$this->form_fields = array(
|
|
|
|
'enabled' => array(
|
2012-08-15 18:15:06 +00:00
|
|
|
'title' => __( 'Enable COD', 'woocommerce' ),
|
|
|
|
'label' => __( 'Enable Cash on Delivery', 'woocommerce' ),
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'description' => '',
|
2012-05-26 16:25:07 +00:00
|
|
|
'default' => 'no'
|
2012-08-15 18:15:06 +00:00
|
|
|
),
|
2012-05-26 16:25:07 +00:00
|
|
|
'title' => array(
|
2012-08-15 18:15:06 +00:00
|
|
|
'title' => __( 'Title', 'woocommerce' ),
|
|
|
|
'type' => 'text',
|
|
|
|
'description' => __( 'Payment method title that the customer will see on your website.', 'woocommerce' ),
|
2013-01-18 12:10:19 +00:00
|
|
|
'default' => __( 'Cash on Delivery', 'woocommerce' ),
|
|
|
|
'desc_tip' => true,
|
2012-08-15 18:15:06 +00:00
|
|
|
),
|
2012-05-26 16:25:07 +00:00
|
|
|
'description' => array(
|
2012-08-15 18:15:06 +00:00
|
|
|
'title' => __( 'Description', 'woocommerce' ),
|
|
|
|
'type' => 'textarea',
|
|
|
|
'description' => __( 'Payment method description that the customer will see on your website.', 'woocommerce' ),
|
2013-02-08 15:11:10 +00:00
|
|
|
'default' => __( 'Pay with cash upon delivery.', 'woocommerce' ),
|
2012-08-15 18:15:06 +00:00
|
|
|
),
|
2012-05-26 16:25:07 +00:00
|
|
|
'instructions' => array(
|
2012-08-15 18:15:06 +00:00
|
|
|
'title' => __( 'Instructions', 'woocommerce' ),
|
|
|
|
'type' => 'textarea',
|
|
|
|
'description' => __( 'Instructions that will be added to the thank you page.', 'woocommerce' ),
|
2013-02-08 15:11:10 +00:00
|
|
|
'default' => __( 'Pay with cash upon delivery.', 'woocommerce' )
|
2012-08-15 18:15:06 +00:00
|
|
|
),
|
2012-09-07 09:49:31 +00:00
|
|
|
'enable_for_methods' => array(
|
|
|
|
'title' => __( 'Enable for shipping methods', 'woocommerce' ),
|
|
|
|
'type' => 'multiselect',
|
|
|
|
'class' => 'chosen_select',
|
|
|
|
'css' => 'width: 450px;',
|
|
|
|
'default' => '',
|
2012-09-17 00:53:17 +00:00
|
|
|
'description' => __( 'If COD is only available for certain methods, set it up here. Leave blank to enable for all methods.', 'woocommerce' ),
|
2013-01-18 12:10:19 +00:00
|
|
|
'options' => $shipping_methods,
|
|
|
|
'desc_tip' => true,
|
2012-09-07 09:49:31 +00:00
|
|
|
)
|
2012-05-26 16:25:07 +00:00
|
|
|
);
|
|
|
|
}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
|
2012-09-07 09:49:31 +00:00
|
|
|
/**
|
|
|
|
* Check If The Gateway Is Available For Use
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function is_available() {
|
|
|
|
global $woocommerce;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-07 09:49:31 +00:00
|
|
|
if ( ! empty( $this->enable_for_methods ) ) {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-07 09:49:31 +00:00
|
|
|
if ( is_page( woocommerce_get_page_id( 'pay' ) ) ) {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-07 09:49:31 +00:00
|
|
|
$order_id = (int) $_GET['order_id'];
|
|
|
|
$order = new WC_Order( $order_id );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
|
|
|
if ( ! $order->shipping_method )
|
2012-09-07 09:49:31 +00:00
|
|
|
return false;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-07 09:49:31 +00:00
|
|
|
$chosen_method = $order->shipping_method;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-07 17:26:13 +00:00
|
|
|
} elseif ( empty( $woocommerce->session->chosen_shipping_method ) ) {
|
2012-09-07 09:49:31 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
2012-09-07 17:26:13 +00:00
|
|
|
$chosen_method = $woocommerce->session->chosen_shipping_method;
|
2012-09-07 09:49:31 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-07 17:26:13 +00:00
|
|
|
$found = false;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-07 09:49:31 +00:00
|
|
|
foreach ( $this->enable_for_methods as $method_id ) {
|
|
|
|
if ( strpos( $chosen_method, $method_id ) === 0 ) {
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-07 09:49:31 +00:00
|
|
|
if ( ! $found )
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-07 09:49:31 +00:00
|
|
|
return parent::is_available();
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-07 09:49:31 +00:00
|
|
|
|
2012-08-15 18:15:06 +00:00
|
|
|
/**
|
|
|
|
* Process the payment and return the result
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param int $order_id
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-05-10 11:44:05 +00:00
|
|
|
function process_payment($order_id) {
|
2012-05-26 16:25:07 +00:00
|
|
|
global $woocommerce;
|
|
|
|
|
|
|
|
$order = new WC_Order( $order_id );
|
|
|
|
|
2013-05-10 11:44:05 +00:00
|
|
|
// Mark as processing (payment won't be taken until delivery)
|
|
|
|
$order->update_status( 'processing', __( 'Payment to be made upon delivery.', 'woocommerce' ) );
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
// Reduce stock levels
|
|
|
|
$order->reduce_order_stock();
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
// Remove cart
|
|
|
|
$woocommerce->cart->empty_cart();
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2012-05-26 16:25:07 +00:00
|
|
|
// Return thankyou redirect
|
|
|
|
return array(
|
|
|
|
'result' => 'success',
|
|
|
|
'redirect' => add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink(woocommerce_get_page_id('thanks'))))
|
|
|
|
);
|
|
|
|
}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output for the order received page.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-05-26 16:25:07 +00:00
|
|
|
function thankyou() {
|
2012-12-31 12:07:43 +00:00
|
|
|
echo $this->instructions != '' ? wpautop( $this->instructions ) : '';
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
2012-12-31 18:25:09 +00:00
|
|
|
}
|