Skip to content

Commit

Permalink
* Fix - Enable for shipping methods dropdown.
Browse files Browse the repository at this point in the history
* WooCommerce v3.2+ compatibility.
  • Loading branch information
stuartduff committed Nov 4, 2017
1 parent 4f5d542 commit 5cd1d90
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 54 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ For this extension to function [WooCommerce](https://www.woocommerce.com/) must

## Changelog

**1.0.2 - 04/11/17**
* Fix - Enable for shipping methods dropdown.
* WooCommerce v3.2+ compatibility.

**1.0.1 - 27/10/17**
* Added WooCommerce plugin version check compatibility.

Expand Down
84 changes: 31 additions & 53 deletions classes/class-wc-invoice-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ class WC_Gateway_Invoice extends WC_Payment_Gateway {
* @since 1.0.0
*/
public function __construct() {
$this->id = 'invoice';
$this->icon = apply_filters('wc_invoice_gateway_icon', '');
$this->method_title = __( 'Invoice Payments', 'wc-invoice-gateway' );
$this->method_description = __( 'Allows invoice payments. Sends an order email to the store admin who\'ll have to manually create and send an invoice to the customer.', 'wc-invoice-gateway' );
$this->has_fields = false;
// Setup general properties
$this->setup_properties();

// Load the settings
$this->init_form_fields();
Expand All @@ -47,6 +44,17 @@ public function __construct() {

}

/**
* Setup general properties for the gateway.
*/
protected function setup_properties() {
$this->id = 'invoice';
$this->icon = apply_filters('wc_invoice_gateway_icon', '');
$this->method_title = __( 'Invoice Payments', 'wc-invoice-gateway' );
$this->method_description = __( 'Allows invoice payments. Sends an order email to the store admin who\'ll have to manually create and send an invoice to the customer.', 'wc-invoice-gateway' );
$this->has_fields = false;
}

/**
* Initialise Gateway Settings Form Fields
* @since 1.0.0
Expand All @@ -56,11 +64,9 @@ function init_form_fields() {

$shipping_methods = array();

if ( is_admin() ) {
foreach ( WC()->shipping()->load_shipping_methods() as $method ) {
$shipping_methods[ $method->id ] = $method->get_title();
}
}
foreach ( WC()->shipping()->load_shipping_methods() as $method ) {
$shipping_methods[ $method->id ] = $method->get_method_title();
}

$this->form_fields = array(
'enabled' => array(
Expand Down Expand Up @@ -150,7 +156,7 @@ public function is_available() {
// Test if order needs shipping.
if ( 0 < sizeof( $order->get_items() ) ) {
foreach ( $order->get_items() as $item ) {
$_product = $order->get_product_from_item( $item );
$_product = $item->get_product();
if ( $_product && $_product->needs_shipping() ) {
$needs_shipping = true;
break;
Expand All @@ -166,48 +172,20 @@ public function is_available() {
return false;
}

// Check methods
if ( ! empty( $this->enable_for_methods ) && $needs_shipping ) {
// Only apply if all packages are being shipped via chosen method, or order is virtual.
if ( ! empty( $this->enable_for_methods ) && $needs_shipping ) {
$chosen_shipping_methods = array();

// Only apply if all packages are being shipped via chosen methods, or order is virtual
$chosen_shipping_methods_session = WC()->session->get( 'chosen_shipping_methods' );
if ( is_object( $order ) ) {
$chosen_shipping_methods = array_unique( array_map( 'wc_get_string_before_colon', $order->get_shipping_methods() ) );
} elseif ( $chosen_shipping_methods_session = WC()->session->get( 'chosen_shipping_methods' ) ) {
$chosen_shipping_methods = array_unique( array_map( 'wc_get_string_before_colon', $chosen_shipping_methods_session ) );
}

if ( isset( $chosen_shipping_methods_session ) ) {
$chosen_shipping_methods = array_unique( $chosen_shipping_methods_session );
} else {
$chosen_shipping_methods = array();
}

$check_method = false;

if ( is_object( $order ) ) {
if ( $order->shipping_method ) {
$check_method = $order->shipping_method;
}

} 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];
}

if ( ! $check_method ) {
return false;
}

$found = false;

foreach ( $this->enable_for_methods as $method_id ) {
if ( strpos( $check_method, $method_id ) === 0 ) {
$found = true;
break;
}
}

if ( ! $found ) {
return false;
}
}
if ( 0 < count( array_diff( $chosen_shipping_methods, $this->enable_for_methods ) ) ) {
return false;
}
}

return parent::is_available();

Expand All @@ -228,7 +206,7 @@ function process_payment( $order_id ) {
$order->update_status( apply_filters( 'wc_invoice_gateway_process_payment_order_status', $this->order_status ), __('Awaiting invoice payment', 'wc-invoice-gateway' ) );

// Reduce stock levels
$order->reduce_order_stock();
wc_reduce_stock_levels( $order_id );

// Remove cart
WC()->cart->empty_cart();
Expand Down Expand Up @@ -262,7 +240,7 @@ public function thankyou_page() {
* @param bool $plain_text
*/
public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
if ( $this->instructions && ! $sent_to_admin && 'invoice' === $order->payment_method && apply_filters( 'wc_invoice_gateway_process_payment_order_status', $this->order_status ) !== $order->payment_status ) {
if ( $this->instructions && ! $sent_to_admin && 'invoice' === $order->get_payment_method() && apply_filters( 'wc_invoice_gateway_process_payment_order_status', $this->order_status ) !== 'wc-' . $order->get_status() ) {
echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
}
}
Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ For this extension to function [WooCommerce](https://www.woocommerce.com/) must

== Changelog ==

= 1.0.2 - 04/11/17 =
* Fix - Enable for shipping methods dropdown.
* WooCommerce v3.2+ compatibility.

= 1.0.1 - 27/10/17 =
* Added WooCommerce plugin version check compatibility.

Expand Down
2 changes: 1 addition & 1 deletion wc-invoice-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WooCommerce Invoice Gateway
* Plugin URI: https://wordpress.org/plugins/wc-invoice-gateway/
* Description: Adds Invoice payment gateway functionality to your WooCommerce store. This type of payment method is usually used in B2B transactions with account customers where taking instant digital payment is not an option.
* Version: 1.0.1
* Version: 1.0.2
* Author: Stuart Duff
* Author URI: http://stuartduff.com
* Requires at least: 4.6.0
Expand Down

0 comments on commit 5cd1d90

Please sign in to comment.