Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 75.30 KB | None | 0 0
  1. class WC_POS_AJAX
  2. {
  3.  
  4.     /**
  5.      * Hook into ajax events
  6.      */
  7.     public function __construct()
  8.     {
  9.  
  10.         if ( get_option( 'wc_pos_enable_new_api', 'no' ) == 'no' ) {
  11.             //$this->increase_timeout();
  12.         }
  13.  
  14.         // woocommerce_EVENT => nopriv
  15.         $ajax_events = array(
  16.             'new_update_outlets_address' => false,
  17.             'edit_update_outlets_address' => false,
  18.             'add_products_to_register' => false,
  19.             'update_product_quantity' => false,
  20.             'remove_product_from_register' => false,
  21.             'add_customer' => false,
  22.             'loading_states' => false,
  23.             'add_customers_to_register' => false,
  24.             'search_variations_for_product' => false,
  25.             'tile_ordering' => false,
  26.             'json_search_usernames' => false,
  27.             'search_variations_for_product_and_sku' => false,
  28.             'check_shipping' => false,
  29.  
  30.             'stripe_get_outlet_address' => false,
  31.             'json_search_products' => false,
  32.             'json_search_products_all' => false,
  33.             'find_variantion_by_attributes' => false,
  34.             'add_product_grid' => false,
  35.             'get_server_product_ids' => false,
  36.             'json_search_customers' => false,
  37.             'checkout' => false,
  38.             'void_register' => false,
  39.             'search_order_by_code' => false,
  40.             'json_search_registers' => false,
  41.             'json_search_outlet' => false,
  42.             'json_search_cashier' => false,
  43.             'update_customer_shipping_address' => false,
  44.             'filter_order_barcode' => false,
  45.             'filter_product_barcode' => false,
  46.             'change_stock' => false,
  47.             'get_grid_options' => false,
  48.             'can_user_open_register' => false,
  49.             'add_product_for_barcode' => false,
  50.             'get_product_variations_for_barcode' => false,
  51.             'json_search_categories' => false,
  52.             'get_products_by_categories' => false,
  53.             'set_register_opening_cash' => false,
  54.             'add_cash_management_action' => false,
  55.             'get_user_avatars' => false,
  56.             'set_register_actual_cash' => false,
  57.             'refresh_bill_screen' => true,
  58.             'get_default_variations' => true,
  59.             'save_tab' => false,
  60.             'get_customer_html' => false,
  61.             'delete_tab' => false,
  62.             'check_user_card_uniqueness' => false,
  63.             'get_user_by_card_number' => false,
  64.             'rename_payment_gateway' => false,
  65.             'logout' => false,
  66.             'pos_print_receipt' => false,
  67.             'use_store_address' => false
  68.         );
  69.  
  70.         foreach ($ajax_events as $ajax_event => $nopriv) {
  71.             add_action('wp_ajax_wc_pos_' . $ajax_event, array($this, $ajax_event));
  72.  
  73.             if ($nopriv)
  74.                 add_action('wp_ajax_nopriv_wc_pos_' . $ajax_event, array($this, $ajax_event));
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * WC REST API can timeout on some servers
  80.      * This is an attempt t o increase the timeout limit
  81.      * TODO: is there a better way?
  82.      */
  83.     public function increase_timeout()
  84.     {
  85.         $timeout = 6000;
  86.         if (!ini_get('safe_mode'))
  87.             @set_time_limit($timeout);
  88.  
  89.         @ini_set('memory_limit', WP_MAX_MEMORY_LIMIT);
  90.         @ini_set('max_execution_time', (int)$timeout);
  91.     }
  92.  
  93.     /**
  94.      * Output headers for JSON requests
  95.      */
  96.     private function json_headers()
  97.     {
  98.         header('Content-Type: application/json; charset=utf-8');
  99.     }
  100.  
  101.     public function new_update_outlets_address()
  102.     {
  103.         check_ajax_referer('new-update-pos-outlets-address', 'security');
  104.         WC_POS()->outlet()->display_outlet_form();
  105.         die();
  106.     }
  107.  
  108.     public function edit_update_outlets_address()
  109.     {
  110.         check_ajax_referer('edit-update-pos-outlets-address', 'security');
  111.         WC_POS()->outlet()->display_edit_form();
  112.         die();
  113.     }
  114.  
  115.     /* change the state according country */
  116.  
  117.     public function loading_states()
  118.     {
  119.         $country = $_REQUEST['country'];
  120.         $id = $_REQUEST['id'];
  121.         $countries = new WC_Countries();
  122.         $filds = $countries->get_address_fields($country, '');
  123.  
  124.         unset($filds['first_name']);
  125.         unset($filds['last_name']);
  126.         unset($filds['company']);
  127.         $filds['country']['options'] = $countries->get_allowed_countries();
  128.         $filds['country']['type'] = 'select';
  129.  
  130.         if ($country != '') {
  131.             $filds['country']['value'] = $country;
  132.             $states = $countries->get_allowed_country_states();
  133.             if (!empty($states[$country])) {
  134.                 $filds['state']['options'] = $states[$country];
  135.                 $filds['state']['type'] = 'select';
  136.             }
  137.         }
  138.  
  139.         $statelabel = $filds['state']['label'];
  140.         $postcodelabel = $filds['postcode']['label'];
  141.         $citylabel = $filds['city']['label'];
  142.         $html = array();
  143.         $state_html = '';
  144.         if ($id == 'shipping_country') {
  145.             $dd = 'shipping_state';
  146.         } else {
  147.             $dd = 'billing_state';
  148.         }
  149.         if (isset($filds['state']['options']) && !empty($filds['state']['options'])) {
  150.             $state_html .= '<select id="' . $dd . '" class="ajax_chosen_select_' . $dd . '" style="width: 100%;" name="' . $id . '_county">';
  151.             foreach ($filds['state']['options'] as $key => $value) {
  152.                 $state_html .= '<option value = "' . $key . '"> ' . $value . '</option>';
  153.             }
  154.             $state_html .= '</select>';
  155.         } else {
  156.             $state_html .= '<input type="text" id="' . $dd . '" name="' . $dd . '" class="input" placeholder="' . $statelabel . '"/>';
  157.         }
  158.         $html['state_html'] = $state_html;
  159.         $html['state_label'] = $statelabel;
  160.         $html['zip_label'] = $postcodelabel;
  161.         $html['city_label'] = $citylabel;
  162.         echo(json_encode($html));
  163.         die;
  164.     }
  165.  
  166.     public function add_customer()
  167.     {
  168.         global $wpdb, $user;
  169.         $userdata = array();
  170.         parse_str($_REQUEST['form_data'], $userdata);
  171.         $email = $userdata['billing_email'];
  172.         $useremail = sanitize_user($email);
  173.  
  174.  
  175.         $nickname = str_replace(' ', '', ucfirst(strtolower($userdata['billing_first_name']))) . str_replace(' ', '', ucfirst(strtolower($userdata['billing_last_name'])));
  176.         $username_opt = get_option('woocommerce_pos_end_of_sale_username_add_customer');
  177.         switch ($username_opt) {
  178.             case 2:
  179.                 $username = str_replace(' ', '', strtolower($userdata['billing_first_name'])) . '-' . str_replace(' ', '', strtolower($userdata['billing_last_name']));
  180.                 break;
  181.             case 3:
  182.                 $username = $email;
  183.                 break;
  184.             default:
  185.                 $username = strtolower($nickname);
  186.                 break;
  187.         }
  188.         $username = _truncate_post_slug($username, 60);
  189.         $check_sql = "SELECT user_login FROM {$wpdb->users} WHERE user_login = '%s' LIMIT 1";
  190.  
  191.         $user_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $username));
  192.  
  193.  
  194.         if ($user_name_check) {
  195.             $suffix = 1;
  196.             do {
  197.                 $alt_user_name = _truncate_post_slug($username, 60 - (strlen($suffix) + 1)) . "-$suffix";
  198.                 $user_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_user_name));
  199.                 $suffix++;
  200.             } while ($user_name_check);
  201.             $username = $alt_user_name;
  202.         }
  203.  
  204.  
  205.         $id_user = username_exists($username);
  206.         $user_id = $userdata['customer_details_id'];
  207.         $new_user = false;
  208.         // CREATES WP USER ACCOUNT
  209.         if (empty($userdata['customer_details_id'])) {
  210.  
  211.             if (!$id_user and email_exists($useremail) == false) {
  212.  
  213.  
  214.                 add_filter('pre_option_woocommerce_registration_generate_password', 'pos_enable_generate_password');
  215.                 $user_id = wc_create_new_customer($useremail, $username);
  216.                 remove_filter('pre_option_woocommerce_registration_generate_password', 'pos_enable_generate_password');
  217.  
  218.                 $new_user = true;
  219.             } else {
  220.                 echo '<!--WC_POS_START-->' . json_encode(
  221.                         array('success' => false, 'message' => __('User already exists.', 'wc_point_of_sale'))
  222.                     ) . '<!--WC_POS_END-->';
  223.                 die();
  224.             }
  225.         }
  226.  
  227.  
  228.         $phone = $userdata['billing_phone'];
  229.         $billing_country = $userdata['billing_country'];
  230.         $billing_firstname = $userdata['billing_first_name'];
  231.         $billing_lastname = $userdata['billing_last_name'];
  232.         $billing_company = $userdata['billing_company'];
  233.         $billing_address = $userdata['billing_address_1'];
  234.         $billing_address1 = $userdata['billing_address_2'];
  235.         $billing_city = $userdata['billing_city'];
  236.         $billing_state = '';
  237.         if (isset($userdata['billing_state'])) {
  238.             $billing_state = $userdata['billing_state'];
  239.         } else if (isset($userdata['billing_country_county'])) {
  240.             $billing_state = $userdata['billing_country_county'];
  241.         }
  242.         $billing_postcode = $userdata['billing_postcode'];
  243.  
  244.         if (isset($userdata['ship_to_different_address'])) {
  245.             $shipping_country = $userdata['shipping_country'];
  246.             $shipping_firstname = $userdata['shipping_first_name'];
  247.             $shipping_lastname = $userdata['shipping_last_name'];
  248.             $shipping_company = $userdata['shipping_company'];
  249.             $shipping_address = $userdata['shipping_address_1'];
  250.             $shipping_address1 = $userdata['shipping_address_2'];
  251.             $shipping_city = $userdata['shipping_city'];
  252.             $shipping_state = '';
  253.             if (isset($userdata['shipping_state'])) {
  254.                 $shipping_state = $userdata['shipping_state'];
  255.             } else if (isset($userdata['shipping_country_county'])) {
  256.                 $shipping_state = $userdata['shipping_country_county'];
  257.             }
  258.             $shipping_postcode = $userdata['shipping_postcode'];
  259.         } else {
  260.             $shipping_country = $billing_country;
  261.             $shipping_firstname = $billing_firstname;
  262.             $shipping_lastname = $billing_lastname;
  263.             $shipping_company = $billing_company;
  264.             $shipping_address = $billing_address;
  265.             $shipping_address1 = $billing_address1;
  266.             $shipping_city = $billing_city;
  267.             $shipping_state = $billing_state;
  268.             $shipping_postcode = $billing_postcode;
  269.         }
  270.  
  271.         /* INSERT IN TO USER TABLE */
  272.         $user_nicename = $username;
  273.         $user_registered = date('Y-m-d h:i:s');
  274.         $display_name = $billing_firstname . " " . $billing_lastname;
  275.  
  276.         if ($user_id) {
  277.             // Use 'update_user_meta()' to add or update the user information fields.
  278.             update_user_meta($user_id, 'user_nicename', $user_nicename);
  279.             update_user_meta($user_id, 'user_registered', $user_registered);
  280.             update_user_meta($user_id, 'display_name', $display_name);
  281.             update_user_meta($user_id, 'first_name', $billing_firstname);
  282.             update_user_meta($user_id, 'last_name', $billing_lastname);
  283.  
  284.             if ($new_user)
  285.                 wp_update_user(array('ID' => $user_id, 'role' => 'customer'));
  286.  
  287.             update_user_meta($user_id, 'billing_first_name', $billing_firstname);
  288.             update_user_meta($user_id, 'billing_last_name', $billing_lastname);
  289.             update_user_meta($user_id, 'billing_company', $billing_company);
  290.             update_user_meta($user_id, 'billing_address_1', $billing_address);
  291.             update_user_meta($user_id, 'billing_address_2', $billing_address1);
  292.             update_user_meta($user_id, 'billing_city', $billing_city);
  293.             update_user_meta($user_id, 'billing_postcode', $billing_postcode);
  294.             update_user_meta($user_id, 'billing_state', $billing_state);
  295.             update_user_meta($user_id, 'billing_country', $billing_country);
  296.             update_user_meta($user_id, 'billing_phone', $phone);
  297.             update_user_meta($user_id, 'billing_email', $email);
  298.             update_user_meta($user_id, 'shipping_first_name', $shipping_firstname);
  299.             update_user_meta($user_id, 'shipping_last_name', $shipping_lastname);
  300.             update_user_meta($user_id, 'shipping_company', $shipping_company);
  301.             update_user_meta($user_id, 'shipping_address_1', $shipping_address);
  302.             update_user_meta($user_id, 'shipping_address_2', $shipping_address1);
  303.             update_user_meta($user_id, 'shipping_city', $shipping_city);
  304.             update_user_meta($user_id, 'shipping_postcode', $shipping_postcode);
  305.             update_user_meta($user_id, 'shipping_state', $shipping_state);
  306.             update_user_meta($user_id, 'shipping_country', $shipping_country);
  307.  
  308.             do_action('woocommerce_checkout_update_user_meta', $user_id, $_POST);
  309.  
  310.             $success = "success";
  311.  
  312.             ob_start();
  313.  
  314.             $out = pos_get_user_html($user_id);
  315.  
  316.             ob_end_clean();
  317.             echo '<!--WC_POS_START-->' . json_encode(
  318.                     array(
  319.                         'success' => true,
  320.                         'id' => $user_id,
  321.                         'html' => $out
  322.                     )
  323.                 ) . '<!--WC_POS_END-->';
  324.  
  325.         }
  326.  
  327.         die;
  328.     }
  329.  
  330.     public function remove_product_from_register()
  331.     {
  332.         global $wpdb;
  333.         check_ajax_referer('remove_product_from_register', 'security');
  334.         $register_id = absint($_POST['register_id']);
  335.  
  336.         $id_product = absint($_POST['id_product']);
  337.         $order_id = absint($_POST['order_id']);
  338.         if (!is_numeric($id_product))
  339.             die();
  340.         wc_delete_order_item($id_product);
  341.         die('Deleted');
  342.     }
  343.  
  344.     public function update_product_quantity()
  345.     {
  346.         check_ajax_referer('add_product_to_register', 'security');
  347.         $register_id = absint($_POST['register_id']);
  348.  
  349.         $item_order_id = absint($_POST['item_order_id']);
  350.         $new_quantity = absint($_POST['new_quantity']);
  351.  
  352.         if (!is_numeric($item_order_id))
  353.             die($item_order_id);
  354.         if (!is_numeric($new_quantity))
  355.             die();
  356.  
  357.         $order_id = absint($_POST['order_id']);
  358.         $order = new WC_Order($order_id);
  359.  
  360.         $order_items = $order->get_items(apply_filters('woocommerce_admin_order_item_types', array('line_item', 'fee')));
  361.  
  362.         $_product = $order->get_product_from_item($order_items[$item_order_id]);
  363.  
  364.         $_tax = new WC_Tax();
  365.         $price = $_product->get_price();
  366.         $qty = 1;
  367.         $line_tax = 0;
  368.  
  369.         if (!defined('WOOCOMMERCE_CHECKOUT'))
  370.             define('WOOCOMMERCE_CHECKOUT', true);
  371.  
  372.  
  373.         if ($_product->is_taxable()) {
  374.             if (get_option('woocommerce_prices_include_tax') === 'no') {
  375.  
  376.                 $tax_rates = $_tax->get_rates($_product->get_tax_class());
  377.                 $taxes = $_tax->calc_tax($price * $qty, $tax_rates, false);
  378.                 $tax_amount = $_tax->get_tax_total($taxes);
  379.                 $line_tax = round($tax_amount, absint(get_option('woocommerce_price_num_decimals')));
  380.             } else {
  381.                 $tax_rates = $_tax->get_rates($_product->get_tax_class());
  382.                 $base_tax_rates = $_tax->get_shop_base_rate($_product->tax_class);
  383.                 $is_vat_exempt = false;
  384.                 if ($is_vat_exempt) {
  385.                     $base_taxes = $_tax->calc_tax($price * $qty, $base_tax_rates, true);
  386.                     $base_tax_amount = array_sum($base_taxes);
  387.                     $line_tax = round($base_tax_amount, absint(get_option('woocommerce_price_num_decimals')));
  388.  
  389.                 } elseif ($tax_rates !== $base_tax_rates) {
  390.                     $base_taxes = $_tax->calc_tax($price * $qty, $base_tax_rates, true);
  391.                     $modded_taxes = $_tax->calc_tax(($price * $qty) - array_sum($base_taxes), $tax_rates, false);
  392.                     #$line_tax              = round( array_sum( $base_taxes ) + array_sum( $modded_taxes ), absint( get_option( 'woocommerce_price_num_decimals' ) ) );
  393.                    $line_tax = round(array_sum($base_taxes), absint(get_option('woocommerce_price_num_decimals')));
  394.                 }
  395.  
  396.             }
  397.  
  398.         }
  399.         wc_update_order_item_meta($item_order_id, '_qty', apply_filters('woocommerce_stock_amount', $new_quantity));
  400.         wc_update_order_item_meta($item_order_id, '_line_tax', $line_tax);
  401.         die('updated');
  402.  
  403.     }
  404.  
  405.     public function add_products_to_register()
  406.     {
  407.         global $wpdb;
  408.         check_ajax_referer('add_product_to_register', 'security');
  409.         $register_id = absint($_POST['register_id']);
  410.  
  411.         $item_to_add = sanitize_text_field($_POST['item_to_add']);
  412.         $order_id = absint($_POST['order_id']);
  413.  
  414. // Find the item
  415.         if (!is_numeric($item_to_add))
  416.             die();
  417.  
  418.         $post = get_post($item_to_add);
  419.  
  420.         if (!$post || ($post->post_type !== 'product' && $post->post_type !== 'product_variation'))
  421.             die();
  422.  
  423.         $_product = get_product($post->ID);
  424.         $_product_id_var = $post->ID;
  425.  
  426.         $order = new WC_Order($order_id);
  427.         $class = 'new_row product_id_' . $_product_id_var;
  428.  
  429. // Set values
  430.         $item = array();
  431.  
  432.         $item['product_id'] = $_product->get_id();
  433.         $item['variation_id'] = isset($_product->variation_id) ? $_product->variation_id : '';
  434.         $item['variation_data'] = isset($_product->variation_data) ? $_product->variation_data : '';
  435.         $item['name'] = $_product->get_title();
  436.         $item['tax_class'] = $_product->get_tax_class();
  437.         $item['qty'] = 1;
  438.         $item['line_subtotal'] = wc_format_decimal($_product->get_price_excluding_tax());
  439.         $item['line_subtotal_tax'] = '';
  440.         $item['line_total'] = wc_format_decimal($_product->get_price_excluding_tax()) * $item['qty'];
  441.         $item['line_tax'] = '';
  442.  
  443.         $_tax = new WC_Tax();
  444.         $price = $_product->get_price();
  445.         $qty = 1;
  446.         $line_tax = 0;
  447.  
  448.         if (!defined('WOOCOMMERCE_CHECKOUT'))
  449.             define('WOOCOMMERCE_CHECKOUT', true);
  450.  
  451.  
  452.         if ($_product->is_taxable()) {
  453.             if (get_option('woocommerce_prices_include_tax') === 'no') {
  454.  
  455.                 $tax_rates = $_tax->get_rates($_product->get_tax_class());
  456.                 $taxes = $_tax->calc_tax($price * $qty, $tax_rates, false);
  457.                 $tax_amount = $_tax->get_tax_total($taxes);
  458.                 $line_tax = round($tax_amount, absint(get_option('woocommerce_price_num_decimals')));
  459.             } else {
  460.                 $tax_rates = $_tax->get_rates($_product->get_tax_class());
  461.                 $base_tax_rates = $_tax->get_shop_base_rate($_product->tax_class);
  462.                 $is_vat_exempt = false;
  463.                 if ($is_vat_exempt) {
  464.                     $base_taxes = $_tax->calc_tax($price * $qty, $base_tax_rates, true);
  465.                     $base_tax_amount = array_sum($base_taxes);
  466.                     $line_tax = round($base_tax_amount, absint(get_option('woocommerce_price_num_decimals')));
  467.  
  468.                 } elseif ($tax_rates !== $base_tax_rates) {
  469.                     $base_taxes = $_tax->calc_tax($price * $qty, $base_tax_rates, true);
  470.                     $modded_taxes = $_tax->calc_tax(($price * $qty) - array_sum($base_taxes), $tax_rates, false);
  471.                     #$line_tax              = round( array_sum( $base_taxes ) + array_sum( $modded_taxes ), absint( get_option( 'woocommerce_price_num_decimals' ) ) );
  472.                    $line_tax = round(array_sum($base_taxes), absint(get_option('woocommerce_price_num_decimals')));
  473.                 }
  474.  
  475.             }
  476.  
  477.         }
  478.         if ($line_tax) $item['line_tax'] = $line_tax;
  479.  
  480.  
  481. // Add line item
  482.         $item_id = wc_add_order_item($order_id, array(
  483.             'order_item_name' => $item['name'],
  484.             'order_item_type' => 'line_item'
  485.         ));
  486.  
  487. // Add line item meta
  488.         if ($item_id) {
  489.             wc_add_order_item_meta($item_id, '_qty', $item['qty']);
  490.             wc_add_order_item_meta($item_id, '_tax_class', $item['tax_class']);
  491.             wc_add_order_item_meta($item_id, '_product_id', $item['product_id']);
  492.             wc_add_order_item_meta($item_id, '_variation_id', $item['variation_id']);
  493.             wc_add_order_item_meta($item_id, '_line_subtotal', $item['line_subtotal']);
  494.             wc_add_order_item_meta($item_id, '_line_subtotal_tax', $item['line_subtotal_tax']);
  495.             wc_add_order_item_meta($item_id, '_line_total', $item['line_total']);
  496.             wc_add_order_item_meta($item_id, '_line_tax', $item['line_tax']);
  497.  
  498. // Store variation data in meta
  499.             if ($item['variation_data'] && is_array($item['variation_data'])) {
  500.                 foreach ($item['variation_data'] as $key => $value) {
  501.                     wc_add_order_item_meta($item_id, str_replace('attribute_', '', $key), $value);
  502.                 }
  503.             }
  504.  
  505.             do_action('woocommerce_ajax_add_order_item_meta', $item_id, $item);
  506.         }
  507.  
  508.  
  509.         $item = apply_filters('woocommerce_ajax_order_item', $item, $item_id);
  510.  
  511.         require_once(dirname(realpath(dirname(__FILE__))) . '/views/html-admin-registers-product-item.php');
  512.  
  513.         die();
  514.     }
  515.  
  516.     public function add_customers_to_register()
  517.     {
  518.         global $wpdb;
  519.         check_ajax_referer('add_customers_to_register', 'security');
  520.  
  521.         $user_to_add = absint($_POST['user_to_add']);
  522.  
  523.         pos_get_user_html($user_to_add);
  524.         die;
  525.     }
  526.  
  527.     public function check_shipping()
  528.     {
  529.         global $wpdb;
  530.  
  531.         check_ajax_referer('check_shipping', 'security');
  532.  
  533.         $register_id = absint($_POST['register_id']);
  534.  
  535.         $user_id = isset($_POST['user_to_add']) ? absint($_POST['user_to_add']) : 0;
  536.         if (!$user_id) die();
  537.  
  538.  
  539.         $products_ids = $_POST['products_ids'];
  540.         parse_str($products_ids, $ids);
  541.         $ids = $ids['product_item_id'];
  542.  
  543.         $products_qt = $_POST['products_qt'];
  544.         parse_str($products_qt, $qty);
  545.         $qty = $qty['order_item_qty'];
  546.  
  547.         $session_class = apply_filters('woocommerce_session_handler', 'WC_Session_Handler');
  548.         WC()->cart = new WC_Cart();
  549.         WC()->customer = new WC_Customer();
  550.         WC()->shipping = new WC_Shipping();
  551.         WC()->session = new $session_class();
  552.  
  553.         $user_info = get_user_meta($user_id);
  554.  
  555.         $country = isset($user_info['billing_country']) ? $user_info['billing_country'][0] : '';
  556.         $state = isset($user_info['billing_state']) ? $user_info['billing_state'][0] : '';
  557.         $postcode = isset($user_info['billing_postcode']) ? $user_info['billing_postcode'][0] : '';
  558.         $city = isset($user_info['billing_city']) ? $user_info['billing_city'][0] : '';
  559.  
  560.         if (isset($user_info['shipping_country']) && $s_country = $user_info['shipping_country'][0]) {
  561.             $s_state = isset($user_info['shipping_state']) ? $user_info['shipping_state'][0] : '';
  562.             $s_postcode = isset($user_info['shipping_postcode']) ? $user_info['shipping_postcode'][0] : '';
  563.             $s_city = isset($user_info['shipping_city']) ? $user_info['shipping_city'][0] : '';
  564.         } else {
  565.             $s_country = $country;
  566.             $s_state = $state;
  567.             $s_postcode = $postcode;
  568.             $s_city = $city;
  569.         }
  570.  
  571.         WC()->customer->set_location($country, $state, $postcode, $city);
  572.         WC()->customer->set_shipping_location($s_country, $s_state, $s_postcode, $s_city);
  573.  
  574.  
  575.         foreach ($ids as $key => $id) {
  576.             $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($id));
  577.             $quantity = empty($qty[$key]) ? 1 : apply_filters('woocommerce_stock_amount', $qty[$key]);
  578.             $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $id, $quantity);
  579.             if ($passed_validation) {
  580.                 WC()->cart->add_to_cart($id, $quantity);
  581.             }
  582.         }
  583.  
  584.         if (!defined('WOOCOMMERCE_CART'))
  585.             define('WOOCOMMERCE_CART', true);
  586.         WC()->cart->calculate_totals();
  587.         WC()->cart->calculate_shipping();
  588.  
  589.         if (WC()->cart->needs_shipping() && WC()->cart->show_shipping()) :
  590.             $packages = WC()->shipping->get_packages();
  591.  
  592.             foreach ($packages as $i => $package) {
  593.                 $chosen_method = 'no_shipping';
  594.                 $available_methods = $package['rates'];
  595.                 $show_package_details = (sizeof($packages) > 1);
  596.                 $index = $i;
  597.                 require(dirname(realpath(dirname(__FILE__))) . '/views/html-admin-cart-shipping.php');
  598.             }
  599.  
  600.         endif;
  601.         // Remove cart
  602.         WC()->cart->empty_cart();
  603.  
  604.         die();
  605.     }
  606.  
  607.     public function search_variations_for_product()
  608.     {
  609.         global $wpdb;
  610.         check_ajax_referer('search_variations_for_product', 'security');
  611.         $id_product = absint($_POST['id_product']);
  612.         $args = array(
  613.             'post_type' => array('product_variation'),
  614.             'posts_per_page' => -1,
  615.             'post_status' => 'publish',
  616.             'order' => 'ASC',
  617.             'orderby' => 'parent title',
  618.             'post_parent' => $id_product,
  619.         );
  620.  
  621.         $posts = get_posts($args);
  622.         $found_products = array();
  623.  
  624.         if ($posts) {
  625.             foreach ($posts as $post) {
  626.                 $product = get_product($post->ID);
  627.                 $image = '';
  628.                 $size = 'shop_thumbnail';
  629.                 if (has_post_thumbnail($post->ID)) {
  630.                     $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $size);
  631.                     $image = $thumbnail[0];
  632.                 } elseif (($parent_id = wp_get_post_parent_id($post->ID)) && has_post_thumbnail($parent_id)) {
  633.                     $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($parent_id), $size);
  634.                     $image = $thumbnail[0];
  635.                 } else {
  636.                     $image = wc_placeholder_img_src();
  637.                 }
  638.                 if (!$image || $image == NULL) $image = wc_placeholder_img_src();
  639.  
  640.                 $found_products[$post->ID]['formatted_name'] = $product->get_formatted_name();
  641.                 $found_products[$post->ID]['image'] = $image;
  642.             }
  643.         }
  644.         if (!empty($found_products))
  645.             echo json_encode($found_products);
  646.         die();
  647.     }
  648.  
  649.     public function search_variations_for_product_and_sku()
  650.     {
  651.         global $wpdb;
  652.         check_ajax_referer('search_variations_for_product_and_sku', 'security');
  653.         $id_product = absint($_POST['id_product']);
  654.         $__product = get_product($id_product);
  655.         $sku = $__product->get_sku();
  656.         $price = woocommerce_price($__product->get_price());
  657.         $args = array(
  658.             'post_type' => array('product_variation'),
  659.             'posts_per_page' => -1,
  660.             'post_status' => 'publish',
  661.             'order' => 'ASC',
  662.             'orderby' => 'parent title',
  663.             'post_parent' => $id_product,
  664.         );
  665.  
  666.         $posts = get_posts($args);
  667.         $variation = array();
  668.  
  669.         if ($posts) {
  670.             foreach ($posts as $post) {
  671.                 $product = get_product($post->ID);
  672.                 $variation[$post->ID] = array(
  673.                     'name' => $product->get_formatted_name(),
  674.                     'sku' => $product->get_sku(),
  675.                 );
  676.             }
  677.         }
  678.         echo json_encode(array('sku' => $sku, 'price' => $price, 'variation' => $variation));
  679.         die();
  680.     }
  681.  
  682.     /**
  683.      * Ajax request handling for tiles ordering
  684.      */
  685.     public function tile_ordering()
  686.     {
  687.         global $wpdb;
  688.  
  689.         $id = (int)$_POST['id'];
  690.         $grid_id = (int)$_POST['grid_id'];
  691.         $next_id = isset($_POST['nextid']) && (int)$_POST['nextid'] ? (int)$_POST['nextid'] : null;
  692.  
  693.         if (!$id || !$grid_id) die(0);
  694.         $index = 0;
  695.         $table_name = $wpdb->prefix . 'wc_poin_of_sale_tiles';
  696.         $all_tiles = $tiles = $wpdb->get_results("SELECT * FROM  $table_name  WHERE grid_id = $grid_id ORDER BY order_position ASC");
  697.  
  698.         if (empty($all_tiles)) die($index);
  699.  
  700.         foreach ($all_tiles as $tile) {
  701.  
  702.             if ($tile->ID == $id) { // our tile to order, we skip
  703.                 continue; // our tile to order, we skip
  704.             }
  705.             // the nextid of our tile to order, lets move our tile here
  706.             if (null !== $next_id && $tile->ID == $next_id) {
  707.                 $index++;
  708.                 $wpdb->update($table_name, array('order_position' => $index), array('ID' => $id));
  709.             }
  710.  
  711.             // set order
  712.             $index++;
  713.             $wpdb->update($table_name, array('order_position' => $index), array('ID' => $tile->ID));
  714.         }
  715.         if (null === $next_id) {
  716.             $index++;
  717.             $wpdb->update($table_name, array('order_position' => $index), array('ID' => $id));
  718.         }
  719.         die($index);
  720.     }
  721.  
  722.     /**
  723.      * Search for customers and return json
  724.      */
  725.     public static function json_search_customers()
  726.     {
  727.         ob_start();
  728.  
  729.         check_ajax_referer('search-customers', 'security');
  730.  
  731.         $term = wc_clean(stripslashes($_GET['term']));
  732.  
  733.         if (empty($term)) {
  734.             die();
  735.         }
  736.  
  737.         $default = isset($_GET['default']) ? $_GET['default'] : __('Guest', 'woocommerce');
  738.  
  739.         $found_customers = array('' => $default);
  740.  
  741.         add_action('pre_user_query', array(__CLASS__, 'json_search_customer_name'));
  742.  
  743.         $customers_query = new WP_User_Query(apply_filters('woocommerce_json_search_customers_query', array(
  744.             'fields' => 'all',
  745.             'orderby' => 'display_name',
  746.             'search' => '*' . $term . '*',
  747.             'search_columns' => array('ID', 'user_login', 'user_email', 'user_nicename')
  748.         )));
  749.  
  750.         remove_action('pre_user_query', array(__CLASS__, 'json_search_customer_name'));
  751.  
  752.         $customers = $customers_query->get_results();
  753.  
  754.         if ($customers) {
  755.             foreach ($customers as $customer) {
  756.                 $found_customers[$customer->ID] = $customer->first_name . ' ' . $customer->last_name . ' &ndash; ' . sanitize_email($customer->user_email);
  757.             }
  758.         }
  759.  
  760.         wp_send_json($found_customers);
  761.  
  762.     }
  763.  
  764.     /**
  765.      * When searching using the WP_User_Query, search names (user meta) too
  766.      * @param  object $query
  767.      * @return object
  768.      */
  769.     public static function json_search_customer_name($query)
  770.     {
  771.         global $wpdb;
  772.  
  773.         $term = wc_clean(stripslashes($_GET['term']));
  774.         if (method_exists($wpdb, 'esc_like')) {
  775.             $term = $wpdb->esc_like($term);
  776.         } else {
  777.             $term = like_escape($term);
  778.         }
  779.  
  780.         $query->query_from .= " INNER JOIN {$wpdb->usermeta} AS user_name ON {$wpdb->users}.ID = user_name.user_id AND ( user_name.meta_key = 'first_name' OR user_name.meta_key = 'last_name' ) ";
  781.         $query->query_where .= $wpdb->prepare(" OR user_name.meta_value LIKE %s ", '%' . $term . '%');
  782.     }
  783.  
  784.     public function json_search_usernames()
  785.     {
  786.         global $wpdb;
  787.  
  788.         check_ajax_referer('search-usernames', 'security');
  789.  
  790.         header('Content-Type: application/json; charset=utf-8');
  791.  
  792.         $term = urldecode(stripslashes(strip_tags($_GET['term'])));
  793.  
  794.         if (empty($term))
  795.             die();
  796.  
  797.         $found_users = array();
  798.  
  799.         $data = WC_POS()->user()->get_data($term);
  800.  
  801.         if ($data) {
  802.             foreach ($data as $userid => $user) {
  803.                 $found_users[$userid] = $user['username'];
  804.             }
  805.         }
  806.  
  807.         echo json_encode($found_users);
  808.         die();
  809.     }
  810.  
  811.     function stripe_get_outlet_address()
  812.     {
  813.         global $wpdb;
  814.         $outlet_id = $_POST['outlet_id'];
  815.         $table_name = $wpdb->prefix . "wc_poin_of_sale_outlets";
  816.         $db_data = $wpdb->get_results("SELECT * FROM $table_name WHERE ID = $outlet_id");
  817.         $data = null;
  818.  
  819.         foreach ($db_data as $value) {
  820.             $value->contact = (array)json_decode($value->contact);
  821.             $data = get_object_vars($value);
  822.         }
  823.         die(json_encode($data));
  824.     }
  825.  
  826.     public function json_search_variation_pr($parent_id, $v_id)
  827.     {
  828.         if (!$parent_id || empty($parent_id)) return false;
  829.         if (!$v_id || empty($v_id)) return false;
  830.  
  831.         $found_products = array();
  832.  
  833.         $product = wc_get_product($v_id);
  834.         $id = $v_id;
  835.  
  836.         $title = "";
  837.         $f_title = "";
  838.  
  839.         $tips = '<strong>' . __('Product ID:', 'woocommerce') . '</strong> ' . absint($parent_id);
  840.         $tips .= '<br/><strong>' . __('Variation ID:', 'woocommerce') . '</strong> ' . absint($id);
  841.  
  842.         $sku = '';
  843.         if ($product && $product->get_sku()) {
  844.             $tips .= '<br/><strong>' . __('Product SKU:', 'woocommerce') . '</strong> ' . esc_html($product->get_sku());
  845.             $title .= esc_html($product->get_sku()) . ' &ndash; ';
  846.             $f_title .= esc_html($product->get_sku()) . ' &ndash; ';
  847.             $sku = esc_html($product->get_sku());
  848.         }
  849.         $title .= '<a target="_blank" href="' . esc_url(admin_url('post.php?post=' . absint($id) . '&action=edit')) . '">' . esc_html($product->get_title()) . '</a>';
  850.         $f_title .= esc_html($product->get_title());
  851.  
  852.  
  853.         $variation_data = array();
  854.         if ($product && isset($product->variation_data)) {
  855.             $f_title .= ' &ndash; ';
  856.             $f_var = '';
  857.             $tips .= '<br/>' . wc_get_formatted_variation($product->variation_data, true);
  858.             $i = 0;
  859.  
  860.             $attributes = (array)maybe_unserialize(get_post_meta($parent_id, '_product_attributes', true));
  861.  
  862.  
  863.             foreach ($product->variation_data as $names => $value) {
  864.                 if (!$value) {
  865.                     continue;
  866.                 }
  867.                 $name = str_replace('attribute_', '', $names);
  868.                 if (isset($attributes[$name])) {
  869.  
  870.                     if ($attributes[$name]['is_taxonomy']) {
  871.  
  872.                         $rental_features = get_taxonomy($name);
  873.                         $variation_data[$i][1] = $rental_features->label;
  874.  
  875.                         $post_terms = wp_get_post_terms($parent_id, $attributes[$name]['name']);
  876.  
  877.                         foreach ($post_terms as $term) {
  878.                             if ($term->slug == $value) {
  879.                                 $variation_data[$i][2] = $term->name;
  880.                                 break;
  881.                             }
  882.                         }
  883.                     } else {
  884.                         $variation_data[$i][1] = $attributes[$name]['name'];
  885.                         $variation_data[$i][2] = '';
  886.                         $options = array_map('trim', explode(WC_DELIMITER, $attributes[$name]['value']));
  887.                         foreach ($options as $option) {
  888.                             if (sanitize_title($option) == $value) {
  889.                                 $variation_data[$i][2] = $option;
  890.                                 break;
  891.                             }
  892.                         }
  893.                     }
  894.                 }
  895.  
  896.                 if (!empty($f_var)) $f_var .= ', ';
  897.                 $f_var .= $variation_data[$i][2];
  898.                 $i++;
  899.             }
  900.             $f_title .= $f_var . ' &ndash; ';
  901.             $f_title .= wc_price($product->get_price());
  902.         }
  903.         $image = '';
  904.         $size = 'shop_thumbnail';
  905.         if (has_post_thumbnail($id)) {
  906.             $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($id), $size);
  907.             $image = $thumbnail[0];
  908.         } elseif (($parent_id = wp_get_post_parent_id($id)) && has_post_thumbnail($parent_id)) {
  909.             $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($parent_id), $size);
  910.             $image = $thumbnail[0];
  911.         } else {
  912.             $image = wc_placeholder_img_src();
  913.         }
  914.         if (!$image || $image == NULL) $image = wc_placeholder_img_src();
  915.  
  916.         $found_products['pid'] = $id;
  917.         $found_products['title'] = $title;
  918.         $found_products['f_title'] = $f_title;
  919.         $found_products['stock'] = $product->get_stock_quantity();
  920.         $found_products['sku'] = $sku;
  921.         $found_products['price'] = $product->get_price();
  922.         $found_products['f_price'] = wc_price($product->get_price());
  923.         $found_products['tax'] = 0;
  924.         $found_products['pr_inc_tax'] = wc_get_price_including_tax($product);
  925.         $found_products['pr_excl_tax'] = wc_get_price_excluding_tax($product);
  926.         $found_products['tip'] = $tips;
  927.         $found_products['variation'] = json_encode($variation_data);
  928.         $found_products['image'] = $image;
  929.         return $found_products;
  930.     }
  931.  
  932.     public function json_search_products_all()
  933.     {
  934.         check_ajax_referer('search-products', 'security');
  935.         $this->json_headers();
  936.         $args = array(
  937.             'post_type' => array('product'),
  938.             'posts_per_page' => -1,
  939.             'post_status' => 'publish',
  940.             'order' => 'ASC',
  941.             'orderby' => 'ID'
  942.         );
  943.  
  944.         $found_products = array();
  945.         $posts = get_posts($args);
  946.         if ($posts) {
  947.             foreach ($posts as $post) {
  948.                 $product = wc_get_product($post->ID);
  949.  
  950.                 $id = $product->get_id();
  951.  
  952.                 if ($product->is_type('variable')) {
  953.                     $variations = $product->get_available_variations();//todo: maybe deprecated
  954.  
  955.                     foreach ($variations as $key => $variation_value) {
  956.                         if ($variation_pr = $this->json_search_variation_pr($id, $variation_value['variation_id'])) {
  957.                             $found_products[$id]['children'][] = $variation_value['variation_id'];
  958.                             $found_products[$variation_value['variation_id']] = $variation_pr;
  959.                         }
  960.                     }
  961.                 }
  962.  
  963.                 $title = "";
  964.                 $f_title = "";
  965.                 $tips = '<strong>' . __('Product ID:', 'woocommerce') . '</strong> ' . absint($id);
  966.  
  967.                 $sku = '';
  968.                 if ($product && $product->get_sku()) {
  969.                     $tips .= '<br/><strong>' . __('Product SKU:', 'woocommerce') . '</strong> ' . esc_html($product->get_sku());
  970.                     $title .= esc_html($product->get_sku()) . ' &ndash; ';
  971.                     $f_title .= esc_html($product->get_sku()) . ' &ndash; ';
  972.                     $sku = esc_html($product->get_sku());
  973.                 }
  974.  
  975.  
  976.                 $title .= '<a target="_blank" href="' . esc_url(admin_url('post.php?post=' . absint($id) . '&action=edit')) . '">' . esc_html($product->get_title()) . '</a>';
  977.                 $f_title .= esc_html($product->get_title());
  978.  
  979.                 $variation_data = array();
  980.  
  981.  
  982.                 $image = '';
  983.                 $size = 'shop_thumbnail';
  984.                 if (has_post_thumbnail($id)) {
  985.                     $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($id), $size);
  986.                     $image = $thumbnail[0];
  987.                 } else {
  988.                     $image = wc_placeholder_img_src();
  989.                 }
  990.                 if (!$image || $image == NULL) $image = wc_placeholder_img_src();
  991.  
  992.                 $found_products[$id]['pid'] = $id;
  993.                 $found_products[$id]['title'] = $title;
  994.                 $found_products[$id]['f_title'] = $f_title;
  995.                 $found_products[$id]['stock'] = $product->get_stock_quantity();
  996.                 $found_products[$id]['sku'] = $sku;
  997.                 $found_products[$id]['price'] = $product->get_price();
  998.                 $found_products[$id]['f_price'] = wc_price($product->get_price());
  999.                 $found_products[$id]['tax'] = 0;
  1000.                 $found_products[$id]['pr_inc_tax'] = wc_get_price_including_tax($product);
  1001.                 $found_products[$id]['pr_excl_tax'] = wc_get_price_excluding_tax($product);
  1002.                 $found_products[$id]['tip'] = $tips;
  1003.                 $found_products[$id]['variation'] = json_encode($variation_data);
  1004.                 $found_products[$id]['image'] = $image;
  1005.  
  1006.                 $attributes = (array)maybe_unserialize(get_post_meta($id, '_product_attributes', true));
  1007.                 $default_attributes = maybe_unserialize(get_post_meta($id, '_default_attributes', true));
  1008.  
  1009.                 if (!empty($attributes)) {
  1010.                     $found_products[$id]['all_var'] = '';
  1011.                     foreach ($attributes as $attribute) {
  1012.  
  1013.                         if (empty($attribute))
  1014.                             continue;
  1015.  
  1016.                         // Only deal with attributes that are variations
  1017.                         if (!$attribute['is_variation'])
  1018.                             continue;
  1019.  
  1020.  
  1021.                         // Get terms for attribute taxonomy or value if its a custom attribute
  1022.                         if ($attribute['is_taxonomy']) {
  1023.  
  1024.                             $rental_features = get_taxonomy($attribute['name']);
  1025.  
  1026.                             $found_products[$id]['all_var'] .= '<select data-label="' . $rental_features->label . '" ><option value="">' . __('No default', 'woocommerce') . ' ' . esc_html(wc_attribute_label($attribute['name'])) . '&hellip;</option>';
  1027.  
  1028.                             $post_terms = wp_get_post_terms($post->ID, $attribute['name']);
  1029.  
  1030.                             foreach ($post_terms as $term)
  1031.                                 $found_products[$id]['all_var'] .= '<option  value="' . esc_attr($term->name) . '">' . esc_attr($term->name) . '</option>';
  1032.  
  1033.                         } else {
  1034.  
  1035.                             $found_products[$id]['all_var'] .= '<select data-label="' . $attribute['name'] . '" ><option value="">' . __('No default', 'woocommerce') . ' ' . esc_html(wc_attribute_label($attribute['name'])) . '&hellip;</option>';
  1036.  
  1037.                             $options = array_map('trim', explode(WC_DELIMITER, $attribute['value']));
  1038.  
  1039.                             foreach ($options as $option)
  1040.                                 $found_products[$id]['all_var'] .= '<option  value="' . esc_attr($option) . '">' . esc_attr($option) . '</option>';
  1041.  
  1042.                         }
  1043.  
  1044.                         $found_products[$id]['all_var'] .= '</select>';
  1045.                     }
  1046.  
  1047.                 }
  1048.                 //{"pid" : 83, "title" : "Some text", "stock" : 15, "price": 3.5, "tax": 3.5, "image": "", "variation": "", "tip" : "" },
  1049.             }
  1050.         }
  1051.  
  1052.         $found_products = apply_filters('wc_pos_json_search_found_products', $found_products);
  1053.  
  1054.         echo json_encode($found_products);
  1055.  
  1056.         die();
  1057.     }
  1058.  
  1059.     /**
  1060.      * Search for products and echo json
  1061.      *
  1062.      * @param string $x (default: '')
  1063.      * @param string $post_types (default: array('product'))
  1064.      */
  1065.     public function json_search_products($x = '', $post_types = array('product'))
  1066.     {
  1067.  
  1068.         check_ajax_referer('search-products', 'security');
  1069.  
  1070.         $this->json_headers();
  1071.  
  1072.         $term = (string)wc_clean(stripslashes($_GET['term']));
  1073.  
  1074.         if (empty($term)) {
  1075.             die();
  1076.         }
  1077.  
  1078.         if (is_numeric($term)) {
  1079.  
  1080.             $args = array(
  1081.                 'post_type' => $post_types,
  1082.                 'post_status' => 'publish',
  1083.                 'posts_per_page' => -1,
  1084.                 'post__in' => array(0, $term),
  1085.                 'fields' => 'ids'
  1086.             );
  1087.  
  1088.             $args2 = array(
  1089.                 'post_type' => $post_types,
  1090.                 'post_status' => 'publish',
  1091.                 'posts_per_page' => -1,
  1092.                 'post_parent' => $term,
  1093.                 'fields' => 'ids'
  1094.             );
  1095.  
  1096.             $args3 = array(
  1097.                 'post_type' => $post_types,
  1098.                 'post_status' => 'publish',
  1099.                 'posts_per_page' => -1,
  1100.                 'meta_query' => array(
  1101.                     array(
  1102.                         'key' => '_sku',
  1103.                         'value' => $term,
  1104.                         'compare' => 'LIKE'
  1105.                     )
  1106.                 ),
  1107.                 'fields' => 'ids'
  1108.             );
  1109.  
  1110.             $posts = array_unique(array_merge(get_posts($args), get_posts($args2), get_posts($args3)));
  1111.  
  1112.         } else {
  1113.  
  1114.             $args = array(
  1115.                 'post_type' => $post_types,
  1116.                 'post_status' => 'publish',
  1117.                 'posts_per_page' => -1,
  1118.                 's' => $term,
  1119.                 'fields' => 'ids'
  1120.             );
  1121.  
  1122.             $args2 = array(
  1123.                 'post_type' => $post_types,
  1124.                 'post_status' => 'publish',
  1125.                 'posts_per_page' => -1,
  1126.                 'meta_query' => array(
  1127.                     array(
  1128.                         'key' => '_sku',
  1129.                         'value' => $term,
  1130.                         'compare' => 'LIKE'
  1131.                     )
  1132.                 ),
  1133.                 'fields' => 'ids'
  1134.             );
  1135.  
  1136.             $posts = array_unique(array_merge(get_posts($args), get_posts($args2)));
  1137.  
  1138.         }
  1139.  
  1140.         $found_products = array();
  1141.  
  1142.         if ($posts) {
  1143.             foreach ($posts as $post) {
  1144.                 $product = get_product($post);
  1145.  
  1146.                 $image = '';
  1147.                 $size = 'shop_thumbnail';
  1148.                 if (has_post_thumbnail($post)) {
  1149.                     $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post), $size);
  1150.                     $image = $thumbnail[0];
  1151.                 } elseif (($parent_id = wp_get_post_parent_id($post)) && has_post_thumbnail($parent_id)) {
  1152.                     $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($parent_id), $size);
  1153.                     $image = $thumbnail[0];
  1154.                 } else {
  1155.                     $image = wc_placeholder_img_src();
  1156.                 }
  1157.                 if (!$image || $image == NULL) $image = wc_placeholder_img_src();
  1158.  
  1159.                 $found_products[$post]['formatted_name'] = $product->get_formatted_name();
  1160.                 $found_products[$post]['name'] = $product->post->post_title;
  1161.                 $found_products[$post]['image'] = $image;
  1162.             }
  1163.         }
  1164.  
  1165.         $found_products = apply_filters('wc_pos_json_search_found_products', $found_products);
  1166.  
  1167.         echo json_encode($found_products);
  1168.  
  1169.         die();
  1170.     }
  1171.  
  1172.     public function find_variantion_by_attributes()
  1173.     {
  1174.  
  1175.         check_ajax_referer('search-products', 'security');
  1176.  
  1177.         $this->json_headers();
  1178.  
  1179.         $attributes = $_POST['attributes'];
  1180.         $register_id = absint($_POST['register_id']);
  1181.         $parent = absint($_POST['parent']);
  1182.  
  1183.         if (empty($attributes)) {
  1184.             die();
  1185.         }
  1186.         $new_attr = array();
  1187.         foreach ($attributes as $value) {
  1188.             $new_attr['attribute_' . sanitize_title($value['name'])] = $value['option'];
  1189.         }
  1190.  
  1191.         $args = array(
  1192.             'post_type' => array('product_variation'),
  1193.             'posts_per_page' => -1,
  1194.             'post_status' => 'publish',
  1195.             'order' => 'ASC',
  1196.             'orderby' => 'parent title',
  1197.             'post_parent' => $parent,
  1198.         );
  1199.  
  1200.         $posts = get_posts($args);
  1201.  
  1202.         $found_products = array();
  1203.  
  1204.         if ($posts) {
  1205.             foreach ($posts as $post) {
  1206.                 $product = get_product($post);
  1207.  
  1208.                 if ($new_attr == $product->variation_data) {
  1209.                     $found_products['id'] = $post->ID;
  1210.                 }
  1211.  
  1212.             }
  1213.         }
  1214.  
  1215.  
  1216.         echo json_encode($found_products);
  1217.         die();
  1218.     }
  1219.  
  1220.     function add_product_grid()
  1221.     {
  1222.         check_ajax_referer('add-product_grid', 'security');
  1223.         global $wpdb;
  1224.         $grid_name = $_POST['term'];
  1225.         $grid_label = sanitize_title($grid_name);
  1226.  
  1227.         $grid_label = _truncate_post_slug($grid_label, 255);
  1228.         $check_sql = "SELECT label FROM {$wpdb->prefix}wc_poin_of_sale_grids WHERE label = '%s' LIMIT 1";
  1229.  
  1230.         $grid_label_check = $wpdb->get_var($wpdb->prepare($check_sql, $grid_label));
  1231.  
  1232.  
  1233.         if ($grid_label_check) {
  1234.             $suffix = 1;
  1235.             do {
  1236.                 $alt_grid_label = _truncate_post_slug($grid_label, 255 - (strlen($suffix) + 1)) . "-$suffix";
  1237.                 $grid_label_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_grid_label));
  1238.                 $suffix++;
  1239.             } while ($grid_label_check);
  1240.             $grid_label = $alt_grid_label;
  1241.         }
  1242.  
  1243.         $grid = array(
  1244.             'label' => $grid_label,
  1245.             'name' => $grid_name
  1246.         );
  1247.         // insert gird layout data  its table "wp_wc_poin_of_sale_grids"
  1248.         if ($wpdb->insert($wpdb->prefix . 'wc_poin_of_sale_grids', $grid)) {
  1249.             do_action('woocommerce_grid_added', $wpdb->insert_id, $grid);
  1250.             echo $wpdb->insert_id;
  1251.             die();
  1252.         }
  1253.  
  1254.     }
  1255.  
  1256.     /**
  1257.      * Get all the product ids
  1258.      * @return json
  1259.      */
  1260.     public function get_server_product_ids()
  1261.     {
  1262.  
  1263.         $args = array(
  1264.             'post_type' => array('product', 'product_variation'),
  1265.             'post_status' => array('publish'),
  1266.             'posts_per_page' => -1,
  1267.             'fields' => 'ids'
  1268.         );
  1269.  
  1270.         $query = new WP_Query($args);
  1271.         $ids = array_map('intval', $query->posts);
  1272.  
  1273.         $this->json_headers();
  1274.         echo json_encode($ids);
  1275.         die();
  1276.     }
  1277.  
  1278.     function checkout()
  1279.     {
  1280.         if (!defined('WOOCOMMERCE_CHECKOUT')) {
  1281.             define('WOOCOMMERCE_CHECKOUT', true);
  1282.         }
  1283.         if (!defined('WC_POS_CHECKOUT')) {
  1284.             define('WC_POS_CHECKOUT', true);
  1285.         }
  1286.         $id = 0;
  1287.  
  1288.         $id_register = $_POST['id_register'];
  1289.         if (isset($_POST['user_id'])) {
  1290.             $user_id = $_POST['user_id'];
  1291.         } else {
  1292.             $user_id = 0;
  1293.         }
  1294.  
  1295.         $enabled_gateways = get_option('pos_enabled_gateways', array());
  1296.         $pos_exist_gateways = get_option('pos_exist_gateways', array());
  1297.  
  1298.         foreach ($pos_exist_gateways as $gateway_id) {
  1299.             if (!in_array($gateway_id, $enabled_gateways)) {
  1300.                 add_filter('option_woocommerce_' . $gateway_id . '_settings', array(WC_POS(), 'disable_gateway'));
  1301.             } else {
  1302.                 add_filter('option_woocommerce_' . $gateway_id . '_settings', array(WC_POS(), 'enable_gateway'));
  1303.             }
  1304.  
  1305.         }
  1306.  
  1307.         if (isset($_POST['id']) && $_POST['id'] != '') {
  1308.             $id = $_POST['id'];
  1309.             new WC_Pos_Checkout($id, $user_id);
  1310.             #new WC_Pos_Registers_Orders($id , $user_id);
  1311.        } else {
  1312.             wc_add_notice('<strong>Register id </strong> ' . __('is a required field.', 'woocommerce'), 'error');
  1313.         }
  1314.  
  1315.         die(0);
  1316.     }
  1317.  
  1318.     public function void_register()
  1319.     {
  1320.         check_ajax_referer('void_register', 'security');
  1321.  
  1322.         if (!isset($_POST['register_id']) || !isset($_POST['register_id'])) {
  1323.             echo json_encode(array('result' => 'error'));
  1324.             die;
  1325.         }
  1326.         $order_id = $_POST['order_id'];
  1327.         $register_id = $_POST['register_id'];
  1328.         $order_type = get_post_type($order_id);
  1329.         $result = array('result' => 'ok', 'order_id' => $order_id);
  1330.  
  1331.         if ($order_type == 'shop_order') {
  1332.             $status = get_post_status($order_id);
  1333.             $order = new WC_Order($order_id);
  1334.             if ($order)
  1335.                 $order->update_status('cancelled');
  1336.             $register = wc_pos_get_register($register_id);
  1337.             if ($register) {
  1338.                 $order_id = (int)$register->order_id;
  1339.                 $order_type = get_post_type($order_id);
  1340.                 if ($order_type == 'pos_temp_register_or') {
  1341.                     $result['order_id'] = $order_id;
  1342.                 } else {
  1343.                     $order_id = WC_POS()->register()->crate_order_id($register_id);
  1344.                     $result['order_id'] = $order_id;
  1345.                 }
  1346.             }
  1347.         }
  1348.         echo json_encode($result);
  1349.         die;
  1350.     }
  1351.  
  1352.     public function search_order_by_code()
  1353.     {
  1354.  
  1355.         global $wpdb;
  1356.         if (!isset($_GET['code']) || empty($_GET['code'])) {
  1357.             echo 'error';
  1358.             die;
  1359.         }
  1360.         $code = $_GET['code'];
  1361.  
  1362.         $result = $wpdb->get_var("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'wc_pos_prefix_suffix_order_number' AND ( meta_value = '{$code}' OR meta_value = '#{$code}') LIMIT 1");
  1363.         if (!$result) {
  1364.             $int = intval(preg_replace('/[^0-9]+/', '', $code), 10);
  1365.             $result = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE ID = {$int} LIMIT 1");
  1366.         }
  1367.  
  1368.         if ($result)
  1369.             echo get_edit_post_link($result);
  1370.         else
  1371.             echo 'error';
  1372.  
  1373.         die;
  1374.  
  1375.     }
  1376.  
  1377.  
  1378.     /**
  1379.      * Search for registers and echo json
  1380.      *
  1381.      */
  1382.     public function json_search_registers()
  1383.     {
  1384.         ob_start();
  1385.  
  1386.         check_ajax_referer('search-products', 'security');
  1387.  
  1388.         $term = (string)wc_clean(stripslashes($_GET['term']));
  1389.  
  1390.         if (empty($term)) {
  1391.             die();
  1392.         }
  1393.  
  1394.         global $wpdb;
  1395.         $table_name = $wpdb->prefix . "wc_poin_of_sale_registers";
  1396.         $registers = $wpdb->get_results("SELECT * FROM $table_name WHERE name LIKE '%{$term}%' OR slug LIKE '%{$term}%'");
  1397.  
  1398.         $found = array();
  1399.  
  1400.         if ($registers) {
  1401.             foreach ($registers as $register) {
  1402.                 $found[$register->ID] = rawurldecode($register->name);
  1403.             }
  1404.         }
  1405.  
  1406.         $found = apply_filters('wc_pos_json_search_registers', $found);
  1407.  
  1408.         wp_send_json($found);
  1409.     }
  1410.  
  1411.  
  1412.     /**
  1413.      * Search for outlet and echo json
  1414.      *
  1415.      */
  1416.     public function json_search_outlet()
  1417.     {
  1418.         ob_start();
  1419.  
  1420.         check_ajax_referer('search-products', 'security');
  1421.  
  1422.         $term = (string)wc_clean(stripslashes($_GET['term']));
  1423.  
  1424.         if (empty($term)) {
  1425.             die();
  1426.         }
  1427.  
  1428.         global $wpdb;
  1429.         $table_name = $wpdb->prefix . "wc_poin_of_sale_outlets";
  1430.         $registers = $wpdb->get_results("SELECT * FROM $table_name WHERE name LIKE '%{$term}%' ");
  1431.  
  1432.         $found = array();
  1433.  
  1434.         if ($registers) {
  1435.             foreach ($registers as $register) {
  1436.                 $found[$register->ID] = rawurldecode($register->name);
  1437.             }
  1438.         }
  1439.  
  1440.         $found = apply_filters('wc_pos_json_search_outlet', $found);
  1441.  
  1442.         wp_send_json($found);
  1443.     }
  1444.  
  1445.     /**
  1446.      * Search for outlet and echo json
  1447.      *
  1448.      */
  1449.     public function json_search_cashier()
  1450.     {
  1451.         //ob_start();
  1452.  
  1453.         check_ajax_referer('search-products', 'security');
  1454.  
  1455.         $term = (string)wc_clean(stripslashes($_GET['term']));
  1456.  
  1457.         if (empty($term)) {
  1458.             die();
  1459.         }
  1460.         $found = array();
  1461.  
  1462.         $user_query = WC_POS()->user()->get_data();
  1463.         if ($user_query) {
  1464.             foreach ($user_query as $user) {
  1465.                 $term = strtolower($term);
  1466.                 $name = strtolower($user['name']);
  1467.                 $username = strtolower($user['username']);
  1468.                 if (strpos($name, $term) !== false || strpos($username, $term) !== false)
  1469.                     $found[$user['ID']] = $user['name'] . ' (' . $user['username'] . ')';
  1470.             }
  1471.         }
  1472.  
  1473.         $found = apply_filters('wc_pos_json_search_cashier', $found);
  1474.  
  1475.         wp_send_json($found);
  1476.     }
  1477.  
  1478.     public function update_customer_shipping_address()
  1479.     {
  1480.         global $wpdb, $user;
  1481.         $userdata = array();
  1482.         parse_str($_REQUEST['form_data'], $userdata);
  1483.         $user_id = $userdata['customer_details_id'];
  1484.         $shipping_country = $userdata['custom_shipping_country'];
  1485.         $shipping_firstname = $userdata['custom_shipping_first_name'];
  1486.         $shipping_lastname = $userdata['custom_shipping_last_name'];
  1487.         $shipping_company = $userdata['custom_shipping_company'];
  1488.         $shipping_address = $userdata['custom_shipping_address_1'];
  1489.         $shipping_address1 = $userdata['custom_shipping_address_2'];
  1490.         $shipping_city = $userdata['custom_shipping_city'];
  1491.         $shipping_state = $userdata['custom_shipping_state'];
  1492.         $shipping_postcode = $userdata['custom_shipping_postcode'];
  1493.  
  1494.         if ($user_id) {
  1495.             update_user_meta($user_id, 'shipping_first_name', $shipping_firstname);
  1496.             update_user_meta($user_id, 'shipping_last_name', $shipping_lastname);
  1497.             update_user_meta($user_id, 'shipping_company', $shipping_company);
  1498.             update_user_meta($user_id, 'shipping_address_1', $shipping_address);
  1499.             update_user_meta($user_id, 'shipping_address_2', $shipping_address1);
  1500.             update_user_meta($user_id, 'shipping_city', $shipping_city);
  1501.             update_user_meta($user_id, 'shipping_postcode', $shipping_postcode);
  1502.             update_user_meta($user_id, 'shipping_state', $shipping_state);
  1503.             update_user_meta($user_id, 'shipping_country', $shipping_country);
  1504.  
  1505.             do_action('woocommerce_checkout_update_user_meta', $user_id, $_POST);
  1506.  
  1507.             $success = "success";
  1508.  
  1509.             $user_to_add = $user_id;
  1510.             $s_addr = array(
  1511.                 'first_name' => $shipping_lastname,
  1512.                 'last_name' => $shipping_firstname,
  1513.                 'company' => $shipping_company,
  1514.                 'address_1' => $shipping_address,
  1515.                 'address_2' => $shipping_address1,
  1516.                 'city' => $shipping_city,
  1517.                 'postcode' => $shipping_postcode,
  1518.                 'state' => $shipping_state,
  1519.                 'country' => $shipping_country
  1520.             );
  1521.             echo '<!--WC_POS_START-->' . json_encode(
  1522.                     array(
  1523.                         'success' => true,
  1524.                         'id' => $user_id,
  1525.                         's_addr' => $s_addr
  1526.                     )
  1527.                 ) . '<!--WC_POS_END-->';
  1528.  
  1529.         }
  1530.  
  1531.         die;
  1532.     }
  1533.  
  1534.     public function filter_product_barcode()
  1535.     {
  1536.         global $wpdb;
  1537.         $barcode = $_POST['barcode'];
  1538.         $product_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $barcode));
  1539.  
  1540.         $result = array();
  1541.         if ($product_id) {
  1542.  
  1543.             $result['status'] = 'success';
  1544.             $result['response'] = $this->get_sku_controller_product($product_id);
  1545.  
  1546.         } else {
  1547.             $result['response'] = '<h2>No product found</h2>';
  1548.             $result['status'] = '404';
  1549.         }
  1550.  
  1551.         $result = json_encode($result);
  1552.         echo $result;
  1553.  
  1554.         die();
  1555.     }
  1556.  
  1557.     public function change_stock()
  1558.     {
  1559.         global $wpdb;
  1560.  
  1561.         $product_id = $_POST['id'];
  1562.         $operation = $_POST['operation'];
  1563.         $value = $_POST['value'];
  1564.         $note = __('Product ', 'wc_point_of_sale');
  1565.  
  1566.         $result = array();
  1567.         if ($product_id) {
  1568.             $product = wc_get_product($product_id);
  1569.             $product->manage_stock = 'yes';
  1570.             $stock = $product->get_stock_quantity();
  1571.  
  1572.             if ($operation == 'increase') {
  1573.                 $stock += $value;
  1574.                 $note .=  '<strong>' . $product->get_name() . '</strong>' . __(' stock increased by ', 'wc_point_of_sale') . $value;
  1575.             } else {
  1576.                 $stock -= $value;
  1577.                 if ($stock < 0) {
  1578.                     $stock = 0;
  1579.                 }
  1580.                 $note .=  $product->get_name() . __(' stock reduced by ', 'wc_point_of_sale') . $value;
  1581.             }
  1582.  
  1583.  
  1584.             wc_update_product_stock($product, $stock);
  1585.  
  1586.             $post_modified = current_time('mysql');
  1587.             $post_modified_gmt = current_time('mysql', 1);
  1588.  
  1589.             wp_update_post(array(
  1590.                 'ID' => $product_id,
  1591.                 'post_modified' => $post_modified,
  1592.                 'post_modified_gmt' => $post_modified_gmt
  1593.             ));
  1594.  
  1595.             if ($product->get_type() == 'variation' && $product->get_parent_id() && $product->get_parent_id() > 0) {
  1596.                 wp_update_post(array(
  1597.                     'ID' => $product->parent->id,
  1598.                     'post_modified' => $post_modified,
  1599.                     'post_modified_gmt' => $post_modified_gmt
  1600.                 ));
  1601.             }
  1602.  
  1603.             $order = wc_get_order($_POST['order_id']);
  1604.             if($order){
  1605.                 $order->add_order_note($note);
  1606.             }
  1607.  
  1608.             $result['status'] = 'success';
  1609.             $result['response'] = $this->get_sku_controller_product($product_id);
  1610.  
  1611.         } else {
  1612.             $result['status'] = '404';
  1613.         }
  1614.  
  1615.         $result = json_encode($result);
  1616.         echo $result;
  1617.  
  1618.         die();
  1619.     }
  1620.  
  1621.     public function get_sku_controller_product($product_id = 0)
  1622.     {
  1623.         $product_data = array();
  1624.         if ($product_id) {
  1625.             $post = get_post($product_id);
  1626.             if ($post->post_type == 'product') {
  1627.                 $product = new WC_Product($product_id);
  1628.                 $product_data['id'] = $product_id;
  1629.                 $product_data['name'] = $product->get_title();
  1630.                 $product_data['sku'] = $product->get_sku();
  1631.                 $product_data['image'] = $product->get_image(array(85, 85));
  1632.                 $product_data['price'] = $product->get_price_html();
  1633.                 $product_data['stock'] = wc_stock_amount($product->get_stock_quantity());
  1634.                 $product_data['stock_status'] = '';
  1635.                 if ($product->is_in_stock()) {
  1636.                     $product_data['stock_status'] = '<mark class="instock">' . __('In stock', 'woocommerce') . '</mark>';
  1637.                 } else {
  1638.                     $product_data['stock_status'] = '<mark class="outofstock">' . __('Out of stock', 'woocommerce') . '</mark>';
  1639.                 }
  1640.                 $product_data['stock_status'] .= ' &times; ' . wc_stock_amount($product->get_stock_quantity());
  1641.             } elseif ($post->post_type = 'product_variation') {
  1642.                 $product = new WC_Product_Variation($product_id);
  1643.                 $product_data['id'] = $product_id;
  1644.                 $product_data['name'] = $post->post_title;
  1645.                 $product_data['sku'] = $product->get_name();
  1646.                 $product_data['image'] = $product->get_image(array(85, 85));
  1647.                 $product_data['price'] = $product->get_price();
  1648.                 $product_data['stock'] = $product->get_stock_quantity();
  1649.                 $product_data['stock_status'] = '';
  1650.                 if ($product_data['stock']) {
  1651.                     $product_data['stock_status'] = '<mark class="instock">' . __('In stock', 'woocommerce') . '</mark>';
  1652.                 } else {
  1653.                     $product_data['stock_status'] = '<mark class="outofstock">' . __('Out of stock', 'woocommerce') . '</mark>';
  1654.                 }
  1655.                 $product_data['stock_status'] .= ' &times; ' . number_format($product_data['stock'], 2);
  1656.             }
  1657.         }
  1658.         return $product_data;
  1659.     }
  1660.  
  1661.     public function get_grid_options()
  1662.     {
  1663.         $this->json_headers();
  1664.         $pos = new WC_Pos_Sell(true);
  1665.         $pos->getRegisted(intval($_POST['reg']));
  1666.         echo $pos->getGrid();
  1667.         die();
  1668.     }
  1669.  
  1670.     public function add_product_for_barcode()
  1671.     {
  1672.         check_ajax_referer('product_for_barcode', 'security');
  1673.  
  1674.         if (!current_user_can('manage_wc_point_of_sale')) {
  1675.             die(-1);
  1676.         }
  1677.  
  1678.         $item_to_add = sanitize_text_field($_POST['item_to_add']);
  1679.  
  1680.         // Find the item
  1681.         if (!is_numeric($item_to_add)) {
  1682.             die();
  1683.         }
  1684.  
  1685.         $post = get_post($item_to_add);
  1686.  
  1687.         if (!$post || ('product' !== $post->post_type && 'product_variation' !== $post->post_type)) {
  1688.             die();
  1689.         }
  1690.  
  1691.         $_product = wc_get_product($post->ID);
  1692.         $class = 'new_row ' . $_product->product_type;
  1693.  
  1694.         include 'views/html-admin-barcode-item.php';
  1695.         // Quit out
  1696.         die();
  1697.     }
  1698.  
  1699.     public function get_product_variations_for_barcode()
  1700.     {
  1701.         check_ajax_referer('product_for_barcode', 'security');
  1702.  
  1703.         if (!current_user_can('manage_wc_point_of_sale')) {
  1704.             die(-1);
  1705.         }
  1706.  
  1707.         $prid = $_POST['prid'];
  1708.  
  1709.         // Find the item
  1710.         if (!is_array($prid)) {
  1711.             die();
  1712.         }
  1713.         $variations = array();
  1714.         foreach ($prid as $id) {
  1715.             $args = array(
  1716.                 'post_parent' => $id,
  1717.                 'post_type' => 'product_variation',
  1718.                 'numberposts' => -1,
  1719.                 'fields' => 'ids',
  1720.             );
  1721.             $children_array = get_children($args, ARRAY_A);
  1722.             if ($children_array) {
  1723.  
  1724.                 $variations = array_merge($variations, $children_array);
  1725.             }
  1726.         }
  1727.         wp_send_json($variations);
  1728.         // Quit out
  1729.         die();
  1730.     }
  1731.  
  1732.     public function can_user_open_register()
  1733.     {
  1734.         $response = array('result' => 'denied');
  1735.  
  1736.         if (isset($_POST['register_id']) && pos_check_user_can_open_register($_POST['register_id'])) {
  1737.  
  1738.             if ($user_id = pos_check_register_lock($_POST['register_id'])) {
  1739.                 $user = get_userdata($user_id);
  1740.                 $response = array(
  1741.                     'result' => 'locked',
  1742.                     'user_id' => $user_id,
  1743.                     'message' => sprintf(__('This register is currently opened by %s.', 'wc_point_of_sale'), $user->display_name),
  1744.                     'avatar' => get_avatar_url($user_id, array('size' => 64))
  1745.                 );
  1746.             } else {
  1747.                 $current_user = wp_get_current_user();
  1748.                 $response = array(
  1749.                     'result' => 'success',
  1750.                     'user_id' => $current_user->ID,
  1751.                     'name' => $current_user->display_name,
  1752.                     'avatar' => get_avatar_url($current_user->ID, array('size' => 64))
  1753.                 );
  1754.             }
  1755.         }
  1756.  
  1757.         wp_send_json($response);
  1758.         die();
  1759.     }
  1760.  
  1761.     public function json_search_categories()
  1762.     {
  1763.         global $wpdb;
  1764.  
  1765.         ob_start();
  1766.  
  1767.         check_ajax_referer('search-products', 'security');
  1768.  
  1769.         $term = wc_clean(stripslashes($_GET['term']));
  1770.  
  1771.         if (empty($term)) {
  1772.             die();
  1773.         }
  1774.         $like_term = '%' . $wpdb->esc_like($term) . '%';
  1775.  
  1776.         $query = $wpdb->prepare("
  1777.                SELECT terms.term_id FROM {$wpdb->terms} terms LEFT JOIN {$wpdb->term_taxonomy} taxonomy ON terms.term_id = taxonomy.term_id
  1778.                WHERE taxonomy.taxonomy = 'product_cat'
  1779.                AND terms.name LIKE %s
  1780.            ", $like_term);
  1781.  
  1782.         $categories = array_unique($wpdb->get_col($query));
  1783.         $found_categories = array();
  1784.  
  1785.         if (!empty($categories)) {
  1786.             foreach ($categories as $term_id) {
  1787.                 $category = get_term($term_id);
  1788.  
  1789.                 if (is_wp_error($category) || !$category) {
  1790.                     continue;
  1791.                 }
  1792.  
  1793.                 $found_categories[$term_id] = rawurldecode($category->name);
  1794.             }
  1795.         }
  1796.  
  1797.         $found_categories = apply_filters('wc_pos_json_search_categories', $found_categories);
  1798.  
  1799.         wp_send_json($found_categories);
  1800.     }
  1801.  
  1802.     public function get_products_by_categories()
  1803.     {
  1804.         check_ajax_referer('product_for_barcode', 'security');
  1805.  
  1806.  
  1807.         if (!current_user_can('manage_wc_point_of_sale') || !isset($_POST['categories'])) {
  1808.             die(-1);
  1809.         }
  1810.  
  1811.         $categories = $_POST['categories'];
  1812.  
  1813.         // Find the item
  1814.         if (!is_array($categories)) {
  1815.             die();
  1816.         }
  1817.  
  1818.         $args = array(
  1819.             'post_type' => 'product',
  1820.             'numberposts' => -1,
  1821.             'fields' => 'ids',
  1822.             'tax_query' => array(
  1823.                 array(
  1824.                     'terms' => $categories,
  1825.                     'taxonomy' => 'product_cat'
  1826.                 )
  1827.             )
  1828.         );
  1829.         $products = array();
  1830.         $posts = get_posts($args, ARRAY_A);
  1831.  
  1832.         if ($posts) {
  1833.             $products = $posts;
  1834.         }
  1835.  
  1836.         wp_send_json($products);
  1837.     }
  1838.  
  1839.     public function set_register_opening_cash()
  1840.     {
  1841.         global $wpdb;
  1842.  
  1843.         $table_name = $wpdb->prefix . "wc_poin_of_sale_registers";
  1844.  
  1845.         $db_data = $wpdb->get_results("SELECT * FROM $table_name WHERE ID = {$_POST['register_id']}");
  1846.         $detail = json_decode($db_data[0]->detail, true);
  1847.         $detail['opening_cash_amount'] = array('status' => true, 'amount' => $_POST['amount'], 'note' => $_POST['note'], 'user' => get_current_user_id(), 'time' => current_time('mysql'));
  1848.         $data['detail'] = json_encode($detail);
  1849.         $wpdb->update($table_name, $data, array('ID' => $_POST['register_id']));
  1850.     }
  1851.  
  1852.     public function add_cash_management_action()
  1853.     {
  1854.         global $wpdb;
  1855.         $table_name = $wpdb->prefix . "wc_poin_of_sale_registers";
  1856.         $data['detail'] = $_POST['register']['detail'];
  1857.         $row = array(
  1858.             'amount' => $_POST['amount'],
  1859.             'note' => $_POST['note'],
  1860.             'time' => current_time('mysql'),
  1861.             'type' => $_POST['action_type'],
  1862.             'user' => get_current_user_id()
  1863.         );
  1864.         switch ($_POST['action_type']) {
  1865.             case 'add-cash':
  1866.                 $row['title'] = __('Cash in', 'wc_point_of_sale');
  1867.                 break;
  1868.             case 'remove-cash':
  1869.                 $row['title'] = __('Cash out', 'wc_point_of_sale');
  1870.                 break;
  1871.         }
  1872.         $data['detail']['cash_management_actions'][] = $row;
  1873.         $data['detail'] = json_encode($data['detail']);
  1874.         if ($wpdb->update($table_name, $data, array('ID' => $_POST['register']['ID']))) {
  1875.             $author = get_user_by('id', $row['user']);
  1876.             wp_die(include('views/html-float-cash-management-table-row.php'));
  1877.         }
  1878.     }
  1879.  
  1880.     public function get_user_avatars()
  1881.     {
  1882.         if (is_plugin_active('wp-user-avatar/wp-user-avatar.php') && has_wp_user_avatar($_POST['userdata']['id'])) {
  1883.             $_POST['userdata']['avatar_url'] = get_wp_user_avatar_src($_POST['userdata']['id'], 'thumbnail');
  1884.         }
  1885.         wp_die(json_encode($_POST['userdata']));
  1886.     }
  1887.  
  1888.     public function set_register_actual_cash()
  1889.     {
  1890.         WC_Pos_Float_Cash::set_actual_cash($_POST['register_id'], $_POST['sum']);
  1891.     }
  1892.  
  1893.     public function refresh_bill_screen()
  1894.     {
  1895.         if ($_POST['register_status'] == 'open') {
  1896.             $register_cart = $_POST['register_cart'];
  1897.             include('views/html-bill-screen-content.php');
  1898.         } elseif ($_POST['register_status'] == 'close') {
  1899.             echo "<span class='receipt_closed'>";
  1900.             _e('This Register Is Closed', 'wc_point_of_sale');
  1901.             echo "</span>";
  1902.         }
  1903.         die();
  1904.     }
  1905.  
  1906.     public function get_default_variations()
  1907.     {
  1908.         wp_die(json_encode(get_post_meta($_GET['product_id'], '_default_attributes', true)));
  1909.     }
  1910.  
  1911.     public function save_tab()
  1912.     {
  1913.         global $wpdb;
  1914.         $table = $wpdb->prefix . 'wc_poin_of_sale_tabs';
  1915.         $data = array(
  1916.             'title' => $_POST['title'],
  1917.             'spend_limit' => $_POST['limit'],
  1918.             'register_id' => $_POST['reg_id'],
  1919.             'order_id' => $_POST['order_id'],
  1920.             'tab_number' => $_POST['tab_number']
  1921.         );
  1922.         if (!intval($_POST['tab_id'])) {
  1923.             $data['opened'] = date('Y-m-d H:i:s', time() - $_POST['seconds']);
  1924.             $result = $wpdb->insert($table, $data);
  1925.             wp_send_json($data);
  1926.         } else {
  1927.             $result = $wpdb->update($table, $data, array('id' => $_POST['tab_id']));
  1928.         }
  1929.         wp_send_json($result);
  1930.     }
  1931.  
  1932.     public function delete_tab()
  1933.     {
  1934.         global $wpdb;
  1935.         $table = $wpdb->prefix . 'wc_poin_of_sale_tabs';
  1936.         $where = array('order_id' => $_POST['order_id']);
  1937.         $wpdb->delete($table, $where);
  1938.         wp_send_json($where);
  1939.     }
  1940.  
  1941.     public function get_customer_html()
  1942.     {
  1943.         if ($_POST['customer_id']) {
  1944.             $customer = new WP_User($_POST['customer_id']);
  1945.             if ($customer) {
  1946.                 $html = '<option value="' . $customer->ID . '">' . $customer->display_name . ' (' . $customer->user_email . '/  ' . $customer->first_name . ')</option>';
  1947.                 wp_die($html);
  1948.             }
  1949.         }
  1950.     }
  1951.  
  1952.     public function check_user_card_uniqueness()
  1953.     {
  1954.  
  1955.         $code = esc_attr($_POST['code']);
  1956.  
  1957.         $users = get_users(
  1958.             array(
  1959.                 'meta_key' => 'user_card_number',
  1960.                 'meta_value' => $code
  1961.             )
  1962.         );
  1963.  
  1964.         if (count($users) == 0) {
  1965.             wp_send_json_success(__('You can use this code', 'wc_point_of_sale'));
  1966.         } else {
  1967.             wp_send_json_error(__('Sorry, this code is already present', 'wc_point_of_sale'));
  1968.         }
  1969.     }
  1970.  
  1971.     public function get_user_by_card_number()
  1972.     {
  1973.  
  1974.         $code = esc_attr($_POST['code']);
  1975.  
  1976.         $users = get_users(
  1977.             array(
  1978.                 'meta_key' => 'user_card_number',
  1979.                 'meta_value' => $code
  1980.             )
  1981.         );
  1982.  
  1983.         if (count($users) == 0) {
  1984.             wp_send_json_error(_('User not found', 'wc_point_of_sale'));
  1985.         } else {
  1986.  
  1987.             $customer = new WC_Customer($users[0]->ID);
  1988.  
  1989.             wp_send_json_success($customer->get_data());
  1990.         }
  1991.     }
  1992.  
  1993.     public function rename_payment_gateway() {
  1994.  
  1995.         if (update_option(esc_attr($_POST['name']).'_name', esc_attr($_POST['value']))) {
  1996.             wp_send_json_success();
  1997.         }
  1998.         wp_send_json_error();
  1999.  
  2000.     }
  2001.  
  2002.     public function logout()
  2003.     {
  2004.         $register_id = (isset($_POST['register_id'])) ? $_POST['register_id'] : 0 ;
  2005.         if ($register_id) {
  2006.             pos_close_register($register_id);
  2007.             wp_logout();
  2008.         }
  2009.     }
  2010.  
  2011.     public function pos_print_receipt()
  2012.     {
  2013.         $order_id = isset($_POST['pos_order_id']) && intval($_POST['pos_order_id']) ? intval($_POST['pos_order_id']) : 0;
  2014.         $order = wc_get_order($order_id);
  2015.  
  2016.         if(!$order) wp_send_json_error("invalid order");
  2017.  
  2018.         do_action('woocommerce_thankyou', $order->get_id());
  2019.  
  2020.         wp_send_json_success("print success");
  2021.     }
  2022.  
  2023.     public function use_store_address()
  2024.     {
  2025.         global $woocommerce;
  2026.         $wc_country = $woocommerce->countries;
  2027.         $address = array(
  2028.             'country' => $wc_country->get_base_country(),
  2029.             'address_1' => $wc_country->get_base_address(),
  2030.             'address_2' => $wc_country->get_base_address_2(),
  2031.             'city' => $wc_country->get_base_city(),
  2032.             'state' => $wc_country->get_base_state(),
  2033.             'postcode' => $wc_country->get_base_postcode(),
  2034.             'name' => get_bloginfo('name'),
  2035.             'website' => get_bloginfo('url'),
  2036.             'email' => get_bloginfo('admin_email')
  2037.         );
  2038.  
  2039.         wp_send_json_success($address);
  2040.     }
  2041.  
  2042.     public function filter_order_barcode()
  2043.     {
  2044.         if(!isset($_POST['barcode']) || empty($_POST['barcode'])){
  2045.             wp_send_json_error(__('invalid order id', 'wc_point_of_sale'), 400);
  2046.         }
  2047.  
  2048.         $order = wc_get_order($_POST['barcode']);
  2049.         if(!$order){
  2050.             wp_send_json_error(__('order not found', 'wc_point_of_sale'), 404);
  2051.         }
  2052.  
  2053.         $products = array();
  2054.         foreach ($order->get_items() as $id => $item){
  2055.             $product = new WC_Order_Item_Product($item);
  2056.             $product_data = $product->get_product()->get_data();
  2057.             $product_data["image"] = wp_get_attachment_image($product_data['image_id']);
  2058.             array_push($products, $product_data);
  2059.         }
  2060.         wp_send_json_success($products);
  2061.     }
  2062. }
  2063.  
  2064. new WC_POS_AJAX();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement