From 33a56c17fa5af6f3949c3f4b2c46ebb0e0fd5ca3 Mon Sep 17 00:00:00 2001 From: Joshua Goossen Date: Wed, 8 Feb 2017 17:02:38 -0600 Subject: [PATCH 1/2] Added a 'pos_check' gateway. This is need because the built in woocommerce 'cheque' gateway marks orders as 'on-hold'. This one marks them as completed. --- includes/gateways.php | 7 +- includes/gateways/check.php | 133 ++++++++++++++++++++++++++++++++++ languages/woocommerce-pos.pot | 13 +++- 3 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 includes/gateways/check.php diff --git a/includes/gateways.php b/includes/gateways.php index d12fee0c..7e26bb8f 100644 --- a/includes/gateways.php +++ b/includes/gateways.php @@ -53,7 +53,8 @@ public function payment_gateways( array $gateways ) { return array_merge($gateways, array( 'WC_POS\Gateways\Cash', - 'WC_POS\Gateways\Card' + 'WC_POS\Gateways\Card', + 'WC_POS\Gateways\Check' )); } @@ -64,9 +65,9 @@ public function payment_gateways( array $gateways ) { */ public function load_gateways( $gateways ) { foreach( $gateways as $gateway ){ - $gateway->pos = in_array( $gateway->id, array( 'pos_cash', 'pos_card', 'paypal' ) ); + $gateway->pos = in_array( $gateway->id, array( 'pos_cash', 'pos_card', 'pos_check', 'paypal' ) ); } return $gateways; } -} \ No newline at end of file +} diff --git a/includes/gateways/check.php b/includes/gateways/check.php new file mode 100644 index 00000000..602db815 --- /dev/null +++ b/includes/gateways/check.php @@ -0,0 +1,133 @@ + + * @link http://www.wcpos.com + * @extends WC_Payment_Gateway + */ + + //Simply copied from card.php and all references to card were changed to check. + +namespace WC_POS\Gateways; + +use WC_Order; +use WC_Payment_Gateway; +use WC_POS\API; + +class Check extends WC_Payment_Gateway { + + /** + * Constructor for the gateway. + */ + public function __construct() { + $this->id = 'pos_check'; + $this->title = __( 'Check', 'woocommerce-pos' ); + $this->description = ''; + $this->icon = apply_filters( 'woocommerce_pos_check_icon', '' ); + $this->has_fields = true; + + // Actions + add_action( 'woocommerce_pos_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); + add_action( 'woocommerce_thankyou_pos_check', array( $this, 'calculate_cashback' ) ); + + } + + /** + * Display the payment fields in the checkout + */ + public function payment_fields() { + + if ( $this->description ) { + echo '

' . wp_kses_post( $this->description ) . '

'; + } + + $currency_pos = get_option( 'woocommerce_currency_pos' ); + + if( $currency_pos == 'left' || 'left_space') { + $left_addon = ''. get_woocommerce_currency_symbol( get_woocommerce_currency() ) .''; + $right_addon = ''; + } else { + $left_addon = ''; + $right_addon = ''. get_woocommerce_currency_symbol( get_woocommerce_currency() ) .''; + } + + echo ' +
+ +
+ '. $left_addon .' + + '. $right_addon .' +
+
+ '; + + } + + /** + * @param int $order_id + * @return array + */ + public function process_payment( $order_id ) { + + // get order object + $order = new WC_Order( $order_id ); + + // update pos_cash data + $data = API::get_raw_data(); + $cashback = isset( $data['payment_details']['pos-cashback'] ) ? wc_format_decimal( $data['payment_details']['pos-cashback'] ) : 0 ; + + if( $cashback !== 0 ) { + + // add order meta + update_post_meta( $order_id, '_pos_check_cashback', $cashback ); + + // add cashback as fee line item + // TODO: this should be handled by $order->add_fee after WC 2.2 + $item_id = wc_add_order_item( $order_id, array( + 'order_item_name' => __('Cashback', 'woocommerce-pos'), + 'order_item_type' => 'fee' + ) ); + + if ( $item_id ) { + wc_add_order_item_meta( $item_id, '_line_total', $cashback ); + wc_add_order_item_meta( $item_id, '_line_tax', 0 ); + wc_add_order_item_meta( $item_id, '_line_subtotal', $cashback ); + wc_add_order_item_meta( $item_id, '_line_subtotal_tax', 0 ); + wc_add_order_item_meta( $item_id, '_tax_class', 'zero-rate' ); + } + + // update the order total to include fee + $order_total = get_post_meta( $order_id, '_order_total', true ); + $order_total += $cashback; + update_post_meta( $order_id, '_order_total', $order_total ); + + } + + // payment complete + $order->payment_complete(); + + // success + return array( + 'result' => 'success' + ); + } + + public function calculate_cashback( $order_id ) { + $message = ''; + $cashback = get_post_meta( $order_id, '_pos_check_cashback', true ); + + // construct message + if( $cashback ) { + $message = __('Cashback', 'woocommerce-pos') . ': '; + $message .= wc_price($cashback); + } + + echo $message; + } + +} diff --git a/languages/woocommerce-pos.pot b/languages/woocommerce-pos.pot index 43ee6de6..4d67d50f 100644 --- a/languages/woocommerce-pos.pot +++ b/languages/woocommerce-pos.pot @@ -444,6 +444,17 @@ msgstr "" msgid "Amount Tendered" msgstr "" +#: includes/gateways/class-wc-pos-cash.php:28 +msgid "Check" +msgstr "" + +#: includes/gateways/class-wc-pos-card.php:60 +#: includes/gateways/class-wc-pos-card.php:63 +#: includes/gateways/class-wc-pos-card.php:92 +#: includes/gateways/class-wc-pos-card.php:126 +msgid "Cashback" +msgstr "" + #: includes/products/class-wc-pos-visibility.php:23 msgid "POS & Online" msgstr "" @@ -613,4 +624,4 @@ msgstr "" #: includes/views/print/tmpl-receipt.php:225 msgctxt "abbreviation for includes (tax)" msgid "incl." -msgstr "" \ No newline at end of file +msgstr "" From bb72144542ea65baad6d2c8d5fbb700488d93f17 Mon Sep 17 00:00:00 2001 From: Joshua Goossen Date: Thu, 9 Feb 2017 08:11:46 -0600 Subject: [PATCH 2/2] Fixed copy/paste error --- languages/woocommerce-pos.pot | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/languages/woocommerce-pos.pot b/languages/woocommerce-pos.pot index 4d67d50f..fb149a63 100644 --- a/languages/woocommerce-pos.pot +++ b/languages/woocommerce-pos.pot @@ -444,14 +444,14 @@ msgstr "" msgid "Amount Tendered" msgstr "" -#: includes/gateways/class-wc-pos-cash.php:28 +#: includes/gateways/class-wc-pos-check.php:28 msgid "Check" msgstr "" -#: includes/gateways/class-wc-pos-card.php:60 -#: includes/gateways/class-wc-pos-card.php:63 -#: includes/gateways/class-wc-pos-card.php:92 -#: includes/gateways/class-wc-pos-card.php:126 +#: includes/gateways/class-wc-pos-check.php:60 +#: includes/gateways/class-wc-pos-check.php:63 +#: includes/gateways/class-wc-pos-check.php:92 +#: includes/gateways/class-wc-pos-check.php:126 msgid "Cashback" msgstr ""