2012-05-26 16:25:07 +00:00
|
|
|
<?php
|
2013-02-20 17:14:46 +00:00
|
|
|
|
2014-09-20 19:42:58 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
2013-02-20 17:14:46 +00:00
|
|
|
|
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
|
2013-08-05 15:34:48 +00:00
|
|
|
* @version 2.1.0
|
2012-08-15 18:15:06 +00:00
|
|
|
* @package WooCommerce/Classes/Payment
|
2013-08-05 15:34:48 +00:00
|
|
|
* @author WooThemes
|
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.
|
|
|
|
*/
|
2013-08-05 15:34:48 +00:00
|
|
|
public function __construct() {
|
|
|
|
$this->id = 'cod';
|
|
|
|
$this->icon = apply_filters( 'woocommerce_cod_icon', '' );
|
|
|
|
$this->method_title = __( 'Cash on Delivery', 'woocommerce' );
|
|
|
|
$this->method_description = __( 'Have your customers pay with cash (or by other means) upon delivery.', 'woocommerce' );
|
|
|
|
$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' );
|
2014-04-28 04:17:40 +00:00
|
|
|
$this->instructions = $this->get_option( 'instructions', $this->description );
|
2012-12-31 12:07:43 +00:00
|
|
|
$this->enable_for_methods = $this->get_option( 'enable_for_methods', array() );
|
2014-05-08 14:05:34 +00:00
|
|
|
$this->enable_for_virtual = $this->get_option( 'enable_for_virtual', 'yes' ) === 'yes' ? true : false;
|
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' ) );
|
2013-12-10 11:06:22 +00:00
|
|
|
add_action( 'woocommerce_thankyou_cod', array( $this, 'thankyou_page' ) );
|
2014-04-28 04:17:40 +00:00
|
|
|
|
|
|
|
// Customer Emails
|
|
|
|
add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise Gateway Settings Form Fields
|
|
|
|
*/
|
2013-08-05 15:34:48 +00:00
|
|
|
public function init_form_fields() {
|
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() )
|
2013-11-25 14:01:32 +00:00
|
|
|
foreach ( WC()->shipping->load_shipping_methods() as $method ) {
|
2012-12-31 12:07:43 +00:00
|
|
|
$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(
|
2013-08-05 15:34:48 +00:00
|
|
|
'title' => __( 'Enable COD', 'woocommerce' ),
|
|
|
|
'label' => __( 'Enable Cash on Delivery', 'woocommerce' ),
|
|
|
|
'type' => 'checkbox',
|
2012-08-15 18:15:06 +00:00
|
|
|
'description' => '',
|
2013-08-05 15:34:48 +00:00
|
|
|
'default' => 'no'
|
2012-08-15 18:15:06 +00:00
|
|
|
),
|
2012-05-26 16:25:07 +00:00
|
|
|
'title' => array(
|
2013-08-05 15:34:48 +00:00
|
|
|
'title' => __( 'Title', 'woocommerce' ),
|
|
|
|
'type' => 'text',
|
|
|
|
'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ),
|
|
|
|
'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(
|
2013-08-05 15:34:48 +00:00
|
|
|
'title' => __( 'Description', 'woocommerce' ),
|
|
|
|
'type' => 'textarea',
|
2012-08-15 18:15:06 +00:00
|
|
|
'description' => __( 'Payment method description that the customer will see on your website.', 'woocommerce' ),
|
2013-08-05 15:34:48 +00:00
|
|
|
'default' => __( 'Pay with cash upon delivery.', 'woocommerce' ),
|
|
|
|
'desc_tip' => true,
|
2012-08-15 18:15:06 +00:00
|
|
|
),
|
2012-05-26 16:25:07 +00:00
|
|
|
'instructions' => array(
|
2013-08-05 15:34:48 +00:00
|
|
|
'title' => __( 'Instructions', 'woocommerce' ),
|
|
|
|
'type' => 'textarea',
|
2012-08-15 18:15:06 +00:00
|
|
|
'description' => __( 'Instructions that will be added to the thank you page.', 'woocommerce' ),
|
2013-08-05 15:34:48 +00:00
|
|
|
'default' => __( 'Pay with cash upon delivery.', 'woocommerce' ),
|
|
|
|
'desc_tip' => true,
|
2012-08-15 18:15:06 +00:00
|
|
|
),
|
2012-09-07 09:49:31 +00:00
|
|
|
'enable_for_methods' => array(
|
2013-10-11 14:27:04 +00:00
|
|
|
'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,
|
|
|
|
'desc_tip' => true,
|
|
|
|
'custom_attributes' => array(
|
|
|
|
'data-placeholder' => __( 'Select shipping methods', 'woocommerce' )
|
|
|
|
)
|
2014-05-08 14:05:34 +00:00
|
|
|
),
|
|
|
|
'enable_for_virtual' => array(
|
|
|
|
'title' => __( 'Enable for virtual orders', 'woocommerce' ),
|
|
|
|
'label' => __( 'Enable COD if the order is virtual', 'woocommerce' ),
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'default' => 'yes'
|
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
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-08-05 15:34:48 +00:00
|
|
|
public function is_available() {
|
2014-04-24 18:33:14 +00:00
|
|
|
$order = null;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2014-05-08 14:06:20 +00:00
|
|
|
if ( ! $this->enable_for_virtual ) {
|
|
|
|
if ( WC()->cart && ! WC()->cart->needs_shipping() ) {
|
2014-05-08 14:05:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-24 15:37:31 +00:00
|
|
|
|
2014-05-08 14:05:34 +00:00
|
|
|
if ( is_page( wc_get_page_id( 'checkout' ) ) && 0 < get_query_var( 'order-pay' ) ) {
|
|
|
|
$order_id = absint( get_query_var( 'order-pay' ) );
|
2014-08-15 12:29:21 +00:00
|
|
|
$order = wc_get_order( $order_id );
|
2014-04-24 18:33:14 +00:00
|
|
|
|
2014-05-08 14:05:34 +00:00
|
|
|
// Test if order needs shipping.
|
|
|
|
$needs_shipping = false;
|
2014-04-24 18:33:14 +00:00
|
|
|
|
2014-05-08 14:05:34 +00:00
|
|
|
if ( 0 < sizeof( $order->get_items() ) ) {
|
|
|
|
foreach ( $order->get_items() as $item ) {
|
|
|
|
$_product = $order->get_product_from_item( $item );
|
2014-04-24 18:33:14 +00:00
|
|
|
|
2014-05-08 14:05:34 +00:00
|
|
|
if ( $_product->needs_shipping() ) {
|
|
|
|
$needs_shipping = true;
|
|
|
|
break;
|
|
|
|
}
|
2014-04-24 18:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 14:05:34 +00:00
|
|
|
$needs_shipping = apply_filters( 'woocommerce_cart_needs_shipping', $needs_shipping );
|
2014-04-24 18:33:14 +00:00
|
|
|
|
2014-05-08 14:05:34 +00:00
|
|
|
if ( $needs_shipping ) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-24 18:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 09:49:31 +00:00
|
|
|
if ( ! empty( $this->enable_for_methods ) ) {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-08-14 20:00:34 +00:00
|
|
|
// Only apply if all packages are being shipped via local pickup
|
2014-02-18 09:58:52 +00:00
|
|
|
$chosen_shipping_methods_session = WC()->session->get( 'chosen_shipping_methods' );
|
2014-02-18 11:27:05 +00:00
|
|
|
|
|
|
|
if ( isset( $chosen_shipping_methods_session ) ) {
|
2014-02-18 09:58:52 +00:00
|
|
|
$chosen_shipping_methods = array_unique( $chosen_shipping_methods_session );
|
2014-02-18 11:27:05 +00:00
|
|
|
} else {
|
|
|
|
$chosen_shipping_methods = array();
|
|
|
|
}
|
|
|
|
|
2013-08-14 20:00:34 +00:00
|
|
|
$check_method = false;
|
|
|
|
|
2014-04-24 18:33:14 +00:00
|
|
|
if ( is_object( $order ) ) {
|
|
|
|
if ( $order->shipping_method ) {
|
2013-08-14 20:00:34 +00:00
|
|
|
$check_method = $order->shipping_method;
|
2014-04-24 18:33:14 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-08-14 20:00:34 +00:00
|
|
|
} elseif ( empty( $chosen_shipping_methods ) || sizeof( $chosen_shipping_methods ) > 1 ) {
|
|
|
|
$check_method = false;
|
|
|
|
} elseif ( sizeof( $chosen_shipping_methods ) == 1 ) {
|
|
|
|
$check_method = $chosen_shipping_methods[0];
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2014-04-24 15:37:31 +00:00
|
|
|
if ( ! $check_method ) {
|
2012-09-07 09:49:31 +00:00
|
|
|
return false;
|
2014-04-24 15:37: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 ) {
|
2013-08-14 20:00:34 +00:00
|
|
|
if ( strpos( $check_method, $method_id ) === 0 ) {
|
2012-09-07 09:49:31 +00:00
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2014-04-24 15:37:31 +00:00
|
|
|
if ( ! $found ) {
|
2012-09-07 09:49:31 +00:00
|
|
|
return false;
|
2014-04-24 15:37:31 +00:00
|
|
|
}
|
2012-09-07 09:49:31 +00:00
|
|
|
}
|
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
|
|
|
|
2014-04-24 18:03:33 +00:00
|
|
|
|
2012-08-15 18:15:06 +00:00
|
|
|
/**
|
|
|
|
* Process the payment and return the result
|
|
|
|
*
|
|
|
|
* @param int $order_id
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-08-05 15:34:48 +00:00
|
|
|
public function process_payment( $order_id ) {
|
2012-05-26 16:25:07 +00:00
|
|
|
|
2014-08-15 12:29:21 +00:00
|
|
|
$order = wc_get_order( $order_id );
|
2012-05-26 16:25:07 +00:00
|
|
|
|
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
|
2013-08-05 15:34:48 +00:00
|
|
|
WC()->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',
|
2013-05-31 15:13:14 +00:00
|
|
|
'redirect' => $this->get_return_url( $order )
|
2012-05-26 16:25:07 +00:00
|
|
|
);
|
|
|
|
}
|
2012-08-15 18:15:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Output for the order received page.
|
|
|
|
*/
|
2013-08-05 15:34:48 +00:00
|
|
|
public function thankyou_page() {
|
2014-05-07 14:54:22 +00:00
|
|
|
if ( $this->instructions ) {
|
2013-08-05 15:34:48 +00:00
|
|
|
echo wpautop( wptexturize( $this->instructions ) );
|
2014-05-07 14:54:22 +00:00
|
|
|
}
|
2012-05-26 16:25:07 +00:00
|
|
|
}
|
2014-04-28 04:17:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add content to the WC emails.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param WC_Order $order
|
|
|
|
* @param bool $sent_to_admin
|
|
|
|
* @param bool $plain_text
|
|
|
|
*/
|
|
|
|
public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
|
2014-05-07 14:54:22 +00:00
|
|
|
if ( $this->instructions && ! $sent_to_admin && 'cod' === $order->payment_method ) {
|
|
|
|
echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
|
|
|
|
}
|
2014-04-28 04:17:40 +00:00
|
|
|
}
|
2014-02-18 11:27:05 +00:00
|
|
|
}
|