/**
  * Process the checkout after the confirm order button is pressed.
  */
 public function process_checkout()
 {
     try {
         if (empty($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'woocommerce-process_checkout')) {
             WC()->session->set('refresh_totals', true);
             throw new Exception(__('We were unable to process your order, please try again.', 'woocommerce'));
         }
         wc_maybe_define_constant('WOOCOMMERCE_CHECKOUT', true);
         wc_set_time_limit(0);
         do_action('woocommerce_before_checkout_process');
         if (WC()->cart->is_empty()) {
             throw new Exception(sprintf(__('Sorry, your session has expired. <a href="%s" class="wc-backward">Return to shop</a>', 'woocommerce'), esc_url(wc_get_page_permalink('shop'))));
         }
         do_action('woocommerce_checkout_process');
         $errors = new WP_Error();
         $posted_data = $this->get_posted_data();
         $this->validate_posted_data($posted_data, $errors);
         $this->validate_checkout($posted_data, $errors);
         $this->update_session($posted_data);
         $this->check_cart_items();
         do_action('woocommerce_after_checkout_validation', $posted_data, $errors);
         foreach ($errors->get_error_messages() as $message) {
             wc_add_notice($message, 'error');
         }
         if (empty($posted_data['woocommerce_checkout_update_totals']) && 0 === wc_notice_count('error')) {
             $this->process_customer($posted_data);
             $order = $this->create_order($posted_data);
             if (is_wp_error($order)) {
                 throw new Exception($order->get_error_message());
             }
             do_action('woocommerce_checkout_order_processed', $order, $posted_data);
             if (WC()->cart->needs_payment()) {
                 $this->process_order_payment($order, $posted_data['payment_method']);
             } else {
                 $this->process_order_without_payment($order);
             }
         }
     } catch (Exception $e) {
         wc_add_notice($e->getMessage(), 'error');
     }
     $this->send_ajax_failure_response();
 }
 /**
  * Import is starting.
  */
 private function import_start()
 {
     if (function_exists('gc_enable')) {
         gc_enable();
     }
     wc_set_time_limit(0);
     @ob_flush();
     @flush();
     @ini_set('auto_detect_line_endings', '1');
 }
示例#3
0
 /**
  * Link all variations via ajax function.
  */
 public static function link_all_variations()
 {
     if (!defined('WC_MAX_LINKED_VARIATIONS')) {
         define('WC_MAX_LINKED_VARIATIONS', 49);
     }
     check_ajax_referer('link-variations', 'security');
     if (!current_user_can('edit_products')) {
         die(-1);
     }
     wc_set_time_limit(0);
     $post_id = intval($_POST['post_id']);
     if (!$post_id) {
         die;
     }
     $variations = array();
     $_product = wc_get_product($post_id, array('product_type' => 'variable'));
     // Put variation attributes into an array
     foreach ($_product->get_attributes() as $attribute) {
         if (!$attribute['is_variation']) {
             continue;
         }
         $attribute_field_name = 'attribute_' . sanitize_title($attribute['name']);
         if ($attribute['is_taxonomy']) {
             $options = wc_get_product_terms($post_id, $attribute['name'], array('fields' => 'slugs'));
         } else {
             $options = explode(WC_DELIMITER, $attribute['value']);
         }
         $options = array_map('trim', $options);
         $variations[$attribute_field_name] = $options;
     }
     // Quit out if none were found
     if (sizeof($variations) == 0) {
         die;
     }
     // Get existing variations so we don't create duplicates
     $available_variations = array();
     foreach ($_product->get_children() as $child_id) {
         $child = $_product->get_child($child_id);
         if (!empty($child->variation_id)) {
             $available_variations[] = $child->get_variation_attributes();
         }
     }
     // Created posts will all have the following data
     $variation_post_data = array('post_title' => 'Product #' . $post_id . ' Variation', 'post_content' => '', 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'post_parent' => $post_id, 'post_type' => 'product_variation');
     $variation_ids = array();
     $added = 0;
     $possible_variations = wc_array_cartesian($variations);
     foreach ($possible_variations as $variation) {
         // Check if variation already exists
         if (in_array($variation, $available_variations)) {
             continue;
         }
         $variation_id = wp_insert_post($variation_post_data);
         $variation_ids[] = $variation_id;
         foreach ($variation as $key => $value) {
             update_post_meta($variation_id, $key, $value);
         }
         // Save stock status
         update_post_meta($variation_id, '_stock_status', 'instock');
         $added++;
         do_action('product_variation_linked', $variation_id);
         if ($added > WC_MAX_LINKED_VARIATIONS) {
             break;
         }
     }
     delete_transient('wc_product_children_' . $post_id);
     echo $added;
     die;
 }
 /**
  * Check and set certain server config variables to ensure downloads work as intended.
  */
 private static function check_server_config()
 {
     wc_set_time_limit(0);
     if (function_exists('get_magic_quotes_runtime') && get_magic_quotes_runtime() && version_compare(phpversion(), '5.4', '<')) {
         set_magic_quotes_runtime(0);
     }
     if (function_exists('apache_setenv')) {
         @apache_setenv('no-gzip', 1);
     }
     @ini_set('zlib.output_compression', 'Off');
     @session_write_close();
 }
