function _add_booking_order_meta($item_id, $cart_data, $cart_item_key) { if (isset($cart_data['st_booking_data']) and !empty($cart_data['st_booking_data'])) { $st_booking_data = $cart_data['st_booking_data']; foreach ($st_booking_data as $key => $value) { wc_add_order_item_meta($item_id, '_st_' . $key, $value); } } }
public function order_item_meta($item_id, $values, $cart_item_key) { // wc_add_order_item_meta( $item_id, "gift_wrap", 'Yes' ); if (isset($values["wpb_cart_items"])) { wc_add_order_item_meta($item_id, "wpb_cart_items", $values["wpb_cart_items"]); } }
function set($item_id, $values, $cart_item_key) { global $wpdb; global $DOPBSP; $reservation_data = $wpdb->get_row('SELECT * FROM ' . $DOPBSP->tables->woocommerce . ' WHERE variation_id=' . wc_get_order_item_meta($item_id, '_variation_id')); if ($reservation_data) { $reservation = json_decode($reservation_data->data); $reservation->currency = $reservation_data->currency; $reservation->item_id = $item_id; $calendar = $wpdb->get_row('SELECT * FROM ' . $DOPBSP->tables->calendars . ' WHERE id=' . $reservation_data->calendar_id); $settings = $wpdb->get_row('SELECT * FROM ' . $DOPBSP->tables->settings . ' WHERE calendar_id=' . $reservation_data->calendar_id); $DOPBSP->classes->translation->setTranslation($reservation_data->language, false); /* * Display details data. */ wc_add_order_item_meta($item_id, $DOPBSP->text('RESERVATIONS_RESERVATION_DETAILS_TITLE'), $this->getDetails($reservation, $calendar, $settings)); /* * Display extra data. */ if ($settings->extra != 0) { wc_add_order_item_meta($item_id, $DOPBSP->text('EXTRAS_FRONT_END_TITLE'), $this->getExtras($reservation, $settings)); } /* * Display discount data. */ if ($settings->discount != 0) { wc_add_order_item_meta($item_id, $DOPBSP->text('DISCOUNTS_FRONT_END_TITLE'), $this->getDiscount($reservation, $settings)); } } }
/** * Add bundle info meta to order items. * * @param int $item_id order item id * @param array $cart_item_values cart item data * @return void */ public function add_order_item_meta($item_id, $cart_item_values, $cart_item_key) { // add data to the product if (isset($cart_item_values['wc-boilerplate-extension-number'])) { wc_add_order_item_meta($item_id, '_wc-boilerplate-extension-number', $cart_item_values['wc-boilerplate-extension-number']); } if (isset($cart_item_values['wc-boilerplate-extension-textbox'])) { wc_add_order_item_meta($item_id, '_wc-boilerplate-extension-textbox', $cart_item_values['wc-boilerplate-extension-textbox']); } }
/** * * Adds booked dates to the order item * * @param int $item_id * @param array $values - * **/ public function easy_booking_add_order_meta($item_id, $values) { $start_text = !empty($this->options['easy_booking_start_date_text']) ? $this->options['easy_booking_start_date_text'] : __('Start', 'easy_booking'); $end_text = !empty($this->options['easy_booking_end_date_text']) ? $this->options['easy_booking_end_date_text'] : __('End', 'easy_booking'); if (!empty($values['_start_date'])) { wc_add_order_item_meta($item_id, $start_text, $values['_start_date']); wc_add_order_item_meta($item_id, '_ebs_start_display', $values['_start_date']); } if (!empty($values['_end_date'])) { wc_add_order_item_meta($item_id, $end_text, $values['_end_date']); wc_add_order_item_meta($item_id, '_ebs_end_display', $values['_end_date']); } if (!empty($values['_ebs_start'])) { wc_add_order_item_meta($item_id, '_ebs_start_format', $values['_ebs_start']); } if (!empty($values['_ebs_end'])) { wc_add_order_item_meta($item_id, '_ebs_end_format', $values['_ebs_end']); } }
public function order_item_meta($item_id, $cart_item) { if (isset($cart_item['wbm_product_cart']) && isset($cart_item['wbm_product_price']) && isset($cart_item['wbm_product_attributes'])) { $wbm_cart_array = $cart_item['wbm_product_attributes']["main"]; $mainAttr = $wbm_cart_array["mainattr"]; unset($wbm_cart_array["mainattr"]); $html = ''; foreach ($wbm_cart_array as $key => $value) { $html .= ' <strong>' . $value[0] . '</strong><br/>'; if (isset($value["selectbox"]) && !empty($value["selectbox"])) { foreach ($value["selectbox"] as $k => $v) { $html .= ' ' . $v . '<br/>'; } } if (isset($value["dates"]) && !empty($value["dates"])) { foreach ($value["dates"] as $m => $n) { $html .= ' ' . $n . '<br/>'; } } } wc_add_order_item_meta($item_id, $mainAttr, $html); } }
public static function add_vendor_to_order_item_meta($item_id, $cart_item) { $vendor_id = $cart_item['data']->post->post_author; $sold_by_label = WC_Vendors::$pv_options->get_option('sold_by_label'); $sold_by = WCV_Vendors::is_vendor($vendor_id) ? sprintf(WCV_Vendors::get_vendor_sold_by($vendor_id)) : get_bloginfo('name'); wc_add_order_item_meta($item_id, apply_filters('wcvendors_sold_by_in_email', $sold_by_label), $sold_by); }
/** * Adds product description to order meta * * @param int $order_id * @param int $item_id * @param object $product * @param int $qty * @param array $args */ public function set_order_meta($order_id, $item_id, $product, $qty, $args) { wc_add_order_item_meta($item_id, '_units', wc_gzd_get_gzd_product($product)->get_product_units_html()); wc_add_order_item_meta($item_id, '_delivery_time', wc_gzd_get_gzd_product($product)->get_delivery_time_html()); wc_add_order_item_meta($item_id, '_item_desc', wc_gzd_get_gzd_product($product)->get_mini_desc()); wc_add_order_item_meta($item_id, '_unit_price', wc_gzd_get_gzd_product($product)->get_unit_html(false)); }
/** * add_product_input_fields_to_order_item_meta. */ public function add_product_input_fields_to_order_item_meta($item_id, $values, $cart_item_key) { $total_number = apply_filters('wcj_get_option_filter', 1, $this->get_value('wcj_' . 'product_input_fields' . '_' . $this->scope . '_total_number', $values['product_id'], 1)); for ($i = 1; $i <= $total_number; $i++) { if (array_key_exists('wcj_product_input_fields_' . $this->scope . '_' . $i, $values)) { $type = $this->get_value('wcj_product_input_fields_type_' . $this->scope . '_' . $i, $values['product_id'], ''); $input_field_value = $values['wcj_product_input_fields_' . $this->scope . '_' . $i]; if ('file' === $type) { $tmp_name = $input_field_value['tmp_name']; $ext = pathinfo($input_field_value['name'], PATHINFO_EXTENSION); $name = $item_id . '.' . $ext; //$input_field_value['name']; $upload_dir = wcj_get_wcj_uploads_dir('input_fields_uploads'); if (!file_exists($upload_dir)) { mkdir($upload_dir, 0755, true); } //$upload_dir = ( wp_mkdir_p( $upload_dir['path'] ) ) ? $upload_dir['path'] : $upload_dir['basedir']; $upload_dir_and_name = $upload_dir . '/' . $name; //move_uploaded_file( $tmp_name, $upload_dir_and_name ); $file_data = file_get_contents($tmp_name); file_put_contents($upload_dir_and_name, $file_data); unlink($tmp_name); //unset( $input_field_value['tmp_name'] ); $input_field_value['tmp_name'] = $upload_dir_and_name; $input_field_value['wcj_type'] = 'file'; //$orig_file_name = $input_field_value['name']; //wc_add_order_item_meta( $item_id, '_wcj_product_input_fields_' . $this->scope . '_' . $i . '_orig_file_name', $orig_file_name ); //$input_field_value = '<a href="' . add_query_arg( 'wcj_download_file', $name ) . '">' . $orig_file_name . '</a>'; //$input_field_value = $orig_file_name; } wc_add_order_item_meta($item_id, '_wcj_product_input_fields_' . $this->scope . '_' . $i, $input_field_value); } } }
/** * Create a new vendor order programmatically * * Returns a new vendor_order object on success which can then be used to add additional data. * * @since * @param array $args * @return WC_Order_Vendor|WP_Error */ public static function create_vendor_order($args = array()) { $default_args = array('vendor_id' => null, 'order_id' => 0, 'vendor_order_id' => 0, 'line_items' => array(), 'date' => current_time('mysql', 0)); $args = wp_parse_args($args, $default_args); $vendor_order_data = array(); if ($args['vendor_order_id'] > 0) { $updating = true; $vendor_order_data['ID'] = $args['vendor_order_id']; } else { $updating = false; $vendor_order_data['post_type'] = 'shop_order_vendor'; $vendor_order_data['post_status'] = 'wc-completed'; $vendor_order_data['ping_status'] = 'closed'; $vendor_order_data['post_author'] = get_current_user_id(); $vendor_order_data['post_password'] = uniqid('vendor_'); // password = 20 char max! (uniqid = 13) $vendor_order_data['post_parent'] = absint($args['order_id']); $vendor_order_data['post_title'] = sprintf(__('Vendor Order – %s', 'woocommerce'), strftime(_x('%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce'))); $vendor_order_data['post_date'] = $args['date']; } if ($updating) { $vendor_order_id = wp_update_post($vendor_order_data); } else { $vendor_order_id = wp_insert_post(apply_filters('woocommerce_new_vendor_order_data', $vendor_order_data), true); } if (is_wp_error($vendor_order_id)) { return $vendor_order_id; } if (!$updating) { // Get vendor order object $vendor_order = wc_get_order($vendor_order_id); $order = wc_get_order($args['order_id']); // Order currency is the same used for the parent order update_post_meta($vendor_order_id, '_order_currency', $order->get_order_currency()); if (sizeof($args['line_items']) > 0) { $order_items = $order->get_items(array('line_item', 'fee', 'shipping')); foreach ($args['line_items'] as $vendor_order_item_id => $vendor_order_item) { if (isset($order_items[$vendor_order_item_id])) { if (empty($vendor_order_item['qty']) && empty($vendor_order_item['total']) && empty($vendor_order_item['tax'])) { continue; } // Prevents errors when the order has no taxes if (!isset($vendor_order_item['tax'])) { $vendor_order_item['tax'] = array(); } switch ($order_items[$vendor_order_item_id]['type']) { case 'line_item': $line_item_args = array('totals' => array('subtotal' => $vendor_order_item['subtotal'], 'total' => $vendor_order_item['total'], 'subtotal_tax' => $vendor_order_item['subtotal_tax'], 'tax' => $vendor_order_item['tax'], 'tax_data' => $vendor_order_item['tax_data'])); $new_item_id = $vendor_order->add_product($order->get_product_from_item($order_items[$vendor_order_item_id]), isset($vendor_order_item['qty']) ? $vendor_order_item['qty'] : 0, $line_item_args); wc_add_order_item_meta($new_item_id, '_vendor_order_item_id', $vendor_order_item_id); wc_add_order_item_meta($new_item_id, '_vendor_commission', $vendor_order_item['commission']); break; case 'shipping': $shipping = new stdClass(); $shipping->label = $order_items[$vendor_order_item_id]['name']; $shipping->id = $order_items[$vendor_order_item_id]['method_id']; $shipping->cost = $vendor_order_item['total']; $shipping->taxes = $vendor_order_item['tax']; $new_item_id = $vendor_order->add_shipping($shipping); wc_add_order_item_meta($new_item_id, '_vendor_order_item_id', $vendor_order_item_id); break; case 'fee': $fee = new stdClass(); $fee->name = $order_items[$vendor_order_item_id]['name']; $fee->tax_class = $order_items[$vendor_order_item_id]['tax_class']; $fee->taxable = $fee->tax_class !== '0'; $fee->amount = $vendor_order_item['total']; $fee->tax = array_sum($vendor_order_item['tax']); $fee->tax_data = $vendor_order_item['tax']; $new_item_id = $vendor_order->add_fee($fee); wc_add_order_item_meta($new_item_id, '_vendor_order_item_id', $vendor_order_item_id); break; } } } $vendor_order->update_taxes(); } $vendor_order->calculate_totals(false); do_action('woocommerce_vendor_order_created', $vendor_order_id, $args); } // Clear transients wc_delete_shop_order_transients($args['order_id']); return new WC_Order_Vendor($vendor_order_id); }
function save_customized_item_meta($item_id, $values, $cart_item_key) { $variation_id = $values["variation_id"]; if (isset($values["output"])) { if (isset($_SESSION["wpc_generated_data"][$variation_id][$cart_item_key])) { wc_add_order_item_meta($item_id, 'wpc_data', $_SESSION["wpc_generated_data"][$variation_id][$cart_item_key]); unset($_SESSION["wpc_generated_data"][$variation_id][$cart_item_key]); } if (empty($_SESSION["wpc_generated_data"][$variation_id])) { unset($_SESSION["wpc_generated_data"][$variation_id]); } } else { if (isset($_SESSION["wpc-uploaded-designs"][$cart_item_key])) { wc_add_order_item_meta($item_id, 'wpc_data_upl', $_SESSION["wpc-uploaded-designs"][$cart_item_key]); unset($_SESSION["wpc-uploaded-designs"][$cart_item_key]); } } if (isset($_SESSION["wpc_design_pricing_options"][$cart_item_key]) && !empty($_SESSION["wpc_design_pricing_options"][$cart_item_key])) { $wpc_design_pricing_options_data = $this->get_design_pricing_options_data($_SESSION["wpc_design_pricing_options"][$cart_item_key]); wc_add_order_item_meta($item_id, '_wpc_design_pricing_options', $wpc_design_pricing_options_data); unset($_SESSION["wpc_design_pricing_options"][$cart_item_key]); } }
// Do the same kind of update for order_taxes - move to lines // Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_taxes' WHERE meta_key = '_order_taxes_old' $order_tax_rows = $wpdb->get_results("\n\tSELECT * FROM {$wpdb->postmeta}\n\tWHERE meta_key = '_order_taxes'\n"); foreach ($order_tax_rows as $order_tax_row) { $order_taxes = (array) maybe_unserialize($order_tax_row->meta_value); if ($order_taxes) { foreach ($order_taxes as $order_tax) { if (!isset($order_tax['label']) || !isset($order_tax['cart_tax']) || !isset($order_tax['shipping_tax'])) { continue; } $item_id = wc_add_order_item($order_tax_row->post_id, array('order_item_name' => $order_tax['label'], 'order_item_type' => 'tax')); // Add line item meta if ($item_id) { wc_add_order_item_meta($item_id, 'compound', absint(isset($order_tax['compound']) ? $order_tax['compound'] : 0)); wc_add_order_item_meta($item_id, 'tax_amount', wc_clean($order_tax['cart_tax'])); wc_add_order_item_meta($item_id, 'shipping_tax_amount', wc_clean($order_tax['shipping_tax'])); } // Delete from DB (rename) $wpdb->query($wpdb->prepare("\n\t\t\t\tUPDATE {$wpdb->postmeta}\n\t\t\t\tSET meta_key = '_order_taxes_old'\n\t\t\t\tWHERE meta_key = '_order_taxes'\n\t\t\t\tAND post_id = %d\n\t\t\t", $order_tax_row->post_id)); unset($tax_amount); } } } // Grab the pre 2.0 Image options and use to populate the new image options settings, // cleaning up afterwards like nice people do foreach (array('catalog', 'single', 'thumbnail') as $value) { $old_settings = array_filter(array('width' => get_option('woocommerce_' . $value . '_image_width'), 'height' => get_option('woocommerce_' . $value . '_image_height'), 'crop' => get_option('woocommerce_' . $value . '_image_crop'))); if (!empty($old_settings) && update_option('shop_' . $value . '_image_size', $old_settings)) { delete_option('woocommerce_' . $value . '_image_width'); delete_option('woocommerce_' . $value . '_image_height'); delete_option('woocommerce_' . $value . '_image_crop');
/** * Create shipping for a sub-order if neccessary * * @param WC_Order $parent_order * @param int $order_id * @param array $product_ids * @return type */ function dokan_create_sub_order_shipping($parent_order, $order_id, $seller_products) { // take only the first shipping method $shipping_methods = $parent_order->get_shipping_methods(); $shipping_method = is_array($shipping_methods) ? reset($shipping_methods) : array(); // bail out if no shipping methods found if (!$shipping_method) { return; } $shipping_products = array(); $packages = array(); // emulate shopping cart for calculating the shipping method foreach ($seller_products as $product_item) { $product = get_product($product_item['product_id']); if ($product->needs_shipping()) { $shipping_products[] = array('product_id' => $product_item['product_id'], 'variation_id' => $product_item['variation_id'], 'variation' => '', 'quantity' => $product_item['qty'], 'data' => $product, 'line_total' => $product_item['line_total'], 'line_tax' => $product_item['line_tax'], 'line_subtotal' => $product_item['line_subtotal'], 'line_subtotal_tax' => $product_item['line_subtotal_tax']); } } if ($shipping_products) { $package = array('contents' => $shipping_products, 'contents_cost' => array_sum(wp_list_pluck($shipping_products, 'line_total')), 'applied_coupons' => array(), 'destination' => array('country' => $parent_order->shipping_country, 'state' => $parent_order->shipping_state, 'postcode' => $parent_order->shipping_postcode, 'city' => $parent_order->shipping_city, 'address' => $parent_order->shipping_address_1, 'address_2' => $parent_order->shipping_address_2)); $wc_shipping = WC_Shipping::instance(); $pack = $wc_shipping->calculate_shipping_for_package($package); if (array_key_exists($shipping_method['method_id'], $pack['rates'])) { $method = $pack['rates'][$shipping_method['method_id']]; $cost = wc_format_decimal($method->cost); $item_id = wc_add_order_item($order_id, array('order_item_name' => $method->label, 'order_item_type' => 'shipping')); if ($item_id) { wc_add_order_item_meta($item_id, 'method_id', $method->id); wc_add_order_item_meta($item_id, 'cost', $cost); } return $cost; } } return 0; }
/** * The '_switched_subscription_key' and '_switched_subscription_new_order' post meta values are no longer used to relate orders * and switched subscriptions, instead, we need to set a '_subscription_switch' value on the switch order and depreacted the old * meta keys by prefixing them with '_wcs_migrated'. * * Subscriptions also sets a '_switched_subscription_item_id' value on the new line item of for the switched item and a item meta * value of '_switched_subscription_new_item_id' on the old line item on the subscription, but the old switching process didn't * change order items, it just created a new order with the new item, so we won't bother setting this as it is purely for record * keeping. * * @param WC_Subscription $new_subscription A subscription object * @param WC_Order $switch_order The original order used to purchase the subscription * @param int $subscription_item_id The order item ID of the item added to the subscription by self::add_product() * @return null * @since 2.0 */ private static function migrate_switch_meta($new_subscription, $switch_order, $subscription_item_id) { global $wpdb; // If the order doesn't contain a switch, we don't need to do anything if ('' == get_post_meta($switch_order->id, '_switched_subscription_key', true)) { return; } $wpdb->query($wpdb->prepare("UPDATE {$wpdb->postmeta} SET `meta_key` = concat( '_wcs_migrated', `meta_key` )\n\t\t\tWHERE `post_id` = %d AND `meta_key` IN ('_switched_subscription_first_payment_timestamp','_switched_subscription_key')", $switch_order->id)); // Select the orders which had the items which were switched by this order $previous_order_id = get_posts(array('post_type' => 'shop_order', 'post_status' => 'any', 'fields' => 'ids', 'posts_per_page' => -1, 'meta_query' => array(array('key' => '_switched_subscription_new_order', 'value' => $switch_order->id)))); if (!empty($previous_order_id)) { $previous_order_id = $previous_order_id[0]; $wpdb->query($wpdb->prepare("UPDATE {$wpdb->postmeta} SET `meta_key` = concat( '_wcs_migrated', `meta_key` )\n\t\t\t\tWHERE `post_id` = %d AND `meta_key` = '_switched_subscription_new_order'", $previous_order_id)); // Because self::get_subscriptions() orders by order ID, it's safe to use wcs_get_subscriptions_for_order() here because the subscription in the new format will have been created for the original order (because its ID will be < the switch order's ID) $old_subscriptions = wcs_get_subscriptions_for_order($previous_order_id); $old_subscription = array_shift($old_subscriptions); // there can be only one if (wcs_is_subscription($old_subscription)) { // Link the old subscription's ID to the switch order using the new switch meta key update_post_meta($switch_order->id, '_subscription_switch', $old_subscription->id); // Now store the new/old item IDs for record keeping foreach ($old_subscription->get_items() as $item_id => $item) { wc_add_order_item_meta($item_id, '_switched_subscription_new_item_id', $subscription_item_id, true); wc_add_order_item_meta($subscription_item_id, '_switched_subscription_item_id', $item_id, true); } WCS_Upgrade_Logger::add(sprintf('For subscription %d: migrated switch data for subscription %d purchased in order %d', $new_subscription->id, $old_subscription->id, $previous_order_id)); } } }
/** * Add a fee to the order * * @param object $fee * @return int|bool Item ID or false */ public function add_fee($fee) { $item_id = wc_add_order_item($this->id, array('order_item_name' => $fee->name, 'order_item_type' => 'fee')); if (!$item_id) { return false; } if ($fee->taxable) { wc_add_order_item_meta($item_id, '_tax_class', $fee->tax_class); } else { wc_add_order_item_meta($item_id, '_tax_class', '0'); } wc_add_order_item_meta($item_id, '_line_total', wc_format_decimal($fee->amount)); wc_add_order_item_meta($item_id, '_line_tax', wc_format_decimal($fee->tax)); // Save tax data - Since 2.2 $tax_data = array_map('wc_format_decimal', $fee->tax_data); wc_add_order_item_meta($item_id, '_line_tax_data', array('total' => $tax_data)); do_action('woocommerce_order_add_fee', $this->id, $item_id, $fee); return $item_id; }
function wdm_add_values_to_order_item_meta($item_id, $values, $key ) { global $woocommerce,$wpdb; if ( isset( $values['cierre'] ) && !empty( $values['cierre']) ) wc_add_order_item_meta( $item_id ,'cierre', $values['cierre'] ); if ( isset( $values['rango'] ) && !empty( $values['rango']) ) wc_add_order_item_meta( $item_id ,'rango', $values['rango'] ); if ( isset( $values['id_modelo'] ) && !empty( $values['id_modelo']) ) wc_add_order_item_meta( $item_id ,'id_modelo', $values['id_modelo'] ); if ( isset( $values['modelo'] ) && !empty( $values['modelo']) ) wc_add_order_item_meta( $item_id ,'modelo', $values['modelo'] ); if ( isset( $values['configurations'] ) && !empty( $values['configurations']) ) wc_add_order_item_meta( $item_id ,'configurations', $values['configurations'] ); }
/** * Creates a sub order * * @param int $parent_order * @param int $seller_id * @param array $seller_products */ function dokan_create_seller_order($parent_order, $seller_id, $seller_products) { $order_data = apply_filters('woocommerce_new_order_data', array('post_type' => 'shop_order', 'post_title' => sprintf(__('Order – %s', 'woocommerce'), strftime(_x('%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce'))), 'post_status' => 'wc-pending', 'ping_status' => 'closed', 'post_excerpt' => isset($posted['order_comments']) ? $posted['order_comments'] : '', 'post_author' => $seller_id, 'post_parent' => $parent_order->id, 'post_password' => uniqid('order_'))); $order_id = wp_insert_post($order_data); if ($order_id && !is_wp_error($order_id)) { $order_total = $order_tax = 0; $product_ids = array(); do_action('woocommerce_new_order', $order_id); // now insert line items foreach ($seller_products as $item) { $order_total += (double) $item['line_total']; $order_tax += (double) $item['line_tax']; $product_ids[] = $item['product_id']; $item_id = wc_add_order_item($order_id, array('order_item_name' => $item['name'], 'order_item_type' => 'line_item')); if ($item_id) { wc_add_order_item_meta($item_id, '_qty', $item['qty']); wc_add_order_item_meta($item_id, '_tax_class', $item['tax_class']); wc_add_order_item_meta($item_id, '_product_id', $item['product_id']); wc_add_order_item_meta($item_id, '_line_subtotal', $item['line_subtotal']); wc_add_order_item_meta($item_id, '_line_total', $item['line_total']); wc_add_order_item_meta($item_id, '_line_tax', $item['line_tax']); wc_add_order_item_meta($item_id, '_line_subtotal_tax', $item['line_subtotal_tax']); } } // foreach $bill_ship = array('_billing_country', '_billing_first_name', '_billing_last_name', '_billing_company', '_billing_address_1', '_billing_address_2', '_billing_city', '_billing_state', '_billing_postcode', '_billing_email', '_billing_phone', '_shipping_country', '_shipping_first_name', '_shipping_last_name', '_shipping_company', '_shipping_address_1', '_shipping_address_2', '_shipping_city', '_shipping_state', '_shipping_postcode'); // save billing and shipping address foreach ($bill_ship as $val) { $order_key = ltrim($val, '_'); update_post_meta($order_id, $val, $parent_order->{$order_key}); } // calculate the total $order_in_total = $order_total + $shipping_cost + $order_tax; // set order meta update_post_meta($order_id, '_payment_method', $parent_order->payment_method); update_post_meta($order_id, '_payment_method_title', $parent_order->payment_method_title); update_post_meta($order_id, '_order_shipping', woocommerce_format_decimal($shipping_cost)); update_post_meta($order_id, '_cart_discount', '0'); update_post_meta($order_id, '_order_tax', woocommerce_format_decimal($order_tax)); update_post_meta($order_id, '_order_shipping_tax', '0'); update_post_meta($order_id, '_order_total', woocommerce_format_decimal($order_in_total)); update_post_meta($order_id, '_order_key', apply_filters('woocommerce_generate_order_key', uniqid('order_'))); update_post_meta($order_id, '_customer_user', $parent_order->customer_user); update_post_meta($order_id, '_order_currency', get_post_meta($parent_order->id, '_order_currency', true)); update_post_meta($order_id, '_prices_include_tax', $parent_order->prices_include_tax); update_post_meta($order_id, '_customer_ip_address', get_post_meta($parent_order->id, '_customer_ip_address', true)); update_post_meta($order_id, '_customer_user_agent', get_post_meta($parent_order->id, '_customer_user_agent', true)); do_action('dokan_checkout_update_order_meta', $order_id, $seller_id); } // if order }
/** * WCMp Calculate shipping for order * * @support flat rate per item * @param int $order_id * @param object $order_posted * @return void */ function wcmp_checkout_order_processed($order_id, $order_posted) { global $wpdb, $WCMp; $order = new WC_Order($order_id); if ($order->has_shipping_method('flat_rate')) { $woocommerce_flat_rate_settings = get_option('woocommerce_flat_rate_settings'); $line_items = $order->get_items('line_item'); if ($woocommerce_flat_rate_settings['enabled'] == 'yes') { if (version_compare(WC_VERSION, '2.5.0', '>=')) { if ($woocommerce_flat_rate_settings['type'] == 'class') { if (!empty($line_items)) { foreach ($line_items as $item_id => $item) { $wc_flat_rate = new WC_Shipping_Flat_Rate(); $product = $order->get_product_from_item($item); $shipping_class = $product->get_shipping_class_id(); $class_cost_string = $shipping_class ? $wc_flat_rate->get_option('class_cost_' . $shipping_class, '') : $wc_flat_rate->get_option('no_class_cost', ''); $cost_item_id = $this->evaluate_flat_shipping_cost($class_cost_string, array('qty' => $item['qty'], 'cost' => $order->get_line_subtotal($item))); $flat_shipping_per_item_val = wc_get_order_item_meta($item_id, 'flat_shipping_per_item', true); if (!$flat_shipping_per_item_val) { wc_add_order_item_meta($item_id, 'flat_shipping_per_item', round($cost_item_id, 2)); } } } } } else { if (version_compare(WC_VERSION, '2.4.0', '>')) { if ($woocommerce_flat_rate_settings['type'] == 'class') { if (!empty($line_items)) { foreach ($line_items as $item_id => $item) { $wc_flat_rate = new WC_Shipping_Flat_Rate(); $product = $order->get_product_from_item($item); $shipping_class = $product->get_shipping_class(); $class_cost_string = $shipping_class ? $wc_flat_rate->get_option('class_cost_' . $shipping_class, '') : $wc_flat_rate->get_option('no_class_cost', ''); $cost_item_id = $this->evaluate_flat_shipping_cost($class_cost_string, array('qty' => $item['qty'], 'cost' => $order->get_line_subtotal($item))); $flat_shipping_per_item_val = wc_get_order_item_meta($item_id, 'flat_shipping_per_item', true); if (!$flat_shipping_per_item_val) { wc_add_order_item_meta($item_id, 'flat_shipping_per_item', round($cost_item_id, 2)); } } } } } else { $woocommerce_flat_rate_settings_cost = $woocommerce_flat_rate_settings['cost']; $woocommerce_flat_rate_settings_fee = $woocommerce_flat_rate_settings['fee']; $woocommerce_flat_rates = get_option('woocommerce_flat_rates'); if ($woocommerce_flat_rate_settings['type'] == 'item') { if (!empty($line_items)) { foreach ($line_items as $item_id => $item) { $fee = $cost = 0; $_product = $order->get_product_from_item($item); $shipping_class = $_product->get_shipping_class(); if (isset($woocommerce_flat_rates[$shipping_class])) { $cost = $woocommerce_flat_rates[$shipping_class]['cost']; $fee = $this->get_fee($woocommerce_flat_rates[$shipping_class]['fee'], $_product->get_price()); } elseif ($woocommerce_flat_rate_settings_cost !== '') { $cost = $woocommerce_flat_rate_settings_cost; $fee = $this->get_fee($woocommerce_flat_rate_settings_fee, $_product->get_price()); $matched = true; } $cost_item_id = ($cost + $fee) * $item['qty']; $flat_shipping_per_item_val = wc_get_order_item_meta($item_id, 'flat_shipping_per_item', true); if (!$flat_shipping_per_item_val) { wc_add_order_item_meta($item_id, 'flat_shipping_per_item', round($cost_item_id, 2)); } } } } } } } } if ($order->has_shipping_method('international_delivery')) { $woocommerce_international_delivery_settings = get_option('woocommerce_international_delivery_settings'); $line_items = $order->get_items('line_item'); if ($woocommerce_international_delivery_settings['enabled'] == 'yes') { if (version_compare(WC_VERSION, '2.5.0', '>=')) { if ($woocommerce_international_delivery_settings['type'] == 'class') { if (!empty($line_items)) { $item_id = false; foreach ($line_items as $item_id => $item) { $wc_international_flat_rate = new WC_Shipping_International_Delivery(); $product = $order->get_product_from_item($item); $shipping_class = $product->get_shipping_class_id(); $class_cost_string = $shipping_class ? $wc_international_flat_rate->get_option('class_cost_' . $shipping_class, '') : $wc_international_flat_rate->get_option('no_class_cost', ''); $cost_item_id = $this->evaluate_flat_shipping_cost($class_cost_string, array('qty' => $item['qty'], 'cost' => $order->get_line_subtotal($item))); $international_flat_shipping_per_item_val = wc_get_order_item_meta($item_id, 'international_flat_shipping_per_item', true); if (!$international_flat_shipping_per_item_val) { wc_add_order_item_meta($item_id, 'international_flat_shipping_per_item', $cost_item_id); } } } } } else { if (version_compare(WC_VERSION, '2.4.0', '>')) { if ($woocommerce_international_delivery_settings['type'] == 'class') { if (!empty($line_items)) { $item_id = false; foreach ($line_items as $item_id => $item) { $wc_international_flat_rate = new WC_Shipping_International_Delivery(); $product = $order->get_product_from_item($item); $shipping_class = $product->get_shipping_class(); $class_cost_string = $shipping_class ? $wc_international_flat_rate->get_option('class_cost_' . $shipping_class, '') : $wc_international_flat_rate->get_option('no_class_cost', ''); $cost_item_id = $this->evaluate_flat_shipping_cost($class_cost_string, array('qty' => $item['qty'], 'cost' => $order->get_line_subtotal($item))); $international_flat_shipping_per_item_val = wc_get_order_item_meta($item_id, 'international_flat_shipping_per_item', true); if (!$international_flat_shipping_per_item_val) { wc_add_order_item_meta($item_id, 'international_flat_shipping_per_item', $cost_item_id); } } } } } } } } $vendor_shipping_array = get_post_meta($order_id, 'dc_pv_shipped', true); $order = new WC_Order($order_id); $commission_array = array(); $mark_ship = 0; $items = $order->get_items('line_item'); foreach ($items as $order_item_id => $item) { $comm_pro_id = $product_id = $item['product_id']; $variation_id = $item['variation_id']; if ($variation_id) { $comm_pro_id = $variation_id; } if ($product_id) { $product_vendors = get_wcmp_product_vendors($product_id); if ($product_vendors) { if (isset($product_vendors->id) && is_array($vendor_shipping_array)) { if (in_array($product_vendors->id, $vendor_shipping_array)) { $mark_ship = 1; } } $insert_query = $wpdb->query($wpdb->prepare("INSERT INTO `{$wpdb->prefix}wcmp_vendor_orders` ( order_id, commission_id, vendor_id, shipping_status, order_item_id, product_id )\n\t\t\t\t\t\t\t\t\t\t\t\t\t VALUES\n\t\t\t\t\t\t\t\t\t\t\t\t\t ( %d, %d, %d, %s, %d, %d ) ON DUPLICATE KEY UPDATE `created` = now()", $order_id, 0, $product_vendors->id, $mark_ship, $order_item_id, $comm_pro_id)); } } } }
/** * Add some meta to a line item */ public static function add_order_item_meta() { check_ajax_referer('order-item', 'security'); if (!current_user_can('edit_shop_orders')) { die(-1); } $meta_id = wc_add_order_item_meta(absint($_POST['order_item_id']), __('Name', 'woocommerce'), __('Value', 'woocommerce')); if ($meta_id) { echo '<tr data-meta_id="' . esc_attr($meta_id) . '"><td><input type="text" name="meta_key[' . $meta_id . ']" /><textarea name="meta_value[' . $meta_id . ']"></textarea></td><td width="1%"><button class="remove_order_item_meta button">×</button></td></tr>'; } die; }
/** * Create a new order refund programmatically * * Returns a new refund object on success which can then be used to add additional data. * * @since 2.2 * @param array $args * @return WC_Order_Refund|WP_Error */ function wc_create_refund($args = array()) { $default_args = array('amount' => '', 'reason' => null, 'order_id' => 0, 'refund_id' => 0, 'line_items' => array()); $args = wp_parse_args($args, $default_args); $refund_data = array(); if ($args['refund_id'] > 0) { $updating = true; $refund_data['ID'] = $args['refund_id']; } else { $updating = false; $refund_data['post_type'] = 'shop_order_refund'; $refund_data['post_status'] = 'wc-completed'; $refund_data['ping_status'] = 'closed'; $refund_data['post_author'] = get_current_user_id() ? get_current_user_id() : 1; $refund_data['post_password'] = uniqid('refund_'); $refund_data['post_parent'] = absint($args['order_id']); $refund_data['post_title'] = sprintf(__('Refund – %s', 'woocommerce'), strftime(_x('%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce'))); } if (!is_null($args['reason'])) { $refund_data['post_excerpt'] = $args['reason']; } if ($updating) { $refund_id = wp_update_post($refund_data); } else { $refund_id = wp_insert_post(apply_filters('woocommerce_new_refund_data', $refund_data), true); } if (is_wp_error($refund_id)) { return $refund_id; } if (!$updating) { // Default refund meta data update_post_meta($refund_id, '_refund_amount', wc_format_decimal($args['amount'])); // Get refund object $refund = wc_get_order($refund_id); // Negative line items if (sizeof($args['line_items']) > 0) { $order = wc_get_order($args['order_id']); $order_items = $order->get_items(array('line_item', 'fee', 'shipping')); foreach ($args['line_items'] as $refund_item_id => $refund_item) { if (isset($order_items[$refund_item_id])) { if (empty($refund_item['qty']) && empty($refund_item['refund_total']) && empty($refund_item['refund_tax'])) { continue; } // Prevents errors when the order has no taxes if (!isset($refund_item['refund_tax'])) { $refund_item['refund_tax'] = array(); } switch ($order_items[$refund_item_id]['type']) { case 'line_item': $line_item_args = array('totals' => array('subtotal' => wc_format_refund_total($refund_item['refund_total']), 'total' => wc_format_refund_total($refund_item['refund_total']), 'subtotal_tax' => wc_format_refund_total(array_sum($refund_item['refund_tax'])), 'tax' => wc_format_refund_total(array_sum($refund_item['refund_tax'])), 'tax_data' => array('total' => array_map('wc_format_refund_total', $refund_item['refund_tax']), 'subtotal' => array_map('wc_format_refund_total', $refund_item['refund_tax'])))); $new_item_id = $refund->add_product($order->get_product_from_item($order_items[$refund_item_id]), isset($refund_item['qty']) ? $refund_item['qty'] : 0, $line_item_args); wc_add_order_item_meta($new_item_id, '_refunded_item_id', $refund_item_id); break; case 'shipping': $shipping = new stdClass(); $shipping->label = $order_items[$refund_item_id]['name']; $shipping->id = $order_items[$refund_item_id]['method_id']; $shipping->cost = wc_format_refund_total($refund_item['refund_total']); $shipping->taxes = array_map('wc_format_refund_total', $refund_item['refund_tax']); $new_item_id = $refund->add_shipping($shipping); wc_add_order_item_meta($new_item_id, '_refunded_item_id', $refund_item_id); break; case 'fee': $fee = new stdClass(); $fee->name = $order_items[$refund_item_id]['name']; $fee->tax_class = $order_items[$refund_item_id]['tax_class']; $fee->taxable = $fee->tax_class !== '0'; $fee->amount = wc_format_refund_total($refund_item['refund_total']); $fee->tax = wc_format_refund_total(array_sum($refund_item['refund_tax'])); $fee->tax_data = array_map('wc_format_refund_total', $refund_item['refund_tax']); $new_item_id = $refund->add_fee($fee); wc_add_order_item_meta($new_item_id, '_refunded_item_id', $refund_item_id); break; } } } $refund->update_taxes(); } $refund->calculate_totals(false); // Set total to total refunded which may vary from order items $refund->set_total(wc_format_decimal($args['amount']) * -1, 'total'); do_action('woocommerce_refund_created', $refund_id); } // Clear transients wc_delete_shop_order_transients($args['order_id']); return new WC_Order_Refund($refund_id); }
/** * Calc line tax */ public function calc_line_taxes() { global $wpdb; check_ajax_referer('calc-totals', 'security'); $this->json_headers(); $tax = new WC_Tax(); $taxes = $tax_rows = $item_taxes = $shipping_taxes = array(); $order_id = absint($_POST['order_id']); $order = new WC_Order($order_id); $country = strtoupper(esc_attr($_POST['country'])); $state = strtoupper(esc_attr($_POST['state'])); $postcode = strtoupper(esc_attr($_POST['postcode'])); $city = sanitize_title(esc_attr($_POST['city'])); $items = isset($_POST['items']) ? $_POST['items'] : array(); $shipping = $_POST['shipping']; $item_tax = 0; // Calculate sales tax first if (sizeof($items) > 0) { foreach ($items as $item_id => $item) { $item_id = absint($item_id); $line_subtotal = isset($item['line_subtotal']) ? wc_format_decimal($item['line_subtotal']) : 0; $line_total = wc_format_decimal($item['line_total']); $tax_class = sanitize_text_field($item['tax_class']); $product_id = $order->get_item_meta($item_id, '_product_id', true); if (!$item_id || '0' == $tax_class) { continue; } // Get product details if (get_post_type($product_id) == 'product') { $_product = get_product($product_id); $item_tax_status = $_product->get_tax_status(); } else { $item_tax_status = 'taxable'; } // Only calc if taxable if ('taxable' == $item_tax_status) { $tax_rates = $tax->find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => $tax_class)); $line_subtotal_taxes = $tax->calc_tax($line_subtotal, $tax_rates, false); $line_taxes = $tax->calc_tax($line_total, $tax_rates, false); $line_subtotal_tax = array_sum($line_subtotal_taxes); $line_tax = array_sum($line_taxes); if ($line_subtotal_tax < 0) { $line_subtotal_tax = 0; } if ($line_tax < 0) { $line_tax = 0; } $item_taxes[$item_id] = array('line_subtotal_tax' => wc_format_localized_price($line_subtotal_tax), 'line_tax' => wc_format_localized_price($line_tax)); $item_tax += $line_tax; // Sum the item taxes foreach (array_keys($taxes + $line_taxes) as $key) { $taxes[$key] = (isset($line_taxes[$key]) ? $line_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); } } } } // Now calculate shipping tax $matched_tax_rates = array(); $tax_rates = $tax->find_rates(array('country' => $country, 'state' => $state, 'postcode' => $postcode, 'city' => $city, 'tax_class' => '')); if ($tax_rates) { foreach ($tax_rates as $key => $rate) { if (isset($rate['shipping']) && 'yes' == $rate['shipping']) { $matched_tax_rates[$key] = $rate; } } } $shipping_taxes = $tax->calc_shipping_tax($shipping, $matched_tax_rates); $shipping_tax = $tax->round(array_sum($shipping_taxes)); // Remove old tax rows $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = 'tax' )", $order_id)); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = 'tax'", $order_id)); // Get tax rates $rates = $wpdb->get_results("SELECT tax_rate_id, tax_rate_country, tax_rate_state, tax_rate_name, tax_rate_priority FROM {$wpdb->prefix}woocommerce_tax_rates ORDER BY tax_rate_name"); $tax_codes = array(); foreach ($rates as $rate) { $code = array(); $code[] = $rate->tax_rate_country; $code[] = $rate->tax_rate_state; $code[] = $rate->tax_rate_name ? sanitize_title($rate->tax_rate_name) : 'TAX'; $code[] = absint($rate->tax_rate_priority); $tax_codes[$rate->tax_rate_id] = strtoupper(implode('-', array_filter($code))); } // Now merge to keep tax rows ob_start(); foreach (array_keys($taxes + $shipping_taxes) as $key) { $item = array(); $item['rate_id'] = $key; $item['name'] = $tax_codes[$key]; $item['label'] = $tax->get_rate_label($key); $item['compound'] = $tax->is_compound($key) ? 1 : 0; $item['tax_amount'] = wc_format_decimal(isset($taxes[$key]) ? $taxes[$key] : 0); $item['shipping_tax_amount'] = wc_format_decimal(isset($shipping_taxes[$key]) ? $shipping_taxes[$key] : 0); if (!$item['label']) { $item['label'] = WC()->countries->tax_or_vat(); } // Add line item $item_id = wc_add_order_item($order_id, array('order_item_name' => $item['name'], 'order_item_type' => 'tax')); // Add line item meta if ($item_id) { wc_add_order_item_meta($item_id, 'rate_id', $item['rate_id']); wc_add_order_item_meta($item_id, 'label', $item['label']); wc_add_order_item_meta($item_id, 'compound', $item['compound']); wc_add_order_item_meta($item_id, 'tax_amount', $item['tax_amount']); wc_add_order_item_meta($item_id, 'shipping_tax_amount', $item['shipping_tax_amount']); } include 'admin/post-types/meta-boxes/views/html-order-tax.php'; } $tax_row_html = ob_get_clean(); // Return echo json_encode(array('item_tax' => $item_tax, 'item_taxes' => $item_taxes, 'shipping_tax' => $shipping_tax, 'tax_row_html' => $tax_row_html)); // Quit out die; }
/** * create_order function. * @access public * @throws Exception * @return int */ public function create_order() { global $wpdb; // Give plugins the opportunity to create an order themselves $order_id = apply_filters('woocommerce_create_order', null, $this); if (is_numeric($order_id)) { return $order_id; } // 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. $order_data = apply_filters('woocommerce_new_order_data', array('post_type' => 'shop_order', 'post_title' => sprintf(__('Order – %s', 'woocommerce'), strftime(_x('%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce'))), 'post_status' => 'publish', 'ping_status' => 'closed', 'post_excerpt' => isset($this->posted['order_comments']) ? $this->posted['order_comments'] : '', 'post_author' => 1, 'post_password' => uniqid('order_'))); // Insert or update the post data $create_new_order = true; if (WC()->session->order_awaiting_payment > 0) { $order_id = absint(WC()->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'; // Resume the unpaid order if its pending if ($order_status == 'pending' || $order_status == 'failed') { // Update the existing order as we are resuming it $create_new_order = false; $order_data['ID'] = $order_id; wp_update_post($order_data); // Clear the old line items - we'll add these again in case they changed $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d )", $order_id)); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d", $order_id)); // Trigger an action for the resumed order do_action('woocommerce_resume_order', $order_id); } } if ($create_new_order) { $order_id = wp_insert_post($order_data, true); if (is_wp_error($order_id)) { throw new Exception('Error: Unable to create order. Please try again.'); } else { do_action('woocommerce_new_order', $order_id); } } // Store user data if ($this->checkout_fields['billing']) { foreach ($this->checkout_fields['billing'] as $key => $field) { update_post_meta($order_id, '_' . $key, $this->posted[$key]); if ($this->customer_id && apply_filters('woocommerce_checkout_update_customer_data', true, $this)) { update_user_meta($this->customer_id, $key, $this->posted[$key]); } } } if ($this->checkout_fields['shipping'] && WC()->cart->needs_shipping()) { foreach ($this->checkout_fields['shipping'] as $key => $field) { $postvalue = false; if ($this->posted['ship_to_different_address'] == false) { if (isset($this->posted[str_replace('shipping_', 'billing_', $key)])) { $postvalue = $this->posted[str_replace('shipping_', 'billing_', $key)]; update_post_meta($order_id, '_' . $key, $postvalue); } } else { $postvalue = $this->posted[$key]; update_post_meta($order_id, '_' . $key, $postvalue); } // User if ($postvalue && $this->customer_id && apply_filters('woocommerce_checkout_update_customer_data', true, $this)) { update_user_meta($this->customer_id, $key, $postvalue); } } } // Save any other user meta if ($this->customer_id) { do_action('woocommerce_checkout_update_user_meta', $this->customer_id, $this->posted); } // Store the line items to the new/resumed order foreach (WC()->cart->get_cart() as $cart_item_key => $values) { $_product = $values['data']; // Add line item $item_id = wc_add_order_item($order_id, array('order_item_name' => $_product->get_title(), 'order_item_type' => 'line_item')); // Add line item meta if ($item_id) { wc_add_order_item_meta($item_id, '_qty', apply_filters('woocommerce_stock_amount', $values['quantity'])); wc_add_order_item_meta($item_id, '_tax_class', $_product->get_tax_class()); wc_add_order_item_meta($item_id, '_product_id', $values['product_id']); wc_add_order_item_meta($item_id, '_variation_id', $values['variation_id']); wc_add_order_item_meta($item_id, '_line_subtotal', wc_format_decimal($values['line_subtotal'])); wc_add_order_item_meta($item_id, '_line_total', wc_format_decimal($values['line_total'])); wc_add_order_item_meta($item_id, '_line_tax', wc_format_decimal($values['line_tax'])); wc_add_order_item_meta($item_id, '_line_subtotal_tax', wc_format_decimal($values['line_subtotal_tax'])); // Store variation data in meta so admin can view it if ($values['variation'] && is_array($values['variation'])) { foreach ($values['variation'] as $key => $value) { wc_add_order_item_meta($item_id, esc_attr(str_replace('attribute_', '', $key)), $value); } } // Add line item meta for backorder status if ($_product->backorders_require_notification() && $_product->is_on_backorder($values['quantity'])) { wc_add_order_item_meta($item_id, apply_filters('woocommerce_backordered_item_meta_name', __('Backordered', 'woocommerce'), $cart_item_key, $order_id), $values['quantity'] - max(0, $_product->get_total_stock())); } // Allow plugins to add order item meta do_action('woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key); } } // Store fees foreach (WC()->cart->get_fees() as $fee) { $item_id = wc_add_order_item($order_id, array('order_item_name' => $fee->name, 'order_item_type' => 'fee')); if ($fee->taxable) { wc_add_order_item_meta($item_id, '_tax_class', $fee->tax_class); } else { wc_add_order_item_meta($item_id, '_tax_class', '0'); } wc_add_order_item_meta($item_id, '_line_total', wc_format_decimal($fee->amount)); wc_add_order_item_meta($item_id, '_line_tax', wc_format_decimal($fee->tax)); } // Store shipping for all packages $packages = WC()->shipping->get_packages(); foreach ($packages as $i => $package) { if (isset($package['rates'][$this->shipping_methods[$i]])) { $method = $package['rates'][$this->shipping_methods[$i]]; $item_id = wc_add_order_item($order_id, array('order_item_name' => $method->label, 'order_item_type' => 'shipping')); if ($item_id) { wc_add_order_item_meta($item_id, 'method_id', $method->id); wc_add_order_item_meta($item_id, 'cost', wc_format_decimal($method->cost)); do_action('woocommerce_add_shipping_order_item', $order_id, $item_id, $i); } } } // Store tax rows foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $key) { $code = WC()->cart->tax->get_rate_code($key); if ($code) { $item_id = wc_add_order_item($order_id, array('order_item_name' => $code, 'order_item_type' => 'tax')); // Add line item meta if ($item_id) { wc_add_order_item_meta($item_id, 'rate_id', $key); wc_add_order_item_meta($item_id, 'label', WC()->cart->tax->get_rate_label($key)); wc_add_order_item_meta($item_id, 'compound', absint(WC()->cart->tax->is_compound($key) ? 1 : 0)); wc_add_order_item_meta($item_id, 'tax_amount', wc_format_decimal(isset(WC()->cart->taxes[$key]) ? WC()->cart->taxes[$key] : 0)); wc_add_order_item_meta($item_id, 'shipping_tax_amount', wc_format_decimal(isset(WC()->cart->shipping_taxes[$key]) ? WC()->cart->shipping_taxes[$key] : 0)); } } } // Store coupons if ($applied_coupons = WC()->cart->get_coupons()) { foreach ($applied_coupons as $code => $coupon) { $item_id = wc_add_order_item($order_id, array('order_item_name' => $code, 'order_item_type' => 'coupon')); // Add line item meta if ($item_id) { wc_add_order_item_meta($item_id, 'discount_amount', isset(WC()->cart->coupon_discount_amounts[$code]) ? WC()->cart->coupon_discount_amounts[$code] : 0); } } } if ($this->payment_method) { update_post_meta($order_id, '_payment_method', $this->payment_method->id); update_post_meta($order_id, '_payment_method_title', $this->payment_method->get_title()); } if (empty($this->posted['billing_email']) && is_user_logged_in()) { $current_user = wp_get_current_user(); update_post_meta($order_id, '_billing_email', $current_user->user_email); } update_post_meta($order_id, '_order_shipping', wc_format_decimal(WC()->cart->shipping_total)); update_post_meta($order_id, '_order_discount', wc_format_decimal(WC()->cart->get_order_discount_total())); update_post_meta($order_id, '_cart_discount', wc_format_decimal(WC()->cart->get_cart_discount_total())); update_post_meta($order_id, '_order_tax', wc_format_decimal(WC()->cart->tax_total)); update_post_meta($order_id, '_order_shipping_tax', wc_format_decimal(WC()->cart->shipping_tax_total)); update_post_meta($order_id, '_order_total', wc_format_decimal(WC()->cart->total, get_option('woocommerce_price_num_decimals'))); update_post_meta($order_id, '_order_key', 'wc_' . apply_filters('woocommerce_generate_order_key', uniqid('order_'))); update_post_meta($order_id, '_customer_user', absint($this->customer_id)); update_post_meta($order_id, '_order_currency', get_woocommerce_currency()); update_post_meta($order_id, '_prices_include_tax', get_option('woocommerce_prices_include_tax')); update_post_meta($order_id, '_customer_ip_address', isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']); update_post_meta($order_id, '_customer_user_agent', isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''); // Let plugins add meta do_action('woocommerce_checkout_update_order_meta', $order_id, $this->posted); // Order status wp_set_object_terms($order_id, 'pending', 'shop_order_status'); return $order_id; }
/** * @deprecated */ function woocommerce_add_order_item_meta($item_id, $meta_key, $meta_value, $unique = false) { return wc_add_order_item_meta($item_id, $meta_key, $meta_value, $unique); }
public function woocommerce_thankyou_order_id($order_id) { $currencies = $this->get_currencies(); //+++ $rate = get_post_meta($order_id, '_woocs_order_rate', true); //condition (!$rate) is lock of chaning order currency looking it using link like //http://dev.currency-switcher.com/checkout/order-received/1003/?key=wc_order_55a52c81ad4ef //this needs if back in paypal process, but looks like it is more dangerous, so commented //update_post_meta($order_id, '_order_currency', $this->current_currency); //+++ update_post_meta($order_id, '_woocs_order_rate', $currencies[$this->current_currency]['rate']); wc_add_order_item_meta($order_id, '_woocs_order_rate', $currencies[$this->current_currency]['rate'], true); update_post_meta($order_id, '_woocs_order_base_currency', $this->default_currency); wc_add_order_item_meta($order_id, '_woocs_order_base_currency', $this->default_currency, true); update_post_meta($order_id, '_woocs_order_currency_changed_mannualy', 0); wc_add_order_item_meta($order_id, '_woocs_order_currency_changed_mannualy', 0, true); return $order_id; }
/** * Save meta box data */ public static function save($post_id, $post) { global $wpdb; // Save tax rows $total_tax = 0; $total_shipping_tax = 0; if (isset($_POST['order_taxes_id'])) { $get_values = array('order_taxes_id', 'order_taxes_rate_id', 'order_taxes_amount', 'order_taxes_shipping_amount'); foreach ($get_values as $value) { ${$value} = isset($_POST[$value]) ? $_POST[$value] : array(); } foreach ($order_taxes_id as $item_id => $value) { if ($item_id == 'new') { foreach ($value as $new_key => $new_value) { $rate_id = absint($order_taxes_rate_id[$item_id][$new_key]); if ($rate_id) { $rate = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %s", $rate_id)); $label = $rate->tax_rate_name ? $rate->tax_rate_name : WC()->countries->tax_or_vat(); $compound = $rate->tax_rate_compound ? 1 : 0; $code = array(); $code[] = $rate->tax_rate_country; $code[] = $rate->tax_rate_state; $code[] = $rate->tax_rate_name ? $rate->tax_rate_name : 'TAX'; $code[] = absint($rate->tax_rate_priority); $code = strtoupper(implode('-', array_filter($code))); } else { $code = ''; $label = WC()->countries->tax_or_vat(); } // Add line item $new_id = wc_add_order_item($post_id, array('order_item_name' => wc_clean($code), 'order_item_type' => 'tax')); // Add line item meta if ($new_id) { wc_update_order_item_meta($new_id, 'rate_id', $rate_id); wc_update_order_item_meta($new_id, 'label', $label); wc_update_order_item_meta($new_id, 'compound', $compound); if (isset($order_taxes_amount[$item_id][$new_key])) { wc_update_order_item_meta($new_id, 'tax_amount', wc_format_decimal($order_taxes_amount[$item_id][$new_key])); $total_tax += wc_format_decimal($order_taxes_amount[$item_id][$new_key]); } if (isset($order_taxes_shipping_amount[$item_id][$new_key])) { wc_update_order_item_meta($new_id, 'shipping_tax_amount', wc_format_decimal($order_taxes_shipping_amount[$item_id][$new_key])); $total_shipping_tax += wc_format_decimal($order_taxes_shipping_amount[$item_id][$new_key]); } } } } else { $item_id = absint($item_id); $rate_id = absint($order_taxes_rate_id[$item_id]); if ($rate_id) { $rate = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %s", $rate_id)); $label = $rate->tax_rate_name ? $rate->tax_rate_name : WC()->countries->tax_or_vat(); $compound = $rate->tax_rate_compound ? 1 : 0; $code = array(); $code[] = $rate->tax_rate_country; $code[] = $rate->tax_rate_state; $code[] = $rate->tax_rate_name ? $rate->tax_rate_name : 'TAX'; $code[] = absint($rate->tax_rate_priority); $code = strtoupper(implode('-', array_filter($code))); } else { $code = ''; $label = WC()->countries->tax_or_vat(); } $wpdb->update($wpdb->prefix . "woocommerce_order_items", array('order_item_name' => wc_clean($code)), array('order_item_id' => $item_id), array('%s'), array('%d')); wc_update_order_item_meta($item_id, 'rate_id', $rate_id); wc_update_order_item_meta($item_id, 'label', $label); wc_update_order_item_meta($item_id, 'compound', $compound); if (isset($order_taxes_amount[$item_id])) { wc_update_order_item_meta($item_id, 'tax_amount', wc_format_decimal($order_taxes_amount[$item_id])); $total_tax += wc_format_decimal($order_taxes_amount[$item_id]); } if (isset($order_taxes_shipping_amount[$item_id])) { wc_update_order_item_meta($item_id, 'shipping_tax_amount', wc_format_decimal($order_taxes_shipping_amount[$item_id])); $total_shipping_tax += wc_format_decimal($order_taxes_shipping_amount[$item_id]); } } } } // Update totals update_post_meta($post_id, '_order_tax', wc_format_decimal($total_tax)); update_post_meta($post_id, '_order_shipping_tax', wc_format_decimal($total_shipping_tax)); update_post_meta($post_id, '_order_discount', wc_format_decimal($_POST['_order_discount'])); update_post_meta($post_id, '_order_total', wc_format_decimal($_POST['_order_total'])); // Shipping Rows $order_shipping = 0; if (isset($_POST['shipping_method_id'])) { $get_values = array('shipping_method_id', 'shipping_method_title', 'shipping_method', 'shipping_cost'); foreach ($get_values as $value) { ${$value} = isset($_POST[$value]) ? $_POST[$value] : array(); } foreach ($shipping_method_id as $item_id => $value) { if ($item_id == 'new') { foreach ($value as $new_key => $new_value) { $method_id = wc_clean($shipping_method[$item_id][$new_key]); $method_title = wc_clean($shipping_method_title[$item_id][$new_key]); $cost = wc_format_decimal($shipping_cost[$item_id][$new_key]); $new_id = wc_add_order_item($post_id, array('order_item_name' => $method_title, 'order_item_type' => 'shipping')); if ($new_id) { wc_add_order_item_meta($new_id, 'method_id', $method_id); wc_add_order_item_meta($new_id, 'cost', $cost); } $order_shipping += $cost; } } else { $item_id = absint($item_id); $method_id = wc_clean($shipping_method[$item_id]); $method_title = wc_clean($shipping_method_title[$item_id]); $cost = wc_format_decimal($shipping_cost[$item_id]); $wpdb->update($wpdb->prefix . "woocommerce_order_items", array('order_item_name' => $method_title), array('order_item_id' => $item_id), array('%s'), array('%d')); wc_update_order_item_meta($item_id, 'method_id', $method_id); wc_update_order_item_meta($item_id, 'cost', $cost); $order_shipping += $cost; } } } // Delete rows if (isset($_POST['delete_order_item_id'])) { $delete_ids = $_POST['delete_order_item_id']; foreach ($delete_ids as $id) { wc_delete_order_item(absint($id)); } } delete_post_meta($post_id, '_shipping_method'); delete_post_meta($post_id, '_shipping_method_title'); update_post_meta($post_id, '_order_shipping', $order_shipping); }
/** * This function updates the database for the delivery details and adds delivery fields on the Order Received page, * WooCommerce->Orders when an order is placed for WooCommerce version greater than 2.0. */ function prdd_lite_order_item_meta($item_meta, $cart_item) { if (version_compare(WOOCOMMERCE_VERSION, "2.0.0") < 0) { return; } // Add the fields global $wpdb, $woocommerce; foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) { $_product = $values['data']; if (isset($values['prdd_lite_delivery'])) { $delivery = $values['prdd_lite_delivery']; } $quantity = $values['quantity']; $post_id = $this->prdd_lite_get_product_id($values['product_id']); $post_title = $_product->get_title(); $query = "SELECT order_item_id,order_id FROM `" . $wpdb->prefix . "woocommerce_order_items`\n\t\t\t\t\t\tWHERE order_id = %s AND order_item_name = %s"; $results = $wpdb->get_results($wpdb->prepare($query, $item_meta, $post_title)); $order_item_ids[] = $results[0]->order_item_id; $order_id = $results[0]->order_id; $order_obj = new WC_order($order_id); $details = $product_ids = array(); $order_items = $order_obj->get_items(); if (isset($values['prdd_lite_delivery'])) { $prdd_settings = get_post_meta($post_id, '_woo_prdd_lite_enable_delivery_date', true); $details = array(); if (isset($delivery[0]['delivery_date']) && $delivery[0]['delivery_date'] != "") { $name = "Delivery Date"; $date_select = $delivery[0]['delivery_date']; wc_add_order_item_meta($results[0]->order_item_id, $name, sanitize_text_field($date_select, true)); } if (array_key_exists('delivery_hidden_date', $delivery[0]) && $delivery[0]['delivery_hidden_date'] != "") { $date_booking = date('Y-m-d', strtotime($delivery[0]['delivery_hidden_date'])); wc_add_order_item_meta($results[0]->order_item_id, '_prdd_lite_date', sanitize_text_field($date_booking, true)); } } if (version_compare(WOOCOMMERCE_VERSION, "2.5") < 0) { continue; } else { // Code where the Delivery dates are not displayed in the customer new order email from WooCommerce version 2.5 $cache_key = WC_Cache_Helper::get_cache_prefix('orders') . 'item_meta_array_' . $results[0]->order_item_id; $item_meta_array = wp_cache_get($cache_key, 'orders'); if (false !== $item_meta_array) { $metadata = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value, meta_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d AND meta_key IN (%s,%s) ORDER BY meta_id", absint($results[0]->order_item_id), "Delivery Date", '_prdd_lite_date')); foreach ($metadata as $metadata_row) { $item_meta_array[$metadata_row->meta_id] = (object) array('key' => $metadata_row->meta_key, 'value' => $metadata_row->meta_value); } wp_cache_set($cache_key, $item_meta_array, 'orders'); } } } }
/** * Add commission id from parent to child order */ public function register_commission_to_parent_order($commission_id, $child_item_id, $key, $suborder) { // add line item to retrieve simply the commission associated (child order) $parent_item_id = self::get_parent_item_id($suborder, $child_item_id); !empty($parent_item_id) && wc_add_order_item_meta($parent_item_id, '_child_' . $key, $commission_id); }
/** * Add product meta * @param $item_id * @param array $meta */ private function add_product_meta($item_id, array $meta) { // line meta foreach ($meta as $m) { $label = isset($m['label']) ? $m['label'] : ''; $value = isset($m['value']) ? $m['value'] : ''; wc_add_order_item_meta($item_id, $label, $value); } }
/** * @param int $order_id * @return array */ public function process_payment($order_id) { // get order object $order = new WC_Order($order_id); $cashback = isset($_POST['pos-cashback']) ? wc_format_decimal($_POST['pos-cashback']) : 0; if ($cashback !== 0) { // add order meta update_post_meta($order_id, '_pos_card_cashback', $cashback); // add cashback as fee line item // TODO: this should be handled by $order->add_fee after WC 2.2 $item_id = wc_add_order_item($order_id, array('order_item_name' => __('Cashback', 'woocommerce-pos'), 'order_item_type' => 'fee')); if ($item_id) { wc_add_order_item_meta($item_id, '_line_total', $cashback); wc_add_order_item_meta($item_id, '_line_tax', 0); wc_add_order_item_meta($item_id, '_line_subtotal', $cashback); wc_add_order_item_meta($item_id, '_line_subtotal_tax', 0); wc_add_order_item_meta($item_id, '_tax_class', 'zero-rate'); } // update the order total to include fee $order_total = get_post_meta($order_id, '_order_total', true); $order_total += $cashback; update_post_meta($order_id, '_order_total', $order_total); } // payment complete $order->payment_complete(); // success return array('result' => 'success'); }
/** * For each package, find the order lines that contain the products that * are included in that package, and link those order lines to this * shipping line. * The assumption is made that however the packages are grouped, each product * can only be put into one package (so no grouping by product variation, for * example). * * @order_id int The Order ID in wp_posts * @shipping_line_id int The ID of the shipping order line that has just been added. * @package_key int The index number of the package in the WC()->shipping->get_packages() list */ public static function link_shipping_line_item($order_id, $shipping_line_id, $package_key) { $packages = WC()->shipping->get_packages(); if (!isset($packages[$package_key])) { return; } $package = $packages[$package_key]; $product_ids = array(); foreach ($package['contents'] as $product) { if (!in_array($product['product_id'], $product_ids)) { $product_ids[] = $product['product_id']; } } // Add the product_ids to the shipping order line, for reference. // Implode to a string, otherwise the API has difficulty supplying it. wc_add_order_item_meta($shipping_line_id, self::ORDER_PRODUCT_IDS_FIELD, implode('|', $product_ids), true); // Go through the order lines and find those that contain these product. $order_line_ids = array(); $order = new WC_Order($order_id); foreach ($order->get_items() as $order_line_id => $product) { if (isset($product['product_id']) && in_array($product['product_id'], $product_ids)) { $order_line_ids[] = $order_line_id; // Link this product order line to its shipping order line. wc_add_order_item_meta($order_line_id, static::SHIPPING_LINE_ID_FIELD, $shipping_line_id, true); } } // Throw the order line IDs onto the shipping line too, for a two-way // link. // Convert it to a string before saving it, as arrays do not get exposed to the REST API. wc_add_order_item_meta($shipping_line_id, static::ORDER_LINE_IDS_FIELD, implode('|', $order_line_ids), true); }