?> </tfoot> <tbody> <?php if (sizeof($order->get_items()) > 0) { foreach ($order->get_items() as $item) { if (isset($item['variation_id']) && $item['variation_id'] > 0) { $_product = new WC_Product_Variation($item['variation_id']); } else { $_product = new WC_Product($item['id']); } echo ' <tr> <td class="product-name">'; echo '<a href="' . get_permalink($item['id']) . '">' . $item['name'] . '</a>'; $item_meta = new order_item_meta($item['item_meta']); $item_meta->display(); if ($_product->exists && $_product->is_downloadable() && ($order->status == 'completed' || get_option('woocommerce_downloads_grant_access_after_payment') == 'yes' && $order->status == 'processing')) { echo '<br/><small><a href="' . $order->get_downloadable_file_url($item['id'], $item['variation_id']) . '">' . __('Download file →', 'woocommerce') . '</a></small>'; } echo '</td><td class="product-quantity">' . $item['qty'] . '</td><td class="product-total">' . $order->get_formatted_line_subtotal($item) . '</td></tr>'; // Show any purchase notes if ($order->status == 'completed' || $order->status == 'processing') { if ($purchase_note = get_post_meta($_product->id, '_purchase_note', true)) { echo '<tr class="product-purchase-note"><td colspan="3">' . apply_filters('the_content', $purchase_note) . '</td></tr>'; } } } } do_action('woocommerce_order_items_table', $order); ?>
/** * Process the checkout after the confirm order button is pressed */ function process_checkout() { global $wpdb, $woocommerce; if (!defined('WOOCOMMERCE_CHECKOUT')) { define('WOOCOMMERCE_CHECKOUT', true); } $woocommerce->verify_nonce('process_checkout'); do_action('woocommerce_before_checkout_process'); if (sizeof($woocommerce->cart->get_cart()) == 0) { $woocommerce->add_error(sprintf(__('Sorry, your session has expired. <a href="%s">Return to homepage →</a>', 'woocommerce'), home_url())); } do_action('woocommerce_checkout_process'); // Checkout fields (not defined in checkout_fields) $this->posted['shiptobilling'] = isset($_POST['shiptobilling']) ? 1 : 0; $this->posted['terms'] = isset($_POST['terms']) ? 1 : 0; $this->posted['createaccount'] = isset($_POST['createaccount']) ? 1 : 0; $this->posted['payment_method'] = isset($_POST['payment_method']) ? woocommerce_clean($_POST['payment_method']) : ''; $this->posted['shipping_method'] = isset($_POST['shipping_method']) ? woocommerce_clean($_POST['shipping_method']) : ''; // Ship to billing only option if ($woocommerce->cart->ship_to_billing_address_only()) { $this->posted['shiptobilling'] = 1; } // Update customer shipping and payment method to posted method $_SESSION['_chosen_shipping_method'] = $this->posted['shipping_method']; $_SESSION['_chosen_payment_method'] = $this->posted['payment_method']; // Update cart totals $woocommerce->cart->calculate_totals(); // Note if we skip shipping $skipped_shipping = false; // Get validation class $validation = $woocommerce->validation(); // Get posted checkout_fields and do validation foreach ($this->checkout_fields as $fieldset_key => $fieldset) { // Skip shipping if its not needed if ($fieldset_key == 'shipping' && (!$woocommerce->cart->needs_shipping() || $woocommerce->cart->ship_to_billing_address_only() || $this->posted['shiptobilling'])) { $skipped_shipping = true; continue; } foreach ($fieldset as $key => $field) { if (!isset($field['type'])) { $field['type'] = 'text'; } // Get Value switch ($field['type']) { case "checkbox": $this->posted[$key] = isset($_POST[$key]) ? 1 : 0; break; default: $this->posted[$key] = isset($_POST[$key]) ? woocommerce_clean($_POST[$key]) : ''; break; } // Hook to allow modification of value $this->posted[$key] = apply_filters('woocommerce_process_checkout_field_' . $key, $this->posted[$key]); // Validation: Required fields if (isset($field['required']) && $field['required'] && empty($this->posted[$key])) { $woocommerce->add_error('<strong>' . $field['label'] . '</strong> ' . __('is a required field.', 'woocommerce')); } if (!empty($this->posted[$key])) { // Special handling for validation and formatting switch ($key) { case "billing_postcode": case "shipping_postcode": $this->posted[$key] = strtoupper(str_replace(' ', '', $this->posted[$key])); if (!$validation->is_postcode($this->posted[$key], $_POST['billing_country'])) { $woocommerce->add_error('<strong>' . $field['label'] . '</strong> ' . __('(billing) is not a valid postcode/ZIP.', 'woocommerce')); } else { $this->posted[$key] = $validation->format_postcode($this->posted[$key], $_POST['billing_country']); } break; case "billing_phone": if (!$validation->is_phone($this->posted[$key])) { $woocommerce->add_error('<strong>' . $field['label'] . '</strong> ' . __('is not a valid number.', 'woocommerce')); } break; case "billing_email": if (!$validation->is_email($this->posted[$key])) { $woocommerce->add_error('<strong>' . $field['label'] . '</strong> ' . __('is not a valid email address.', 'woocommerce')); } break; } } } } // Update customer location to posted location so we can correctly check available shipping methods $woocommerce->customer->set_country($this->posted['billing_country']); $woocommerce->customer->set_state($this->posted['billing_state']); $woocommerce->customer->set_postcode($this->posted['billing_postcode']); // Shipping Information if (!$skipped_shipping) { // Update customer location to posted location so we can correctly check available shipping methods $woocommerce->customer->set_shipping_country($this->posted['shipping_country']); $woocommerce->customer->set_shipping_state($this->posted['shipping_state']); $woocommerce->customer->set_shipping_postcode($this->posted['shipping_postcode']); } else { // Update customer location to posted location so we can correctly check available shipping methods $woocommerce->customer->set_shipping_country($this->posted['billing_country']); $woocommerce->customer->set_shipping_state($this->posted['billing_state']); $woocommerce->customer->set_shipping_postcode($this->posted['billing_postcode']); } if (is_user_logged_in()) { $this->creating_account = false; } elseif (isset($this->posted['createaccount']) && $this->posted['createaccount']) { $this->creating_account = true; } elseif ($this->must_create_account) { $this->creating_account = true; } else { $this->creating_account = false; } if ($this->creating_account) { if (empty($this->posted['account_username'])) { $woocommerce->add_error(__('Please enter an account username.', 'woocommerce')); } if (empty($this->posted['account_password'])) { $woocommerce->add_error(__('Please enter an account password.', 'woocommerce')); } if ($this->posted['account_password-2'] !== $this->posted['account_password']) { $woocommerce->add_error(__('Passwords do not match.', 'woocommerce')); } // Check the username if (!validate_username($this->posted['account_username'])) { $woocommerce->add_error(__('Invalid email/username.', 'woocommerce')); } elseif (username_exists($this->posted['account_username'])) { $woocommerce->add_error(__('An account is already registered with that username. Please choose another.', 'woocommerce')); } // Check the e-mail address if (email_exists($this->posted['billing_email'])) { $woocommerce->add_error(__('An account is already registered with your email address. Please login.', 'woocommerce')); } } // Terms if (!isset($_POST['woocommerce_checkout_update_totals']) && empty($this->posted['terms']) && woocommerce_get_page_id('terms') > 0) { $woocommerce->add_error(__('You must accept our Terms & Conditions.', 'woocommerce')); } if ($woocommerce->cart->needs_shipping()) { // Shipping Method $available_methods = $woocommerce->shipping->get_available_shipping_methods(); if (!isset($available_methods[$this->posted['shipping_method']])) { $woocommerce->add_error(__('Invalid shipping method.', 'woocommerce')); } } if ($woocommerce->cart->needs_payment()) { // Payment Method $available_gateways = $woocommerce->payment_gateways->get_available_payment_gateways(); if (!isset($available_gateways[$this->posted['payment_method']])) { $woocommerce->add_error(__('Invalid payment method.', 'woocommerce')); } else { // Payment Method Field Validation $available_gateways[$this->posted['payment_method']]->validate_fields(); } } do_action('woocommerce_after_checkout_validation', $this->posted); if (!isset($_POST['woocommerce_checkout_update_totals']) && $woocommerce->error_count() == 0) { $user_id = get_current_user_id(); while (1) { // Create customer account and log them in if ($this->creating_account && !$user_id) { $reg_errors = new WP_Error(); do_action('woocommerce_register_post', $this->posted['account_username'], $this->posted['billing_email'], $reg_errors); $errors = apply_filters('woocommerce_registration_errors', $reg_errors, $this->posted['account_username'], $this->posted['billing_email']); // if there are no errors, let's create the user account if (!$reg_errors->get_error_code()) { $user_pass = esc_attr($this->posted['account_password']); $user_id = wp_create_user($this->posted['account_username'], $user_pass, $this->posted['billing_email']); if (!$user_id) { $woocommerce->add_error('<strong>' . __('ERROR', 'woocommerce') . '</strong>: ' . __('Couldn’t register you... please contact us if you continue to have problems.', 'woocommerce')); break; } // Change role wp_update_user(array('ID' => $user_id, 'role' => 'customer')); // send the user a confirmation and their login details $mailer = $woocommerce->mailer(); $mailer->customer_new_account($user_id, $user_pass); // set the WP login cookie $secure_cookie = is_ssl() ? true : false; wp_set_auth_cookie($user_id, true, $secure_cookie); } else { $woocommerce->add_error($reg_errors->get_error_message()); break; } } // Create Order (send cart variable so we can record items and reduce inventory). Only create if this is a new order, not if the payment was rejected last time. $_tax = new WC_Tax(); $order_data = array('post_type' => 'shop_order', 'post_title' => 'Order – ' . date('F j, Y @ h:i A'), 'post_status' => 'publish', 'ping_status' => 'closed', 'post_excerpt' => $this->posted['order_comments'], 'post_author' => 1, 'post_password' => uniqid('order_')); // Cart items $order_items = array(); foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { $_product = $values['data']; // Store any item meta data - item meta class lets plugins add item meta in a standardized way $item_meta = new order_item_meta(); $item_meta->new_order_item($values); // Store variation data in meta so admin can view it if ($values['variation'] && is_array($values['variation'])) { foreach ($values['variation'] as $key => $value) { $item_meta->add(esc_attr(str_replace('attribute_', '', $key)), $value); } } $order_items[] = apply_filters('new_order_item', array('id' => $values['product_id'], 'variation_id' => $values['variation_id'], 'name' => $_product->get_title(), 'qty' => (int) $values['quantity'], 'item_meta' => $item_meta->meta, 'line_subtotal' => rtrim(rtrim(number_format($values['line_subtotal'], 4, '.', ''), '0'), '.'), 'line_subtotal_tax' => rtrim(rtrim(number_format($values['line_subtotal_tax'], 4, '.', ''), '0'), '.'), 'line_total' => rtrim(rtrim(number_format($values['line_total'], 4, '.', ''), '0'), '.'), 'line_tax' => rtrim(rtrim(number_format($values['line_tax'], 4, '.', ''), '0'), '.'), 'tax_class' => $_product->get_tax_class()), $values); } // Check order items for errors do_action('woocommerce_check_new_order_items', $order_items); if ($woocommerce->error_count() > 0) { break; } // Insert or update the post data $create_new_order = true; if (isset($_SESSION['order_awaiting_payment']) && $_SESSION['order_awaiting_payment'] > 0) { $order_id = (int) $_SESSION['order_awaiting_payment']; /* Check order is unpaid by getting its status */ $terms = wp_get_object_terms($order_id, 'shop_order_status', array('fields' => 'slugs')); $order_status = isset($terms[0]) ? $terms[0] : 'pending'; if ($order_status == 'pending') { // Resume the unpaid order $order_data['ID'] = $order_id; wp_update_post($order_data); do_action('woocommerce_resume_order', $order_id); $create_new_order = false; } } if ($create_new_order) { $order_id = wp_insert_post($order_data); if (is_wp_error($order_id)) { $woocommerce->add_error('Error: Unable to create order. Please try again.'); break; } else { // Inserted successfully do_action('woocommerce_new_order', $order_id); } } // Get better formatted billing method (title) $shipping_method = $this->posted['shipping_method']; if (isset($available_methods) && isset($available_methods[$this->posted['shipping_method']])) { $shipping_method = $available_methods[$this->posted['shipping_method']]->label; } // Get better formatted shipping method (title/label) $payment_method = $this->posted['payment_method']; if (isset($available_gateways) && isset($available_gateways[$this->posted['payment_method']])) { $payment_method = $available_gateways[$this->posted['payment_method']]->title; } // UPDATE ORDER META // Save billing and shipping first, also save to user meta if logged in if ($this->checkout_fields['billing']) { foreach ($this->checkout_fields['billing'] as $key => $field) { // Post update_post_meta($order_id, '_' . $key, $this->posted[$key]); // User if ($user_id > 0 && !empty($this->posted[$key])) { update_user_meta($user_id, $key, $this->posted[$key]); // Special fields switch ($key) { case "billing_emaill": if (!email_exists($this->posted[$key])) { wp_update_user(array('ID' => $user_id, 'user_email' => $this->posted[$key])); } break; case "billing_first_name": wp_update_user(array('ID' => $user_id, 'first_name' => $this->posted[$key])); break; case "billing_last_name": wp_update_user(array('ID' => $user_id, 'last_name' => $this->posted[$key])); break; } } } } if ($this->checkout_fields['shipping'] && $woocommerce->cart->needs_shipping()) { foreach ($this->checkout_fields['shipping'] as $key => $field) { if ($this->posted['shiptobilling']) { $field_key = str_replace('shipping_', 'billing_', $key); // Post update_post_meta($order_id, '_' . $key, $this->posted[$field_key]); } else { // Post update_post_meta($order_id, '_' . $key, $this->posted[$key]); // User if ($user_id > 0) { update_user_meta($user_id, $key, $this->posted[$key]); } } } } // Save any other user meta if ($user_id) { do_action('woocommerce_checkout_update_user_meta', $user_id, $this->posted); } // Prepare order taxes for storage $order_taxes = array(); foreach (array_keys($woocommerce->cart->taxes + $woocommerce->cart->shipping_taxes) as $key) { $is_compound = $woocommerce->cart->tax->is_compound($key) ? 1 : 0; $cart_tax = isset($woocommerce->cart->taxes[$key]) ? $woocommerce->cart->taxes[$key] : 0; $shipping_tax = isset($woocommerce->cart->shipping_taxes[$key]) ? $woocommerce->cart->shipping_taxes[$key] : 0; $order_taxes[] = array('label' => $woocommerce->cart->tax->get_rate_label($key), 'compound' => $is_compound, 'cart_tax' => number_format($cart_tax, 2, '.', ''), 'shipping_tax' => number_format($shipping_tax, 2, '.', '')); } // Save other order meta fields update_post_meta($order_id, '_shipping_method', $this->posted['shipping_method']); update_post_meta($order_id, '_payment_method', $this->posted['payment_method']); update_post_meta($order_id, '_shipping_method_title', $shipping_method); update_post_meta($order_id, '_payment_method_title', $payment_method); update_post_meta($order_id, '_order_shipping', number_format($woocommerce->cart->shipping_total, 2, '.', '')); update_post_meta($order_id, '_order_discount', number_format($woocommerce->cart->get_order_discount_total(), 2, '.', '')); update_post_meta($order_id, '_cart_discount', number_format($woocommerce->cart->get_cart_discount_total(), 2, '.', '')); update_post_meta($order_id, '_order_tax', number_format($woocommerce->cart->tax_total, 2, '.', '')); update_post_meta($order_id, '_order_shipping_tax', number_format($woocommerce->cart->shipping_tax_total, 2, '.', '')); update_post_meta($order_id, '_order_total', number_format($woocommerce->cart->total, 2, '.', '')); update_post_meta($order_id, '_order_key', apply_filters('woocommerce_generate_order_key', uniqid('order_'))); update_post_meta($order_id, '_customer_user', (int) $user_id); update_post_meta($order_id, '_order_items', $order_items); update_post_meta($order_id, '_order_taxes', $order_taxes); update_post_meta($order_id, '_order_currency', get_option('woocommerce_currency')); update_post_meta($order_id, '_prices_include_tax', get_option('woocommerce_prices_include_tax')); do_action('woocommerce_checkout_update_order_meta', $order_id, $this->posted); // Order status wp_set_object_terms($order_id, 'pending', 'shop_order_status'); // Discount code meta if ($applied_coupons = $woocommerce->cart->get_applied_coupons()) { update_post_meta($order_id, 'coupons', implode(', ', $applied_coupons)); } // Order is saved do_action('woocommerce_checkout_order_processed', $order_id, $this->posted); // Prevent timeout @set_time_limit(0); // Process payment if ($woocommerce->cart->needs_payment()) { // Store Order ID in session so it can be re-used after payment failure $_SESSION['order_awaiting_payment'] = $order_id; // Process Payment $result = $available_gateways[$this->posted['payment_method']]->process_payment($order_id); // Redirect to success/confirmation/payment page if ($result['result'] == 'success') { $result = apply_filters('woocommerce_payment_successful_result', $result); if (is_ajax()) { echo json_encode($result); exit; } else { wp_redirect($result['redirect']); exit; } } } else { $order = new WC_Order($order_id); // No payment was required for order $order->payment_complete(); // Empty the Cart $woocommerce->cart->empty_cart(); // Get redirect $return_url = get_permalink(woocommerce_get_page_id('thanks')); $return_url = add_query_arg('key', $order->order_key, add_query_arg('order', $order->id, $return_url)); // Redirect to success/confirmation/payment page if (is_ajax()) { echo json_encode(array('result' => 'success', 'redirect' => apply_filters('woocommerce_checkout_no_payment_needed_redirect', $return_url, $order))); exit; } else { wp_safe_redirect(apply_filters('woocommerce_checkout_no_payment_needed_redirect', $return_url, $order)); exit; } } // Break out of loop break; } } // If we reached this point then there were errors if (is_ajax()) { ob_start(); $woocommerce->show_messages(); $messages = ob_get_clean(); echo json_encode(array('result' => 'failure', 'messages' => $messages, 'refresh' => isset($_SESSION['refresh_totals']) ? 'true' : 'false')); unset($_SESSION['refresh_totals']); exit; } }
/** Output items for display in html emails */ function email_order_items_table($show_download_links = false, $show_sku = false) { $return = ''; foreach ($this->get_items() as $item) { $_product = $this->get_product_from_item($item); $file = $sku = $variation = ''; if ($show_sku && $_product->get_sku()) { $sku = ' (#' . $_product->get_sku() . ')'; } $item_meta = new order_item_meta($item['item_meta']); $variation = '<br/><small>' . $item_meta->display(true, true) . '</small>'; if ($show_download_links) { if ($_product->exists) { if ($_product->is_downloadable()) { $file = '<br/><small>' . __('Download:', 'woocommerce') . ' <a href="' . $this->get_downloadable_file_url($item['id'], $item['variation_id']) . '">' . $this->get_downloadable_file_url($item['id'], $item['variation_id']) . '</a></small>'; } } } $return .= '<tr> <td style="text-align:left; border: 1px solid #eee;">' . apply_filters('woocommerce_order_product_title', $item['name'], $_product) . $sku . $file . $variation . '</td> <td style="text-align:left; border: 1px solid #eee;">' . $item['qty'] . '</td> <td style="text-align:left; border: 1px solid #eee;">'; if ($this->display_cart_ex_tax || !$this->prices_include_tax) { $ex_tax_label = $this->prices_include_tax ? 1 : 0; $return .= woocommerce_price($this->get_line_subtotal($item), array('ex_tax_label' => $ex_tax_label)); } else { $return .= woocommerce_price($this->get_line_subtotal($item, true)); } $return .= ' </td> </tr>'; } $return = apply_filters('woocommerce_email_order_items_table', $return); return $return; }
function woocommerce_process_shop_order_meta($post_id, $post) { global $wpdb, $woocommerce, $woocommerce_errors; // Add key add_post_meta($post_id, '_order_key', uniqid('order_'), true); // Update post data update_post_meta($post_id, '_billing_first_name', stripslashes($_POST['_billing_first_name'])); update_post_meta($post_id, '_billing_last_name', stripslashes($_POST['_billing_last_name'])); update_post_meta($post_id, '_billing_company', stripslashes($_POST['_billing_company'])); update_post_meta($post_id, '_billing_address_1', stripslashes($_POST['_billing_address_1'])); update_post_meta($post_id, '_billing_address_2', stripslashes($_POST['_billing_address_2'])); update_post_meta($post_id, '_billing_city', stripslashes($_POST['_billing_city'])); update_post_meta($post_id, '_billing_postcode', stripslashes($_POST['_billing_postcode'])); update_post_meta($post_id, '_billing_country', stripslashes($_POST['_billing_country'])); update_post_meta($post_id, '_billing_state', stripslashes($_POST['_billing_state'])); update_post_meta($post_id, '_billing_email', stripslashes($_POST['_billing_email'])); update_post_meta($post_id, '_billing_phone', stripslashes($_POST['_billing_phone'])); update_post_meta($post_id, '_shipping_first_name', stripslashes($_POST['_shipping_first_name'])); update_post_meta($post_id, '_shipping_last_name', stripslashes($_POST['_shipping_last_name'])); update_post_meta($post_id, '_shipping_company', stripslashes($_POST['_shipping_company'])); update_post_meta($post_id, '_shipping_address_1', stripslashes($_POST['_shipping_address_1'])); update_post_meta($post_id, '_shipping_address_2', stripslashes($_POST['_shipping_address_2'])); update_post_meta($post_id, '_shipping_city', stripslashes($_POST['_shipping_city'])); update_post_meta($post_id, '_shipping_postcode', stripslashes($_POST['_shipping_postcode'])); update_post_meta($post_id, '_shipping_country', stripslashes($_POST['_shipping_country'])); update_post_meta($post_id, '_shipping_state', stripslashes($_POST['_shipping_state'])); update_post_meta($post_id, '_order_shipping', stripslashes($_POST['_order_shipping'])); update_post_meta($post_id, '_cart_discount', stripslashes($_POST['_cart_discount'])); update_post_meta($post_id, '_order_discount', stripslashes($_POST['_order_discount'])); update_post_meta($post_id, '_order_total', stripslashes($_POST['_order_total'])); update_post_meta($post_id, '_customer_user', (int) $_POST['customer_user']); update_post_meta($post_id, '_order_tax', stripslashes($_POST['_order_tax'])); update_post_meta($post_id, '_order_shipping_tax', stripslashes($_POST['_order_shipping_tax'])); // Shipping method handling if (get_post_meta($post_id, '_shipping_method', true) !== stripslashes($_POST['_shipping_method'])) { update_post_meta($post_id, '_shipping_method', stripslashes($_POST['_shipping_method'])); update_post_meta($post_id, '_shipping_method_title', stripslashes($_POST['_shipping_method'])); } // Payment method handling if (get_post_meta($post_id, '_payment_method', true) !== stripslashes($_POST['_payment_method'])) { update_post_meta($post_id, '_payment_method', stripslashes($_POST['_payment_method'])); update_post_meta($post_id, '_payment_method_title', stripslashes($_POST['_payment_method'])); } // Update date if (empty($_POST['order_date'])) { $date = current_time('timestamp'); } else { $date = strtotime($_POST['order_date'] . ' ' . (int) $_POST['order_date_hour'] . ':' . (int) $_POST['order_date_minute'] . ':00'); } $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_date = %s WHERE ID = %s", date('Y-m-d H:i:s', $date), $post_id)); // Tax rows $order_taxes = array(); if (isset($_POST['_order_taxes_label'])) { $order_taxes_label = $_POST['_order_taxes_label']; $order_taxes_compound = isset($_POST['_order_taxes_compound']) ? $_POST['_order_taxes_compound'] : array(); $order_taxes_cart = $_POST['_order_taxes_cart']; $order_taxes_shipping = $_POST['_order_taxes_shipping']; $order_taxes_label_count = sizeof($order_taxes_label); for ($i = 0; $i < $order_taxes_label_count; $i++) { // Add to array if the tax amount is set if (!$order_taxes_cart[$i] && !$order_taxes_shipping[$i]) { continue; } if (!$order_taxes_label[$i]) { $order_taxes_label[$i] = $woocommerce->countries->tax_or_vat(); } if (isset($order_taxes_compound[$i])) { $is_compound = 1; } else { $is_compound = 0; } $order_taxes[] = array('label' => esc_attr($order_taxes_label[$i]), 'compound' => $is_compound, 'cart_tax' => esc_attr($order_taxes_cart[$i]), 'shipping_tax' => esc_attr($order_taxes_shipping[$i])); } } update_post_meta($post_id, '_order_taxes', $order_taxes); // Order items $order_items = array(); if (isset($_POST['item_id'])) { $item_id = $_POST['item_id']; $item_variation = $_POST['item_variation']; $item_name = $_POST['item_name']; $item_quantity = $_POST['item_quantity']; $line_subtotal = $_POST['line_subtotal']; $line_subtotal_tax = $_POST['line_subtotal_tax']; $line_total = $_POST['line_total']; $line_tax = $_POST['line_tax']; $item_meta_names = isset($_POST['meta_name']) ? $_POST['meta_name'] : ''; $item_meta_values = isset($_POST['meta_value']) ? $_POST['meta_value'] : ''; $item_tax_class = $_POST['item_tax_class']; $item_id_count = sizeof($item_id); for ($i = 0; $i < $item_id_count; $i++) { if (!isset($item_id[$i]) || !$item_id[$i]) { continue; } if (!isset($item_name[$i])) { continue; } if (!isset($item_quantity[$i]) || $item_quantity[$i] < 1) { continue; } if (!isset($line_total[$i])) { continue; } if (!isset($line_tax[$i])) { continue; } // Meta $item_meta = new order_item_meta(); if (isset($item_meta_names[$i]) && isset($item_meta_values[$i])) { $meta_names = $item_meta_names[$i]; $meta_values = $item_meta_values[$i]; $meta_names_count = sizeof($meta_names); for ($ii = 0; $ii < $meta_names_count; $ii++) { $meta_name = esc_attr($meta_names[$ii]); $meta_value = esc_attr($meta_values[$ii]); if ($meta_name && $meta_value) { $item_meta->add($meta_name, $meta_value); } } } // Add to array $order_items[] = apply_filters('update_order_item', array('id' => htmlspecialchars(stripslashes($item_id[$i])), 'variation_id' => (int) $item_variation[$i], 'name' => htmlspecialchars(stripslashes($item_name[$i])), 'qty' => (int) $item_quantity[$i], 'line_total' => rtrim(rtrim(number_format(woocommerce_clean($line_total[$i]), 4, '.', ''), '0'), '.'), 'line_tax' => rtrim(rtrim(number_format(woocommerce_clean($line_tax[$i]), 4, '.', ''), '0'), '.'), 'line_subtotal' => rtrim(rtrim(number_format(woocommerce_clean($line_subtotal[$i]), 4, '.', ''), '0'), '.'), 'line_subtotal_tax' => rtrim(rtrim(number_format(woocommerce_clean($line_subtotal_tax[$i]), 4, '.', ''), '0'), '.'), 'item_meta' => $item_meta->meta, 'tax_class' => woocommerce_clean($item_tax_class[$i]))); } } update_post_meta($post_id, '_order_items', $order_items); // Order data saved, now get it so we can manipulate status $order = new WC_Order($post_id); // Order status $order->update_status($_POST['order_status']); // Handle button actions if (isset($_POST['reduce_stock']) && $_POST['reduce_stock'] && sizeof($order_items) > 0) { $order->add_order_note(__('Manually reducing stock.', 'woocommerce')); foreach ($order_items as $order_item) { $_product = $order->get_product_from_item($order_item); if ($_product->exists()) { if ($_product->managing_stock()) { $old_stock = $_product->stock; $new_quantity = $_product->reduce_stock($order_item['qty']); $order->add_order_note(sprintf(__('Item #%s stock reduced from %s to %s.', 'woocommerce'), $order_item['id'], $old_stock, $new_quantity)); $order->send_stock_notifications($_product, $new_quantity, $order_item['qty']); } } else { $order->add_order_note(sprintf(__('Item %s %s not found, skipping.', 'woocommerce'), $order_item['id'], $order_item['name'])); } } $order->add_order_note(__('Manual stock reduction complete.', 'woocommerce')); do_action('woocommerce_reduce_order_stock', $order); } elseif (isset($_POST['restore_stock']) && $_POST['restore_stock'] && sizeof($order_items) > 0) { $order->add_order_note(__('Manually restoring stock.', 'woocommerce')); foreach ($order_items as $order_item) { $_product = $order->get_product_from_item($order_item); if ($_product->exists()) { if ($_product->managing_stock()) { $old_stock = $_product->stock; $new_quantity = $_product->increase_stock($order_item['qty']); $order->add_order_note(sprintf(__('Item #%s stock increased from %s to %s.', 'woocommerce'), $order_item['id'], $old_stock, $new_quantity)); } } else { $order->add_order_note(sprintf(__('Item %s %s not found, skipping.', 'woocommerce'), $order_item['id'], $order_item['name'])); } } $order->add_order_note(__('Manual stock restore complete.', 'woocommerce')); do_action('woocommerce_restore_order_stock', $order); } elseif (isset($_POST['invoice']) && $_POST['invoice']) { do_action('woocommerce_before_send_customer_invoice', $order); $mailer = $woocommerce->mailer(); $mailer->customer_invoice($order); do_action('woocommerce_after__customer_invoice', $order); } delete_transient('woocommerce_processing_order_count'); }
/** * Get PayPal Args for passing to PP **/ function get_paypal_args($order) { global $woocommerce; $order_id = $order->id; if ($this->debug == 'yes') { $this->log->add('paypal', 'Generating payment form for order #' . $order_id . '. Notify URL: ' . trailingslashit(home_url()) . '?paypalListener=paypal_standard_IPN'); } if (in_array($order->billing_country, array('US', 'CA'))) { $order->billing_phone = str_replace(array('(', '-', ' ', ')'), '', $order->billing_phone); $phone_args = array('night_phone_a' => substr($order->billing_phone, 0, 3), 'night_phone_b' => substr($order->billing_phone, 3, 3), 'night_phone_c' => substr($order->billing_phone, 6, 4), 'day_phone_a' => substr($order->billing_phone, 0, 3), 'day_phone_b' => substr($order->billing_phone, 3, 3), 'day_phone_c' => substr($order->billing_phone, 6, 4)); } else { $phone_args = array('night_phone_b' => $order->billing_phone, 'day_phone_b' => $order->billing_phone); } // PayPal Args $paypal_args = array_merge(array('cmd' => '_cart', 'business' => $this->email, 'no_note' => 1, 'currency_code' => get_option('woocommerce_currency'), 'charset' => 'UTF-8', 'rm' => 2, 'upload' => 1, 'return' => $this->get_return_url($order), 'cancel_return' => $order->get_cancel_order_url(), 'custom' => $order_id, 'notify_url' => trailingslashit(home_url()) . '?paypalListener=paypal_standard_IPN', 'first_name' => $order->billing_first_name, 'last_name' => $order->billing_last_name, 'company' => $order->billing_company, 'address1' => $order->billing_address_1, 'address2' => $order->billing_address_2, 'city' => $order->billing_city, 'state' => $order->billing_state, 'zip' => $order->billing_postcode, 'country' => $order->billing_country, 'email' => $order->billing_email, 'invoice' => $order->order_key), $phone_args); // Shipping if ($this->send_shipping == 'yes') { $paypal_args['no_shipping'] = 0; $paypal_args['address_override'] = 1; // If we are sending shipping, send shipping address instead of billing $paypal_args['first_name'] = $order->shipping_first_name; $paypal_args['last_name'] = $order->shipping_last_name; $paypal_args['company'] = $order->shipping_company; $paypal_args['address1'] = $order->shipping_address_1; $paypal_args['address2'] = $order->shipping_address_2; $paypal_args['city'] = $order->shipping_city; $paypal_args['state'] = $order->shipping_state; $paypal_args['country'] = $order->shipping_country; $paypal_args['zip'] = $order->shipping_postcode; } else { $paypal_args['no_shipping'] = 1; } // If prices include tax or have order discounts, send the whole order if (get_option('woocommerce_prices_include_tax') == 'yes' || $order->get_order_discount() > 0) { // Discount $paypal_args['discount_amount_cart'] = $order->get_order_discount(); // Don't pass items - paypal borks tax due to prices including tax. PayPal has no option for tax inclusive pricing sadly. Pass 1 item for the order items overall $item_names = array(); if (sizeof($order->get_items()) > 0) { foreach ($order->get_items() as $item) { if ($item['qty']) { $item_names[] = $item['name'] . ' x ' . $item['qty']; } } } $paypal_args['item_name_1'] = sprintf(__('Order #%s', 'woocommerce'), $order->id) . " - " . implode(', ', $item_names); $paypal_args['quantity_1'] = 1; $paypal_args['amount_1'] = number_format($order->get_order_total() - $order->get_shipping() + $order->get_order_discount(), 2, '.', ''); // Shipping Cost if ($order->get_shipping() > 0) { $paypal_args['item_name_2'] = __('Shipping via', 'woocommerce') . ucwords($order->shipping_method_title); $paypal_args['quantity_2'] = '1'; $paypal_args['amount_2'] = number_format($order->get_shipping(), 2, '.', ''); } } else { // Tax $paypal_args['tax_cart'] = $order->get_total_tax(); // Cart Contents $item_loop = 0; if (sizeof($order->get_items()) > 0) { foreach ($order->get_items() as $item) { if ($item['qty']) { $item_loop++; $product = $order->get_product_from_item($item); $item_name = $item['name']; $item_meta = new order_item_meta($item['item_meta']); if ($meta = $item_meta->display(true, true)) { $item_name .= ' (' . $meta . ')'; } $paypal_args['item_name_' . $item_loop] = $item_name; if ($product->get_sku()) { $paypal_args['item_number_' . $item_loop] = $product->get_sku(); } $paypal_args['quantity_' . $item_loop] = $item['qty']; $paypal_args['amount_' . $item_loop] = $order->get_item_total($item, false); } } } // Shipping Cost if ($order->get_shipping() > 0) { $item_loop++; $paypal_args['item_name_' . $item_loop] = __('Shipping via', 'woocommerce') . ucwords($order->shipping_method_title); $paypal_args['quantity_' . $item_loop] = '1'; $paypal_args['amount_' . $item_loop] = number_format($order->get_shipping(), 2, '.', ''); } } $paypal_args = apply_filters('woocommerce_paypal_args', $paypal_args); return $paypal_args; }
<?php /** * Email Order Item * * Shows a line item inside the order emails table */ global $woocommerce; foreach ($items as $item) { // Get/prep product data $_product = $order->get_product_from_item($item); $item_meta = new order_item_meta($item['item_meta']); $image = $show_image ? '<img src="' . current(wp_get_attachment_image_src(get_post_thumbnail_id($_product->id), 'thumbnail')) . '" alt="Product Image" height="' . $image_size[1] . '" width="' . $image_size[0] . '" style="vertical-align:middle; margin-right: 10px;" />' : ''; ?> <tr> <td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php // Show title/image etc echo apply_filters('woocommerce_order_product_image', $image, $_product, $show_image); // Product name echo apply_filters('woocommerce_order_product_title', $item['name'], $_product); // SKU echo $show_sku && $_product->get_sku() ? ' (#' . $_product->get_sku() . ')' : ''; // File URL echo $show_download_links && $_product->exists() && $_product->is_downloadable() ? '<br/><small>' . __('Download:', 'woocommerce') . ' <a href="' . $order->get_downloadable_file_url($item['id'], $item['variation_id']) . '" target="_blank">' . $order->get_downloadable_file_url($item['id'], $item['variation_id']) . '</a></small>' : ''; // Variation echo $item_meta->meta ? '<br/><small>' . $item_meta->display(true, true) . '</small>' : ''; ?> </td> <td style="text-align:left; vertical-align:middle; border: 1px solid #eee;"><?php echo $item['qty']; ?>
/** * Generate the paypal button link **/ public function generate_paypal_form($order_id) { global $woocommerce; $order = new WC_Order($order_id); if ($this->testmode == 'yes') { $paypal_adr = $this->testurl . '?test_ipn=1&'; } else { $paypal_adr = $this->liveurl . '?'; } if ($this->debug == 'yes') { $this->log->add('paypal', 'Generating payment form for order #' . $order_id . '. Notify URL: ' . trailingslashit(home_url()) . '?paypalListener=paypal_standard_IPN'); } if (in_array($order->billing_country, array('US', 'CA'))) { $order->billing_phone = str_replace(array('(', '-', ' ', ')'), '', $order->billing_phone); $phone_args = array('night_phone_a' => substr($order->billing_phone, 0, 3), 'night_phone_b' => substr($order->billing_phone, 3, 3), 'night_phone_c' => substr($order->billing_phone, 6, 4), 'day_phone_a' => substr($order->billing_phone, 0, 3), 'day_phone_b' => substr($order->billing_phone, 3, 3), 'day_phone_c' => substr($order->billing_phone, 6, 4)); } else { $phone_args = array('night_phone_b' => $order->billing_phone, 'day_phone_b' => $order->billing_phone); } // PayPal Args $paypal_args = array_merge(array('cmd' => '_cart', 'business' => $this->email, 'no_note' => 1, 'currency_code' => get_option('woocommerce_currency'), 'charset' => 'UTF-8', 'rm' => 2, 'upload' => 1, 'return' => $this->get_return_url($order), 'cancel_return' => $order->get_cancel_order_url(), 'custom' => $order_id, 'notify_url' => trailingslashit(home_url()) . '?paypalListener=paypal_standard_IPN', 'first_name' => $order->billing_first_name, 'last_name' => $order->billing_last_name, 'company' => $order->billing_company, 'address1' => $order->billing_address_1, 'address2' => $order->billing_address_2, 'city' => $order->billing_city, 'state' => $order->billing_state, 'zip' => $order->billing_postcode, 'country' => $order->billing_country, 'email' => $order->billing_email, 'invoice' => $order->order_key), $phone_args); // Shipping if ($this->send_shipping == 'yes') { $paypal_args['no_shipping'] = 0; $paypal_args['address_override'] = 1; // If we are sending shipping, send shipping address instead of billing $paypal_args['first_name'] = $order->shipping_first_name; $paypal_args['last_name'] = $order->shipping_last_name; $paypal_args['company'] = $order->shipping_company; $paypal_args['address1'] = $order->shipping_address_1; $paypal_args['address2'] = $order->shipping_address_2; $paypal_args['city'] = $order->shipping_city; $paypal_args['state'] = $order->shipping_state; $paypal_args['zip'] = $order->shipping_postcode; $paypal_args['country'] = $order->shipping_country; } else { $paypal_args['no_shipping'] = 1; } // If prices include tax or have order discounts, send the whole order if (get_option('woocommerce_prices_include_tax') == 'yes' || $order->get_order_discount() > 0) { // Discount $paypal_args['discount_amount_cart'] = $order->get_order_discount(); // Don't pass items - paypal borks tax due to prices including tax. PayPal has no option for tax inclusive pricing sadly. Pass 1 item for the order items overall $paypal_args['item_name_1'] = sprintf(__('Order #%s', 'woocommerce'), $order->id); $paypal_args['quantity_1'] = 1; $paypal_args['amount_1'] = number_format($order->get_order_total() - $order->get_shipping() + $order->get_order_discount(), 2, '.', ''); // Shipping Cost if ($order->get_shipping() > 0) { $paypal_args['item_name_2'] = __('Shipping via ', 'woocommerce') . ucwords($order->shipping_method_title); $paypal_args['quantity_2'] = '1'; $paypal_args['amount_2'] = number_format($order->get_shipping(), 2, '.', ''); } } else { // Tax $paypal_args['tax_cart'] = $order->get_total_tax(); // Cart Contents $item_loop = 0; if (sizeof($order->get_items()) > 0) { foreach ($order->get_items() as $item) { if ($item['qty']) { $item_loop++; $product = $order->get_product_from_item($item); $item_name = $item['name']; if (get_option('woocommerce_enable_sku') == 'yes' && $product->get_sku()) { $item_name .= ' (' . $product->get_sku() . ')'; } $item_meta = new order_item_meta($item['item_meta']); if ($meta = $item_meta->display(true, true)) { $item_name .= ' (' . $meta . ')'; } $paypal_args['item_name_' . $item_loop] = $item_name; $paypal_args['quantity_' . $item_loop] = $item['qty']; $paypal_args['amount_' . $item_loop] = $order->get_item_total($item, false); } } } // Shipping Cost if ($order->get_shipping() > 0) { $item_loop++; $paypal_args['item_name_' . $item_loop] = __('Shipping via ', 'woocommerce') . ucwords($order->shipping_method_title); $paypal_args['quantity_' . $item_loop] = '1'; $paypal_args['amount_' . $item_loop] = number_format($order->get_shipping(), 2, '.', ''); } } $paypal_args = apply_filters('woocommerce_paypal_args', $paypal_args); $paypal_args_array = array(); foreach ($paypal_args as $key => $value) { $paypal_args_array[] = '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_attr($value) . '" />'; } $woocommerce->add_inline_js(' jQuery("body").block({ message: "<img src=\\"' . esc_url($woocommerce->plugin_url()) . '/assets/images/ajax-loader.gif\\" alt=\\"Redirecting...\\" style=\\"float:left; margin-right: 10px;\\" />' . __('Thank you for your order. We are now redirecting you to PayPal to make payment.', 'woocommerce') . '", overlayCSS: { background: "#fff", opacity: 0.6 }, css: { padding: 20, textAlign: "center", color: "#555", border: "3px solid #aaa", backgroundColor:"#fff", cursor: "wait", lineHeight: "32px" } }); jQuery("#submit_paypal_payment_form").click(); '); return '<form action="' . esc_url($paypal_adr) . '" method="post" id="paypal_payment_form"> ' . implode('', $paypal_args_array) . ' <input type="submit" class="button-alt" id="submit_paypal_payment_form" value="' . __('Pay via PayPal', 'woocommerce') . '" /> <a class="button cancel" href="' . esc_url($order->get_cancel_order_url()) . '">' . __('Cancel order & restore cart', 'woocommerce') . '</a> </form>'; }
/** Output items for display in html emails */ function email_order_items_table($show_download_links = false, $show_sku = false, $show_purchase_note = false, $show_image = false, $image_size = array(32, 32)) { $return = ''; foreach ($this->get_items() as $item) { $_product = $this->get_product_from_item($item); $file = $sku = $variation = $image = ''; if ($show_image) { $src = wp_get_attachment_image_src(get_post_thumbnail_id($_product->id), 'thumbnail'); $image = apply_filters('woocommerce_order_product_image', '<img src="' . $src[0] . '" alt="Product Image" height="' . $image_size[1] . '" width="' . $image_size[0] . '" style="vertical-align:middle; margin-right: 10px;" />', $_product); } if ($show_sku && $_product->get_sku()) { $sku = ' (#' . $_product->get_sku() . ')'; } $item_meta = new order_item_meta($item['item_meta']); $variation = '<br/><small>' . $item_meta->display(true, true) . '</small>'; if ($show_download_links) { if ($_product->exists) { if ($_product->is_downloadable()) { $file = '<br/><small>' . __('Download:', 'woocommerce') . ' <a href="' . $this->get_downloadable_file_url($item['id'], $item['variation_id']) . '">' . $this->get_downloadable_file_url($item['id'], $item['variation_id']) . '</a></small>'; } } } $return .= '<tr> <td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">' . $image . apply_filters('woocommerce_order_product_title', $item['name'], $_product) . $sku . $file . $variation . '</td> <td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">' . $item['qty'] . '</td> <td style="text-align:left; vertical-align:middle; border: 1px solid #eee;">'; if ($this->display_cart_ex_tax || !$this->prices_include_tax) { $ex_tax_label = $this->prices_include_tax ? 1 : 0; $return .= woocommerce_price($this->get_line_subtotal($item), array('ex_tax_label' => $ex_tax_label)); } else { $return .= woocommerce_price($this->get_line_subtotal($item, true)); } $return .= ' </td> </tr>'; // Show any purchase notes if ($show_purchase_note) { if ($purchase_note = get_post_meta($_product->id, '_purchase_note', true)) { $return .= '<tr><td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee;">' . apply_filters('the_content', $purchase_note) . '</td></tr>'; } } } $return = apply_filters('woocommerce_email_order_items_table', $return); return $return; }
function woocommerce_process_shop_order_meta($post_id, $post) { global $wpdb, $woocommerce; $woocommerce_errors = array(); // Add key add_post_meta($post_id, '_order_key', uniqid('order_')); // Update post data update_post_meta($post_id, '_billing_first_name', stripslashes($_POST['_billing_first_name'])); update_post_meta($post_id, '_billing_last_name', stripslashes($_POST['_billing_last_name'])); update_post_meta($post_id, '_billing_company', stripslashes($_POST['_billing_company'])); update_post_meta($post_id, '_billing_address_1', stripslashes($_POST['_billing_address_1'])); update_post_meta($post_id, '_billing_address_2', stripslashes($_POST['_billing_address_2'])); update_post_meta($post_id, '_billing_city', stripslashes($_POST['_billing_city'])); update_post_meta($post_id, '_billing_postcode', stripslashes($_POST['_billing_postcode'])); update_post_meta($post_id, '_billing_country', stripslashes($_POST['_billing_country'])); update_post_meta($post_id, '_billing_state', stripslashes($_POST['_billing_state'])); update_post_meta($post_id, '_billing_email', stripslashes($_POST['_billing_email'])); update_post_meta($post_id, '_billing_phone', stripslashes($_POST['_billing_phone'])); update_post_meta($post_id, '_shipping_first_name', stripslashes($_POST['_shipping_first_name'])); update_post_meta($post_id, '_shipping_last_name', stripslashes($_POST['_shipping_last_name'])); update_post_meta($post_id, '_shipping_company', stripslashes($_POST['_shipping_company'])); update_post_meta($post_id, '_shipping_address_1', stripslashes($_POST['_shipping_address_1'])); update_post_meta($post_id, '_shipping_address_2', stripslashes($_POST['_shipping_address_2'])); update_post_meta($post_id, '_shipping_city', stripslashes($_POST['_shipping_city'])); update_post_meta($post_id, '_shipping_postcode', stripslashes($_POST['_shipping_postcode'])); update_post_meta($post_id, '_shipping_country', stripslashes($_POST['_shipping_country'])); update_post_meta($post_id, '_shipping_state', stripslashes($_POST['_shipping_state'])); update_post_meta($post_id, '_shipping_method', stripslashes($_POST['_shipping_method'])); update_post_meta($post_id, '_payment_method', stripslashes($_POST['_payment_method'])); update_post_meta($post_id, '_order_shipping', stripslashes($_POST['_order_shipping'])); update_post_meta($post_id, '_cart_discount', stripslashes($_POST['_cart_discount'])); update_post_meta($post_id, '_order_discount', stripslashes($_POST['_order_discount'])); update_post_meta($post_id, '_order_total', stripslashes($_POST['_order_total'])); update_post_meta($post_id, '_customer_user', (int) $_POST['customer_user']); update_post_meta($post_id, '_order_tax', stripslashes($_POST['_order_tax'])); update_post_meta($post_id, '_order_shipping_tax', stripslashes($_POST['_order_shipping_tax'])); // Tax rows $order_taxes = array(); if (isset($_POST['_order_taxes_label'])) { $order_taxes_label = $_POST['_order_taxes_label']; $order_taxes_compound = isset($_POST['_order_taxes_compound']) ? $_POST['_order_taxes_compound'] : array(); $order_taxes_cart = $_POST['_order_taxes_cart']; $order_taxes_shipping = $_POST['_order_taxes_shipping']; for ($i = 0; $i < sizeof($order_taxes_label); $i++) { // Add to array if the tax amount is set if (!$order_taxes_cart[$i] && !$order_taxes_shipping[$i]) { continue; } if (!$order_taxes_label[$i]) { $order_taxes_label[$i] = $woocommerce->countries->tax_or_vat(); } if (isset($order_taxes_compound[$i])) { $is_compound = 1; } else { $is_compound = 0; } $order_taxes[] = array('label' => esc_attr($order_taxes_label[$i]), 'compound' => $is_compound, 'cart_tax' => esc_attr($order_taxes_cart[$i]), 'shipping_tax' => esc_attr($order_taxes_shipping[$i])); } } update_post_meta($post_id, '_order_taxes', $order_taxes); // Order items $order_items = array(); if (isset($_POST['item_id'])) { $item_id = $_POST['item_id']; $item_variation = $_POST['item_variation']; $item_name = $_POST['item_name']; $item_quantity = $_POST['item_quantity']; $line_subtotal = $_POST['line_subtotal']; $line_subtotal_tax = $_POST['line_subtotal_tax']; $line_total = $_POST['line_total']; $line_tax = $_POST['line_tax']; $item_meta_names = isset($_POST['meta_name']) ? $_POST['meta_name'] : ''; $item_meta_values = isset($_POST['meta_value']) ? $_POST['meta_value'] : ''; $item_tax_class = $_POST['item_tax_class']; for ($i = 0; $i < sizeof($item_id); $i++) { if (!isset($item_id[$i]) || !$item_id[$i]) { continue; } if (!isset($item_name[$i])) { continue; } if (!isset($item_quantity[$i]) || $item_quantity[$i] < 1) { continue; } if (!isset($line_total[$i])) { continue; } if (!isset($line_tax[$i])) { continue; } // Meta $item_meta = new order_item_meta(); if (isset($item_meta_names[$i]) && isset($item_meta_values[$i])) { $meta_names = $item_meta_names[$i]; $meta_values = $item_meta_values[$i]; for ($ii = 0; $ii < sizeof($meta_names); $ii++) { $meta_name = esc_attr($meta_names[$ii]); $meta_value = esc_attr($meta_values[$ii]); if ($meta_name && $meta_value) { $item_meta->add($meta_name, $meta_value); } } } // Add to array $order_items[] = apply_filters('update_order_item', array('id' => htmlspecialchars(stripslashes($item_id[$i])), 'variation_id' => (int) $item_variation[$i], 'name' => htmlspecialchars(stripslashes($item_name[$i])), 'qty' => (int) $item_quantity[$i], 'line_total' => rtrim(rtrim(number_format(woocommerce_clean($line_total[$i]), 4, '.', ''), '0'), '.'), 'line_tax' => rtrim(rtrim(number_format(woocommerce_clean($line_tax[$i]), 4, '.', ''), '0'), '.'), 'line_subtotal' => rtrim(rtrim(number_format(woocommerce_clean($line_subtotal[$i]), 4, '.', ''), '0'), '.'), 'line_subtotal_tax' => rtrim(rtrim(number_format(woocommerce_clean($line_subtotal_tax[$i]), 4, '.', ''), '0'), '.'), 'item_meta' => $item_meta->meta, 'tax_class' => woocommerce_clean($item_tax_class[$i]))); } } update_post_meta($post_id, '_order_items', $order_items); // Give a password - not used, but can protect the content/comments from theme functions if ($post->post_password == '') { $order_post = array(); $order_post['ID'] = $post_id; $order_post['post_password'] = uniqid('order_'); wp_update_post($order_post); } // Order data saved, now get it so we can manipulate status $order = new WC_Order($post_id); // Order status $order->update_status($_POST['order_status']); // Handle button actions if (isset($_POST['reduce_stock']) && $_POST['reduce_stock'] && sizeof($order_items) > 0) { $order->add_order_note(__('Manually reducing stock.', 'woocommerce')); foreach ($order_items as $order_item) { $_product = $order->get_product_from_item($order_item); if ($_product->exists) { if ($_product->managing_stock()) { $old_stock = $_product->stock; $new_quantity = $_product->reduce_stock($order_item['qty']); $order->add_order_note(sprintf(__('Item #%s stock reduced from %s to %s.', 'woocommerce'), $order_item['id'], $old_stock, $new_quantity)); if ($new_quantity < 0) { do_action('woocommerce_product_on_backorder', array('product' => $_product, 'order_id' => $post_id, 'quantity' => $order_item['qty'])); } // stock status notifications if (get_option('woocommerce_notify_no_stock_amount') && get_option('woocommerce_notify_no_stock_amount') >= $new_quantity) { do_action('woocommerce_no_stock', $_product); } elseif (get_option('woocommerce_notify_low_stock_amount') && get_option('woocommerce_notify_low_stock_amount') >= $new_quantity) { do_action('woocommerce_low_stock', $_product); } } } else { $order->add_order_note(sprintf(__('Item %s %s not found, skipping.', 'woocommerce'), $order_item['id'], $order_item['name'])); } } $order->add_order_note(__('Manual stock reduction complete.', 'woocommerce')); } elseif (isset($_POST['restore_stock']) && $_POST['restore_stock'] && sizeof($order_items) > 0) { $order->add_order_note(__('Manually restoring stock.', 'woocommerce')); foreach ($order_items as $order_item) { $_product = $order->get_product_from_item($order_item); if ($_product->exists) { if ($_product->managing_stock()) { $old_stock = $_product->stock; $new_quantity = $_product->increase_stock($order_item['qty']); $order->add_order_note(sprintf(__('Item #%s stock increased from %s to %s.', 'woocommerce'), $order_item['id'], $old_stock, $new_quantity)); } } else { $order->add_order_note(sprintf(__('Item %s %s not found, skipping.', 'woocommerce'), $order_item['id'], $order_item['name'])); } } $order->add_order_note(__('Manual stock restore complete.', 'woocommerce')); } elseif (isset($_POST['invoice']) && $_POST['invoice']) { // Mail link to customer global $woocommerce; $mailer = $woocommerce->mailer(); $mailer->customer_invoice($order); } // Error Handling if (sizeof($woocommerce_errors) > 0) { update_option('woocommerce_errors', $woocommerce_errors); } }
/** * Prepare Order * * Save the cart session to an order that can be retrieved when customer returns from PayPal. */ function prepare_order() { global $woocommerce; $order_id = ""; if (sizeof(WC()->cart->get_cart()) == 0) { $woocommerce->add_error(sprintf(__('Sorry, your session has expired. <a href="%s">Return to homepage →</a>', 'wc-paypal-express'), home_url())); } if ($woocommerce->cart->needs_shipping()) { // Shipping Method $available_methods = WC()->shipping->get_shipping_methods(); if (!isset($available_methods[$_SESSION['_chosen_shipping_method']])) { $woocommerce->add_error(__('Invalid shipping method.', 'wc-paypal-express'), home_url()); return 0; } } // Create Order ( send cart variable so we can record items and reduce inventory ). // Only create if this is a new order, not if the payment was rejected last time. $order_data = array('post_type' => 'shop_order', 'post_title' => 'Order – ' . date('F j, Y @ h:i A'), 'post_status' => 'publish', 'ping_status' => 'closed', 'post_excerpt' => '', 'post_author' => 1); // Cart items $order_items = array(); foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { $_product = $values['data']; // Store any item meta data - item meta class lets plugins add item meta in a standardized way $item_meta = new order_item_meta(); $item_meta->new_order_item($values); // Store variation data in meta so admin can view it if ($values['variation'] && is_array($values['variation'])) { foreach ($values['variation'] as $key => $value) { $item_meta->add(esc_attr(str_replace('attribute_', '', $key)), $value); } } $order_items[] = apply_filters('new_order_item', array('id' => $values['product_id'], 'variation_id' => $values['variation_id'], 'name' => html_entity_decode($_product->get_title(), ENT_NOQUOTES, 'UTF-8'), 'qty' => (int) $values['quantity'], 'item_meta' => $item_meta->meta, 'line_subtotal' => rtrim(rtrim(number_format($values['line_subtotal'], 4, '.', ''), '0'), '.'), 'line_subtotal_tax' => rtrim(rtrim(number_format($values['line_subtotal_tax'], 4, '.', ''), '0'), '.'), 'line_total' => rtrim(rtrim(number_format($values['line_total'], 4, '.', ''), '0'), '.'), 'line_tax' => rtrim(rtrim(number_format($values['line_tax'], 4, '.', ''), '0'), '.'), 'tax_class' => $_product->get_tax_class()), $values); } // Insert or update the post data $create_new_order = true; if (isset($_SESSION['order_awaiting_payment']) && $_SESSION['order_awaiting_payment'] > 0) { $order_id = (int) $_SESSION['order_awaiting_payment']; /* Check order is unpaid */ $order = new WC_Order($order_id); if ($order->status == 'pending') { // Resume the unpaid order $order_data['ID'] = $order_id; wp_update_post($order_data); do_action('woocommerce_resume_order', $order_id); $create_new_order = false; } } if ($create_new_order) { $order_id = wp_insert_post($order_data); if (is_wp_error($order_id)) { $woocommerce->add_error('Error: Unable to create order. Please try again.'); } else { // Inserted successfully do_action('woocommerce_new_order', $order_id); } } // Get better formatted billing method ( title ) if (isset($_SESSION['_chosen_shipping_method'])) { $shipping_method = $_SESSION['_chosen_shipping_method']; if (isset($available_methods) && isset($available_methods[$_SESSION['_chosen_shipping_method']])) { $shipping_method = $available_methods[$_SESSION['_chosen_shipping_method']]->label; } } // Prepare order taxes for storage $order_taxes = array(); foreach (array_keys($woocommerce->cart->taxes + WC()->cart->shipping_taxes) as $key) { $is_compound = $woocommerce->cart->tax->is_compound($key) ? 1 : 0; $cart_tax = isset($woocommerce->cart->taxes[$key]) ? WC()->cart->taxes[$key] : 0; $shipping_tax = isset($woocommerce->cart->shipping_taxes[$key]) ? WC()->cart->shipping_taxes[$key] : 0; $order_taxes[] = array('label' => $woocommerce->cart->tax->get_rate_label($key), 'compound' => $is_compound, 'cart_tax' => number_format($cart_tax, 2, '.', ''), 'shipping_tax' => number_format($shipping_tax, 2, '.', '')); } // These fields are not returned from PayPal Express update_post_meta($order_id, '_billing_company', ""); update_post_meta($order_id, '_billing_address_1', ""); update_post_meta($order_id, '_billing_address_2', ""); update_post_meta($order_id, '_billing_city', ""); update_post_meta($order_id, '_billing_postcode', ""); update_post_meta($order_id, '_billing_country', ""); update_post_meta($order_id, '_billing_state', ""); update_post_meta($order_id, '_billing_email', ""); update_post_meta($order_id, '_billing_phone', ""); if (isset($_SESSION['_chosen_shipping_method'])) { update_post_meta($order_id, '_shipping_method', $_SESSION['_chosen_shipping_method']); } update_post_meta($order_id, '_payment_method', $this->id); update_post_meta($order_id, '_shipping_method_title', $shipping_method); update_post_meta($order_id, '_payment_method_title', $this->title); update_post_meta($order_id, '_order_shipping', number_format($woocommerce->cart->shipping_total, 2, '.', '')); update_post_meta($order_id, '_order_discount', number_format($woocommerce->cart->get_order_discount_total(), 2, '.', '')); update_post_meta($order_id, '_cart_discount', number_format($woocommerce->cart->get_cart_discount_total(), 2, '.', '')); update_post_meta($order_id, '_order_tax', number_format($woocommerce->cart->tax_total, 2, '.', '')); update_post_meta($order_id, '_order_shipping_tax', number_format($woocommerce->cart->shipping_tax_total, 2, '.', '')); update_post_meta($order_id, '_order_total', number_format($woocommerce->cart->total, 2, '.', '')); update_post_meta($order_id, '_order_key', apply_filters('woocommerce_generate_order_key', uniqid('order_'))); update_post_meta($order_id, '_customer_user', (int) get_current_user_id()); update_post_meta($order_id, '_order_items', $order_items); update_post_meta($order_id, '_order_taxes', $order_taxes); update_post_meta($order_id, '_order_currency', get_option('woocommerce_currency')); update_post_meta($order_id, '_prices_include_tax', get_option('woocommerce_prices_include_tax')); // Order status wp_set_object_terms($order_id, 'pending', 'shop_order_status'); // Discount code meta if ($applied_coupons = $woocommerce->cart->get_applied_coupons()) { update_post_meta($order_id, 'coupons', implode(', ', $applied_coupons)); } return $order_id; }