示例#5
0
 /**
  * Link all variations via ajax function.
  */
 public static function link_all_variations()
 {
     check_ajax_referer('link-variations', 'security');
     if (!current_user_can('edit_products')) {
         die(-1);
     }
     wc_maybe_define_constant('WC_MAX_LINKED_VARIATIONS', 49);
     wc_set_time_limit(0);
     $post_id = intval($_POST['post_id']);
     if (!$post_id) {
         die;
     }
     $variations = array();
     $product = wc_get_product($post_id);
     if ($product->is_type('variable')) {
         $attributes = wc_list_pluck(array_filter($product->get_attributes(), 'wc_attributes_array_filter_variation'), 'get_slugs');
         if (!empty($attributes)) {
             // Get existing variations so we don't create duplicates.
             $existing_variations = array_map('wc_get_product', $product->get_children());
             $existing_attributes = array();
             foreach ($existing_variations as $existing_variation) {
                 $existing_attributes[] = $existing_variation->get_attributes();
             }
             $added = 0;
             $possible_attributes = wc_array_cartesian($attributes);
             foreach ($possible_attributes as $possible_attribute) {
                 if (in_array($possible_attribute, $existing_attributes)) {
                     continue;
                 }
                 $variation = new WC_Product_Variation();
                 $variation->set_parent_id($post_id);
                 $variation->set_attributes($possible_attribute);
                 do_action('product_variation_linked', $variation->save());
                 if ($added++ > WC_MAX_LINKED_VARIATIONS) {
                     break;
                 }
             }
             echo $added;
         }
     }
     die;
 }
 /**
  * Sends a request to the Connect for WooCommerce Server
  *
  * @param $method
  * @param $path
  * @param $body
  * @return mixed|WP_Error
  */
 protected function request($method, $path, $body = array())
 {
     // TODO - incorporate caching for repeated identical requests
     if (!class_exists('Jetpack_Data')) {
         return new WP_Error('jetpack_data_class_not_found', 'Unable to send request to Connect for WooCommerce server. Jetpack_Data was not found.');
     }
     if (!method_exists('Jetpack_Data', 'get_access_token')) {
         return new WP_Error('jetpack_data_get_access_token_not_found', 'Unable to send request to Connect for WooCommerce server. Jetpack_Data does not implement get_access_token.');
     }
     if (!is_array($body)) {
         return new WP_Error('request_body_should_be_array', 'Unable to send request to Connect for WooCommerce server. Body must be an array.');
     }
     $url = trailingslashit(WOOCOMMERCE_CONNECT_SERVER_URL);
     $url = apply_filters('wc_connect_server_url', $url);
     $url = trailingslashit($url) . ltrim($path, '/');
     // Add useful system information to requests that contain bodies
     if (in_array($method, array('POST', 'PUT'))) {
         $body = $this->request_body($body);
         $body = wp_json_encode(apply_filters('wc_connect_api_client_body', $body));
         if (!$body) {
             return new WP_Error('unable_to_json_encode_body', 'Unable to encode body for request to Connect for WooCommerce server.');
         }
     }
     $headers = $this->request_headers();
     if (is_wp_error($headers)) {
         return $headers;
     }
     $http_timeout = 60;
     // 1 minute
     if (function_exists('wc_set_time_limit')) {
         wc_set_time_limit($http_timeout + 10);
     }
     $args = array('headers' => $headers, 'method' => $method, 'body' => $body, 'redirection' => 0, 'compress' => true, 'timeout' => $http_timeout);
     $args = apply_filters('wc_connect_request_args', $args);
     $response = wp_remote_request($url, $args);
     $response_code = wp_remote_retrieve_response_code($response);
     // If the received response is not JSON, return the raw response
     $content_type = wp_remote_retrieve_header($response, 'content-type');
     if (false === strpos($content_type, 'application/json')) {
         if (200 != $response_code) {
             return new WP_Error('wcc_server_error', sprintf('Error: The Connect for WooCommerce server returned HTTP code: %d', $response_code));
         }
         return $response;
     }
     $response_body = wp_remote_retrieve_body($response);
     if (!empty($response_body)) {
         $response_body = json_decode($response_body);
     }
     if (200 != $response_code) {
         if (empty($response_body)) {
             return new WP_Error('wcc_server_empty_response', sprintf('Error: The Connect for WooCommerce server returned ( %d ) and an empty response body.', $response_code));
         }
         $error = property_exists($response_body, 'error') ? $response_body->error : '';
         $message = property_exists($response_body, 'message') ? $response_body->message : '';
         $data = property_exists($response_body, 'data') ? $response_body->data : '';
         return new WP_Error('wcc_server_error_response', sprintf('Error: The Connect for WooCommerce server returned: %s %s ( %d )', $error, $message, $response_code), $data);
     }
     return $response_body;
 }