/** * 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(); }
/** * 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; }