Overview
  • Namespace
  • Class

Namespaces

  • None
  • WC_POS
    • Admin
      • Settings
        • Receipt
    • API
    • Gateways
    • Integrations
    • Products

Classes

  • WC_POS\Activator
  • WC_POS\Admin
  • WC_POS\Admin\Gateways
  • WC_POS\Admin\Menu
  • WC_POS\Admin\Notices
  • WC_POS\Admin\Orders
  • WC_POS\Admin\Page
  • WC_POS\Admin\Permalink
  • WC_POS\Admin\Plugins
  • WC_POS\Admin\Products
  • WC_POS\Admin\Settings
  • WC_POS\Admin\Settings\Access
  • WC_POS\Admin\Settings\Checkout
  • WC_POS\Admin\Settings\Customers
  • WC_POS\Admin\Settings\Gateways
  • WC_POS\Admin\Settings\General
  • WC_POS\Admin\Settings\HotKeys
  • WC_POS\Admin\Settings\Page
  • WC_POS\Admin\Settings\Receipt\Options
  • WC_POS\Admin\Settings\Receipt\Template
  • WC_POS\Admin\Settings\Receipts
  • WC_POS\Admin\Settings\Status
  • WC_POS\Admin\Settings\Tools
  • WC_POS\Admin\Status
  • WC_POS\Ajax
  • WC_POS\API
  • WC_POS\API\Coupons
  • WC_POS\API\Customers
  • WC_POS\API\Gateways
  • WC_POS\API\i18n
  • WC_POS\API\Orders
  • WC_POS\API\Params
  • WC_POS\API\Payload
  • WC_POS\API\Products
  • WC_POS\API\Settings
  • WC_POS\API\Support
  • WC_POS\API\Templates
  • WC_POS\Customers
  • WC_POS\Deactivator
  • WC_POS\Gateways
  • WC_POS\Gateways\Card
  • WC_POS\Gateways\Cash
  • WC_POS\i18n
  • WC_POS\Integrations\Bookings
  • WC_POS\Products
  • WC_POS\Products\Visibility
  • WC_POS\Setup
  • WC_POS\Status
  • WC_POS\Tax
  • WC_POS\Template

