woocommerce/classes/gateways/cod/class-wc-cod.php

215 lines
5.5 KiB
PHP
Raw Normal View History

2012-05-26 16:25:07 +00:00
<?php
/**
* 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.
*
* @class WC_COD
2012-08-15 18:15:06 +00:00
* @extends WC_Payment_Gateway
* @version 1.6.4
* @package WooCommerce/Classes/Payment
* @author Patrick Garman
2012-05-26 16:25:07 +00:00
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2012-05-26 16:25:07 +00:00
class WC_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() {
$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' );
$this->has_fields = false;
2012-08-15 18:15:06 +00:00
// Load the settings
2012-05-26 16:25:07 +00:00
$this->init_settings();
2012-08-15 18:15:06 +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
add_action( 'woocommerce_update_options_payment_gateways', array( $this, 'process_admin_options' ) );
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() {
global $woocommerce;
2012-11-27 16:22:47 +00:00
$shipping_methods = array();
2012-11-27 16:22:47 +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' ),
2012-05-26 16:25:07 +00:00
'default' => __( 'Cash on Delivery', 'woocommerce' )
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' ),
2012-05-26 16:25:07 +00:00
'default' => 'Pay with cash upon delivery.'
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' ),
2012-05-26 16:25:07 +00:00
'default' => 'Pay with cash upon delivery.'
2012-08-15 18:15:06 +00:00
),
'enable_for_methods' => array(
'title' => __( 'Enable for shipping methods', 'woocommerce' ),
'type' => 'multiselect',
'class' => 'chosen_select',
'css' => 'width: 450px;',
'default' => '',
'description' => __( 'If COD is only available for certain methods, set it up here. Leave blank to enable for all methods.', 'woocommerce' ),
'options' => $shipping_methods
)
2012-05-26 16:25:07 +00:00
);
}
2012-08-15 18:15:06 +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
if ( ! empty( $this->enable_for_methods ) ) {
2012-11-27 16:22:47 +00:00
if ( is_page( woocommerce_get_page_id( 'pay' ) ) ) {
2012-11-27 16:22:47 +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 )
return false;
2012-11-27 16:22:47 +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 ) ) {
return false;
} else {
2012-09-07 17:26:13 +00:00
$chosen_method = $woocommerce->session->chosen_shipping_method;
}
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
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
if ( ! $found )
return false;
}
2012-11-27 16:22:47 +00:00
return parent::is_available();
}
2012-11-27 16:22:47 +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
*/
2012-05-26 16:25:07 +00:00
function process_payment ($order_id) {
global $woocommerce;
$order = new WC_Order( $order_id );
// Mark as on-hold (we're awaiting the cheque)
2012-10-16 09:45:33 +00:00
$order->update_status('on-hold', __( '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() {
echo $this->instructions != '' ? wpautop( $this->instructions ) : '';
2012-05-26 16:25:07 +00:00
}
2012-08-15 18:15:06 +00:00
2012-05-26 16:25:07 +00:00
}
2012-08-15 18:15:06 +00:00
/**
* Add the gateway to WooCommerce
*
* @access public
* @param array $methods
* @package WooCommerce/Classes/Payment
* @return array
*/
function woocommerce_cod_add_gateway( $methods ) {
$methods[] = 'WC_COD';
return $methods;
}
add_filter( 'woocommerce_payment_gateways', 'woocommerce_cod_add_gateway' );