Functions

  • is_pos
  • is_pos_admin
  • wc_pos_get_option
  • wc_pos_json_encode
  • wc_pos_locate_template
  • wc_pos_trim_html_string
  • wc_pos_update_option
  • wc_pos_url
  1 <?php
  2 
  3 /**
  4  * POS Order Admin Class
  5  * - only active for $screen-id = edit-shop_order or shop_order
  6  * - allow users to change order type: POS/Online
  7  * - allow users to switch POS payment method
  8  * - filter orders by POS vs Online
  9  *
 10  * @class    WC_POS_Admin_Orders
 11  * @package  WooCommerce POS
 12  * @author   Paul Kilmurray <paul@kilbot.com.au>
 13  * @link     http://www.woopos.com.au
 14  */
 15 
 16 namespace WC_POS\Admin;
 17 
 18 use WC_POS\Admin\Settings\Checkout;
 19 
 20 class Orders {
 21 
 22   private $pos_order;
 23 
 24   public function __construct() {
 25 
 26     // option for order type
 27     add_action( 'woocommerce_admin_order_data_after_order_details', array($this, 'order_details') );
 28     add_action( 'woocommerce_process_shop_order_meta', array($this, 'save'), 10, 2 );
 29 
 30     // payment method dropdown
 31     if( version_compare( WC()->version, '2.3', '>' ) ){
 32       add_filter( 'woocommerce_payment_gateways', array($this, 'payment_gateways'), 20, 1 );
 33     }
 34 
 35     // pos vs online filter on edit-shop_order page
 36     add_filter( 'views_edit-shop_order', array( $this, 'pos_order_filters' ), 10, 1 );
 37     add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 10, 1 );
 38 
 39     // add class to pos order rows
 40     add_filter( 'post_class', array( $this, 'post_class' ), 10, 3 );
 41     add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_styles' ) );
 42 
 43   }
 44 
 45   /**
 46    * Add select dropdown
 47    * @param $order
 48    */
 49   public function order_details($order){
 50     $this->pos_order = get_post_meta( $order->id, '_pos', true );
 51     include 'views/order-details.php';
 52   }
 53 
 54   /**
 55    * Save the order type
 56    * @param $post_id
 57    * @param $post
 58    */
 59   public function save( $post_id, $post ){
 60     if( isset($_POST['wc_pos_order_type']) ){
 61       update_post_meta($post_id, '_pos', $_POST['wc_pos_order_type']);
 62     }
 63   }
 64 
 65   /**
 66    * Show POS enabled gateways
 67    * note: only available in WC > 2.3
 68    * @param $gateways
 69    * @return array
 70    */
 71   public function payment_gateways($gateways){
 72     // get checkout settings data
 73     $settings = Checkout::get_instance();
 74     $enabled_ids = $settings->get_enabled_gateway_ids();
 75 
 76     $loaded_gateways = array();
 77 
 78     foreach($gateways as $Gateway){
 79       $gateway = new $Gateway();
 80       if( in_array( $gateway->id, $enabled_ids ) ){
 81         $gateway->enabled = 'yes';
 82       }
 83       $loaded_gateways[] = $gateway;
 84     }
 85 
 86     return $loaded_gateways;
 87   }
 88 
 89   /**
 90    * Order admin filter links
 91    * @param  array $views
 92    * @return array
 93    */
 94   public function pos_order_filters( $views ) {
 95     global $wpdb;
 96 
 97     $visibility_filters = array(
 98       'yes' => __( 'POS', 'woocommerce-pos' ),
 99       'no' => __( 'Online', 'woocommerce-pos' )
100     );
101 
102     if ( isset( $_GET['pos_order'] ) && !empty( $_GET['pos_order'] ) ) {
103       $views['all'] = str_replace( 'class="current"', '', $views['all'] );
104     }
105 
106     foreach( $visibility_filters as $key => $label ) {
107       $sql = "SELECT count(DISTINCT pm.post_id)
108             FROM $wpdb->postmeta pm
109             JOIN $wpdb->posts p ON (p.ID = pm.post_id)
110             WHERE ";
111       $sql .= $key == 'no' ? " pm.post_id NOT IN ( SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_pos' ) "  : " pm.meta_key = '_pos' AND pm.meta_value = '1' ";
112 //      if( version_compare( WC()->version, '2.2.0' ) >= 0 ) {
113         $sql .= "AND p.post_type = 'shop_order'";
114 //      } else {
115 //        $sql .= "AND p.post_type = 'shop_order' AND p.post_status = 'publish'";
116 //      }
117 
118       $count = $wpdb->get_var($sql);
119       $class            = ( isset( $_GET['pos_order'] ) && $_GET['pos_order'] == $key ) ? 'current' : '';
120       $query_string     = remove_query_arg(array( 'pos_order' ));
121       if( $class == '' ) $query_string = remove_query_arg(array( 'paged' ));
122       $query_string     = add_query_arg( 'pos_order', urlencode($key), $query_string );
123       $views[$key]  = '<a href="'. $query_string . '" class="' . esc_attr( $class ) . '">' . $label . ' <span class="count">(' . number_format_i18n( $count ) . ')</a>';
124     }
125 
126     return $views;
127   }
128 
129   /**
130    * Order admin filter
131    * @param $query
132    */
133   function pre_get_posts( $query ) {
134 
135     if ( isset( $_GET['pos_order'] ) && !empty( $_GET['pos_order'] ) ) {
136       if( $_GET['pos_order'] == 'yes' ) {
137         $meta_query =  array(
138           array(
139             'key'       => '_pos',
140             'value'     => '1',
141             'compare'   => '=='
142           )
143         );
144       }
145       else {
146         $meta_query =  array(
147           'relation' => 'OR',
148           array(
149             'key'       => '_pos',
150             'value'     => '0',
151             'compare'   => '=='
152           ),
153           array(
154             'key'       => '_pos',
155             'compare'   => 'NOT EXISTS'
156           )
157         );
158       }
159 
160       $query->set( 'meta_query', $meta_query );
161     }
162 
163   }
164 
165   /**
166    * Add type-wc_pos_order class to pos order rows
167    *
168    * @param $classes
169    * @param $class
170    * @param $id
171    * @return array
172    */
173   public function post_class($classes, $class, $id){
174     if( get_post_meta( $id, '_pos', true ) ){
175       $classes[] = 'type-wc_pos_order';
176     }
177     return $classes;
178   }
179 
180   /**
181    * CSS
182    */
183   public function enqueue_admin_styles() {
184     $css = '
185       .widefat .type-wc_pos_order .column-order_status {
186         background: url( '. \WC_POS\PLUGIN_URL .'assets/logo.svg ) no-repeat 75% 9px;
187         background-size: 18px;
188         fill: #94d31b;
189       }
190       .widefat .type-wc_pos_order .column-order_status mark {
191         margin: 0 auto 0 0;
192       }
193     ';
194     wp_add_inline_style( 'wp-admin', $css );
195   }
196 
197 }
API documentation generated by ApiGen