本文整理汇总了PHP中wc_add_order_item函数的典型用法代码示例。如果您正苦于以下问题:PHP wc_add_order_item函数的具体用法?PHP wc_add_order_item怎么用?PHP wc_add_order_item使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wc_add_order_item函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: add_order_item
/**
* Add order item via ajax
* exact copy from /wp-content/plugins/woocommerce/includes/class-wc-ajax.php, with change to template selection
*/
public static function add_order_item()
{
check_ajax_referer('order-item', 'security');
$item_to_add = sanitize_text_field($_POST['item_to_add']);
$order_id = absint($_POST['order_id']);
// Find the item
if (!is_numeric($item_to_add)) {
die;
}
$post = get_post($item_to_add);
if (!$post || 'product' !== $post->post_type && 'product_variation' !== $post->post_type) {
die;
}
$_product = wc_get_product($post->ID);
$order = wc_get_order($order_id);
$order_taxes = $order->get_taxes();
$class = 'new_row';
// Set values
$item = array();
$item['product_id'] = $_product->id;
$item['variation_id'] = isset($_product->variation_id) ? $_product->variation_id : '';
$item['variation_data'] = isset($_product->variation_data) ? $_product->variation_data : '';
$item['name'] = $_product->get_title();
$item['tax_class'] = $_product->get_tax_class();
$item['qty'] = 1;
$item['line_subtotal'] = wc_format_decimal($_product->get_price_excluding_tax());
$item['line_subtotal_tax'] = '';
$item['line_total'] = wc_format_decimal($_product->get_price_excluding_tax());
$item['line_tax'] = '';
// Add line item
$item_id = wc_add_order_item($order_id, array('order_item_name' => $item['name'], 'order_item_type' => 'line_item'));
// Add line item meta
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, '_variation_id', $item['variation_id']);
wc_add_order_item_meta($item_id, '_line_subtotal', $item['line_subtotal']);
wc_add_order_item_meta($item_id, '_line_subtotal_tax', $item['line_subtotal_tax']);
wc_add_order_item_meta($item_id, '_line_total', $item['line_total']);
wc_add_order_item_meta($item_id, '_line_tax', $item['line_tax']);
// Since 2.2
wc_add_order_item_meta($item_id, '_line_tax_data', array('total' => array(), 'subtotal' => array()));
// Store variation data in meta
if ($item['variation_data'] && is_array($item['variation_data'])) {
foreach ($item['variation_data'] as $key => $value) {
wc_add_order_item_meta($item_id, str_replace('attribute_', '', $key), $value);
}
}
do_action('woocommerce_ajax_add_order_item_meta', $item_id, $item);
}
$item = apply_filters('woocommerce_ajax_order_item', $item, $item_id);
//include( 'admin/meta-boxes/views/html-order-item.php' );
//@@@@LOUSHOU - allow overtake of template
include apply_filters('qsot-woo-template', 'meta-boxes/views/html-order-item.php', 'admin');
// Quit out
die;
}
示例2: add_product
/**
* Add the details of an order item to a subscription as a product line item.
*
* When adding a product to a subscription, we can't use WC_Abstract_Order::add_product() because it requires a product object
* and the details of the product may have changed since it was purchased so we can't simply instantiate an instance of the
* product based on ID.
*
* @param WC_Subscription $new_subscription A subscription object
* @param int $order_item_id ID of the subscription item on the original order
* @param array $order_item An array of order item data in the form returned by WC_Abstract_Order::get_items()
* @return int Subscription $item_id The order item id of the new line item added to the subscription.
* @since 2.0
*/
private static function add_product($new_subscription, $order_item_id, $order_item)
{
global $wpdb;
$item_id = wc_add_order_item($new_subscription->id, array('order_item_name' => $order_item['name'], 'order_item_type' => 'line_item'));
WCS_Upgrade_Logger::add(sprintf('For subscription %d: new line item ID %d added', $new_subscription->id, $item_id));
$order_item = WCS_Repair_2_0::maybe_repair_order_item($order_item);
$wpdb->query($wpdb->prepare("INSERT INTO `{$wpdb->prefix}woocommerce_order_itemmeta` (`order_item_id`, `meta_key`, `meta_value`)\n\t\t\t VALUES\n\t\t\t\t(%d, '_qty', %s),\n\t\t\t\t(%d, '_tax_class', %s),\n\t\t\t\t(%d, '_product_id', %s),\n\t\t\t\t(%d, '_variation_id', %s),\n\t\t\t\t(%d, '_line_subtotal', %s),\n\t\t\t\t(%d, '_line_total', %s),\n\t\t\t\t(%d, '_line_subtotal_tax', %s),\n\t\t\t\t(%d, '_line_tax', %s)", $item_id, $order_item['qty'], $item_id, $order_item['tax_class'], $item_id, $order_item['product_id'], $item_id, $order_item['variation_id'], $item_id, $order_item['recurring_line_subtotal'], $item_id, $order_item['recurring_line_total'], $item_id, $order_item['recurring_line_subtotal_tax'], $item_id, $order_item['recurring_line_tax']));
// Save tax data array added in WC 2.2 (so it won't exist for all orders/subscriptions)
self::add_line_tax_data($item_id, $order_item_id, $order_item);
if (isset($order_item['subscription_trial_length']) && $order_item['subscription_trial_length'] > 0) {
wc_add_order_item_meta($item_id, '_has_trial', 'true');
}
// Don't copy item meta already copied
$reserved_item_meta_keys = array('_item_meta', '_qty', '_tax_class', '_product_id', '_variation_id', '_line_subtotal', '_line_total', '_line_tax', '_line_tax_data', '_line_subtotal_tax');
$meta_keys_to_copy = array_diff(array_keys($order_item['item_meta']), array_merge($reserved_item_meta_keys, self::$subscription_item_meta_keys));
// Add variation and any other meta
foreach ($meta_keys_to_copy as $meta_key) {
foreach ($order_item['item_meta'][$meta_key] as $meta_value) {
wc_add_order_item_meta($item_id, $meta_key, $meta_value);
}
}
WCS_Upgrade_Logger::add(sprintf('For subscription %d: for item %d added %s', $new_subscription->id, $item_id, implode(', ', $meta_keys_to_copy)));
// Now that we've copied over the old data, prefix some the subscription meta keys with _wcs_migrated to deprecate it without deleting it (yet)
$rows_affected = self::deprecate_item_meta($order_item_id);
WCS_Upgrade_Logger::add(sprintf('For subscription %d: %s rows of line item meta deprecated', $new_subscription->id, $rows_affected));
return $item_id;
}
示例3: unset
$wpdb->query($wpdb->prepare("\n\t\t\t\tUPDATE {$wpdb->postmeta}\n\t\t\t\tSET meta_key = '_order_items_old'\n\t\t\t\tWHERE meta_key = '_order_items'\n\t\t\t\tAND post_id = %d\n\t\t\t", $order_item_row->post_id));
}
unset($meta_rows, $item_id, $order_item);
}
}
// 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) {
示例4: dokan_create_sub_order_shipping
/**
* 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;
}
示例5: add_order_item
/**
* Add an item to the provided order
*
* @since 3.0.0
* @param \WC_Order $order
* @param array $item Parsed item data from CSV
* @param string $type Line item type
* @return int|false ID of the inserted order item, false on failure
*/
private function add_order_item(WC_Order $order, $item, $type)
{
$result = false;
switch ($type) {
case 'line_item':
$product = $this->get_product_for_item($item);
$args = $this->prepare_product_args($item);
$result = $order->add_product($product, $args['qty'], $args);
if (!$result) {
wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot add order item "%s".', 'woocommerce-csv-import-suite'), esc_html($identifier)));
}
break;
case 'shipping':
$args = array('order_item_name' => $item['method_title'], 'order_item_type' => 'shipping');
// we're using wc_add_order_item instead of $order->add_shipping because
// we do not want the order total to be recalculated
$result = wc_add_order_item($order->id, $args);
if (!$result) {
wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot add shipping method "%s".', 'woocommerce-csv-import-suite'), esc_html($item['title'])));
}
break;
case 'tax':
$args = array('order_item_name' => $item['code'], 'order_item_type' => 'tax');
$result = wc_add_order_item($order->id, $args);
if (!$result) {
wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot add tax "%s".', 'woocommerce-csv-import-suite'), esc_html($item['label'])));
}
break;
case 'coupon':
$result = $order->add_coupon($item['code'], $item['amount']);
if (!$result) {
wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot add coupon "%s".', 'woocommerce-csv-import-suite'), esc_html($item['code'])));
}
break;
case 'fee':
$order_fee = new stdClass();
$order_fee->id = sanitize_title($item['title']);
$order_fee->name = $item['title'];
$order_fee->amount = isset($item['total']) ? floatval($item['total']) : 0;
$order_fee->taxable = false;
$order_fee->tax = 0;
$order_fee->tax_data = array();
$order_fee->tax_class = '';
// if taxable, tax class and total are required
if (isset($item['taxable']) && $item['taxable']) {
$order_fee->taxable = true;
$order_fee->tax_class = $item['tax_class'];
if (isset($item['total_tax'])) {
$order_fee->tax = isset($item['total_tax']) ? wc_format_refund_total($item['total_tax']) : 0;
}
if (isset($item['tax_data'])) {
$tax_data = isset($item['tax_data']['total']) ? $item['tax_data']['total'] : $item['tax_data'];
$order_fee->tax = wc_format_refund_total(array_sum($tax_data));
$order_fee->tax_data = array_map('wc_format_refund_total', $tax_data);
}
}
$result = $order->add_fee($order_fee);
if (!$result) {
wc_csv_import_suite()->log(sprintf(__('> > Warning: cannot add fee "%s".', 'woocommerce-csv-import-suite'), esc_html($item['title'])));
}
break;
}
// store original order item ID
if ($result && isset($item['order_item_id']) && $item['order_item_id'] > 0) {
wc_update_order_item_meta($result, '_original_order_item_id', $item['order_item_id']);
}
return $result;
}
示例6: sync
//.........这里部分代码省略.........
$order = wc_create_order(array('customer_id' => $customer_id, 'customer_note' => $customer_note, 'created_via' => 'eBay'));
remove_filter('woocommerce_new_order_data', $new_order_data_callback);
$order_id = $order->id;
update_post_meta($order_id, '_codisto_orderid', (int) $ordercontent->orderid);
update_post_meta($order_id, '_codisto_ebayuser', (string) $ordercontent->ebayusername);
update_post_meta($order_id, '_order_currency', (string) $ordercontent->transactcurrency);
update_post_meta($order_id, '_customer_ip_address', '-');
delete_post_meta($order_id, '_prices_include_tax');
do_action('woocommerce_new_order', $order_id);
foreach ($ordercontent->orderlines->orderline as $orderline) {
if ($orderline->productcode[0] != 'FREIGHT') {
$productcode = (string) $orderline->productcode;
if ($productcode == null) {
$productcode = '';
}
$productname = (string) $orderline->productname;
if ($productname == null) {
$productname = '';
}
$product_id = $orderline->externalreference[0];
if ($product_id != null) {
$product_id = intval($product_id);
}
$variation_id = 0;
if (get_post_type($product_id) === 'product_variation') {
$variation_id = $product_id;
$product_id = wp_get_post_parent_id($variation_id);
if (!is_numeric($product_id) || $product_id === 0) {
$product_id = 0;
$variation_id = 0;
}
}
$qty = (int) $orderline->quantity[0];
$item_id = wc_add_order_item($order_id, array('order_item_name' => $productname, 'order_item_type' => 'line_item'));
wc_add_order_item_meta($item_id, '_qty', $qty);
if (!is_null($product_id) && $product_id !== 0) {
wc_add_order_item_meta($item_id, '_product_id', $product_id);
wc_add_order_item_meta($item_id, '_variation_id', $variation_id);
wc_add_order_item_meta($item_id, '_tax_class', '');
} else {
wc_add_order_item_meta($item_id, '_product_id', 0);
wc_add_order_item_meta($item_id, '_variation_id', 0);
wc_add_order_item_meta($item_id, '_tax_class', '');
}
$line_total = wc_format_decimal((double) $orderline->linetotal);
$line_total_tax = wc_format_decimal((double) $orderline->linetotalinctax - (double) $orderline->linetotal);
wc_add_order_item_meta($item_id, '_line_subtotal', $line_total);
wc_add_order_item_meta($item_id, '_line_total', $line_total);
wc_add_order_item_meta($item_id, '_line_subtotal_tax', $line_total_tax);
wc_add_order_item_meta($item_id, '_line_tax', $line_total_tax);
wc_add_order_item_meta($item_id, '_line_tax_data', array('total' => array(1 => $line_total_tax), 'subtotal' => array(1 => $line_total_tax)));
$tax += $line_total_tax;
} else {
$item_id = wc_add_order_item($order_id, array('order_item_name' => (string) $orderline->productname, 'order_item_type' => 'shipping'));
wc_add_order_item_meta($item_id, 'cost', wc_format_decimal((double) $orderline->linetotal));
$shipping += (double) $orderline->linetotal;
$shipping_tax += (double) $orderline->linetotalinctax - (double) $orderline->linetotal;
}
}
if ($ordercontent->paymentstatus == 'complete') {
$transaction_id = (string) $ordercontent->orderpayments[0]->orderpayment->transactionid;
if ($transaction_id) {
update_post_meta($order_id, '_payment_method', 'paypal');
update_post_meta($order_id, '_payment_method_title', __('PayPal', 'woocommerce'));
update_post_meta($order_id, '_transaction_id', $transaction_id);
} else {
示例7: create_order
/**
* 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()));
}
//.........这里部分代码省略.........
示例8: save
/**
* 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'));
//.........这里部分代码省略.........
示例9: create_renewal_order
//.........这里部分代码省略.........
'_subscriptio_renewal' => 'yes',
);
foreach ($other_meta_fields as $field_key => $field_value) {
update_post_meta($order_id, $field_key, $field_value);
}
// Check if subscription product is variable
$product_id_to_use = !empty($subscription->variation_id) ? $subscription->variation_id : $subscription->product_id;
// Check if product still exists
if (Subscriptio::product_is_active($product_id_to_use)) {
// Load product object
$product = new WC_Product($product_id_to_use);
// Get product name
$product_title = $product->get_title();
// Update product name on subscription if it was changed
if ($product_title != $subscription->product_name) {
$subscription->update_subscription_details(array(
'product_name' => $product_title,
));
}
}
// If not - use saved product "snapshot" from previous order
else {
$product_title = $subscription->product_name;
}
// Add line item (product) to order
$item_id = wc_add_order_item($order_id, array(
'order_item_name' => $product_title,
'order_item_type' => 'line_item',
));
if (!$item_id) {
throw new Exception(__('Unable to add product to renewal order.', 'subscriptio'));
}
// Add line item meta
$item_meta = array(
'_qty' => !empty($subscription->quantity) ? $subscription->quantity : 1,
'_tax_class' => $subscription->renewal_tax_class,
'_product_id' => $subscription->product_id,
'_variation_id' => !empty($subscription->variation_id) ? $subscription->variation_id : '',
'_line_subtotal' => wc_format_decimal($subscription->renewal_line_subtotal),
'_line_subtotal_tax' => wc_format_decimal($subscription->renewal_line_subtotal_tax),
'_line_total' => wc_format_decimal($subscription->renewal_line_total),
'_line_tax' => wc_format_decimal($subscription->renewal_line_tax),
);
foreach ($item_meta as $item_meta_key => $item_meta_value) {
wc_add_order_item_meta($item_id, $item_meta_key, $item_meta_value);
}
// Save shipping info (if any)
if (!empty($subscription->shipping)) {
$shipping_item_id = wc_add_order_item($order_id, array(
'order_item_name' => $subscription->shipping['name'],
'order_item_type' => 'shipping',
));
wc_add_order_item_meta($shipping_item_id, 'method_id', $subscription->shipping['method_id']);
示例10: update_orders
function update_orders()
{
global $wpdb;
// loop through orders
$wpec_order_table = $wpdb->prefix . 'wpsc_purchase_logs';
$wpec_formdata_table = $wpdb->prefix . 'wpsc_submited_form_data';
$order_data = $wpdb->get_results("SELECT * FROM `" . $wpec_order_table . "`", ARRAY_A);
foreach ((array) $order_data as $order) {
$post_title = "WPEC Order - " . $order['id'] . " - " . date('Y-m-d H:i:s', $order['date']);
// check to see if order has already been added
$order_exists = $wpdb->get_var($wpdb->prepare("\n SELECT ID FROM {$wpdb->posts} \n WHERE post_title = %s \n AND post_type = 'shop_order'", $post_title));
if ($order_exists) {
continue;
}
// create a new post with custom post type 'shop_order'
$post = array('comment_status' => 'closed', 'ping_status' => 'closed', 'post_author' => $this->post_author, 'post_parent' => '0', 'post_status' => 'publish', 'post_title' => $post_title, 'post_type' => 'shop_order');
// insert post
$post_id = wp_insert_post($post, true);
// wpec tables
$wpsc_cart_contents_table = $wpdb->prefix . 'wpsc_cart_contents';
$wpsc_purchase_logs_table = $wpdb->prefix . 'wpsc_purchase_logs';
$wpsc_submited_form_data_table = $wpdb->prefix . 'wpsc_submited_form_data';
$wpsc_checkout_forms_table = $wpdb->prefix . 'wpsc_checkout_forms';
/*
CUSTOMER DATA
*/
$userinfo = $wpdb->get_results("\n SELECT \n `" . $wpsc_submited_form_data_table . "`.`value`,\n `" . $wpsc_checkout_forms_table . "`.`name`,\n `" . $wpsc_checkout_forms_table . "`.`unique_name`\n FROM `" . $wpsc_checkout_forms_table . "`\n LEFT JOIN `" . $wpsc_submited_form_data_table . "`\n ON `" . $wpsc_checkout_forms_table . "`.id = `" . $wpsc_submited_form_data_table . "`.`form_id`\n WHERE `" . $wpsc_submited_form_data_table . "`.`log_id`=" . $order['id'] . "\n ORDER BY `" . $wpsc_checkout_forms_table . "`.`checkout_order`\n ", ARRAY_A);
foreach ($userinfo as $info) {
$userinfo[$info['unique_name']] = $info['value'];
}
// ID
update_post_meta($post_id, '_customer_user', $order['user_ID']);
// billing address
update_post_meta($post_id, '_billing_first_name', $userinfo['billingfirstname']);
update_post_meta($post_id, '_billing_last_name', $userinfo['billinglastname']);
update_post_meta($post_id, '_billing_address_1', $userinfo['billingaddress']);
update_post_meta($post_id, '_billing_address_2', "");
update_post_meta($post_id, '_billing_city', $userinfo['billingcity']);
update_post_meta($post_id, '_billing_postcode', $userinfo['billingpostcode']);
update_post_meta($post_id, '_billing_country', $userinfo['billingcountry']);
update_post_meta($post_id, '_billing_email', $userinfo['billingemail']);
update_post_meta($post_id, '_billing_phone', $userinfo['billingphone']);
// shipping address
update_post_meta($post_id, '_shipping_first_name', $userinfo['shippingfirstname']);
update_post_meta($post_id, '_shipping_last_name', $userinfo['shippinglastname']);
update_post_meta($post_id, '_shipping_company', "");
update_post_meta($post_id, '_shipping_address_1', $userinfo['shippingaddress']);
update_post_meta($post_id, '_shipping_address_2', "");
update_post_meta($post_id, '_shipping_city', $userinfo['shippingcity']);
update_post_meta($post_id, '_shipping_postcode', $userinfo['shippingpostcode']);
update_post_meta($post_id, '_shipping_country', $userinfo['shippingcountry']);
update_post_meta($post_id, '_shipping_state', "");
/*
ORDER ITEMS
*/
$cartcontent = $wpdb->get_results("\n SELECT * \n FROM `" . $wpsc_cart_contents_table . "` \n WHERE `purchaseid`=" . $order['id'] . "\n ");
foreach ($cartcontent as $item) {
$item_id = wc_add_order_item($post_id, array('order_item_name' => $item->name, 'order_item_type' => 'line_item'));
if ($item_id) {
wc_add_order_item_meta($item_id, '_qty', $item->quantity);
wc_add_order_item_meta($item_id, '_product_id', $item->prodid);
wc_add_order_item_meta($item_id, '_variation_id', null);
$subtotal = $item->quantity * $item->price;
wc_add_order_item_meta($item_id, '_line_subtotal', $subtotal);
wc_add_order_item_meta($item_id, '_line_subtotal_tax', $subtotal + $item->tax_charged);
wc_add_order_item_meta($item_id, '_line_total', $subtotal + $item->tax_charged);
wc_add_order_item_meta($item_id, '_line_tax', $item->tax_charged);
}
}
/*
ORDER DATA
*/
$extrainfo = $wpdb->get_results("\n SELECT DISTINCT `" . $wpsc_purchase_logs_table . "` . * \n FROM `" . $wpsc_submited_form_data_table . "`\n LEFT JOIN `" . $wpsc_purchase_logs_table . "`\n ON `" . $wpsc_submited_form_data_table . "`.`log_id` = `" . $wpsc_purchase_logs_table . "`.`id`\n WHERE `" . $wpsc_purchase_logs_table . "`.`id`=" . $order['id'] . "\n ");
$extrainfo = $extrainfo[0];
update_post_meta($post_id, '_payment_method', $extrainfo->gateway);
update_post_meta($post_id, '_order_shipping', $extrainfo->base_shipping);
update_post_meta($post_id, '_order_discount', $extrainfo->base_shipping);
update_post_meta($post_id, '_cart_discount', $extrainfo->base_shipping);
update_post_meta($post_id, '_order_tax', $extrainfo->base_shipping);
update_post_meta($post_id, '_order_shipping_tax', $extrainfo->base_shipping);
update_post_meta($post_id, '_order_total', $extrainfo->totalprice);
update_post_meta($post_id, '_order_key', uniqid('order_'));
update_post_meta($post_id, '_order_currency', "EUR");
update_post_meta($post_id, '_prices_include_tax', "no");
// order date
wp_update_post(array('ID' => $post_id, 'post_date' => date_i18n('Y-m-d H:i:s', $extrainfo->date), 'post_date_gmt' => date_i18n('Y-m-d H:i:s', $extrainfo->date, true)));
// order status
switch ($order['processed']) {
case "1":
$status = 'failed';
break;
case "2":
$status = 'pending';
break;
case "3":
case "4":
$status = 'processing';
break;
case "5":
$status = 'completed';
//.........这里部分代码省略.........
示例11: update_cart_by_woocart
public function update_cart_by_woocart($order_id, $data)
{
global $wp;
global $wpdb, $woocommerce, $pwa;
$xml = simplexml_load_string($data);
$order = new WC_Order($order_id);
$billing_address = array('first_name' => (string) $xml->ProcessedOrder->BuyerInfo->BuyerName, 'last_name' => '', 'company' => '', 'email' => (string) $xml->ProcessedOrder->BuyerInfo->BuyerEmailAddress, 'phone' => '', 'address_1' => '', 'address_2' => '', 'city' => '', 'state' => '', 'postcode' => '', 'country' => '');
$shipping_address = array('first_name' => (string) $xml->ProcessedOrder->ShippingAddress->Name, 'last_name' => '', 'company' => '', 'email' => '', 'phone' => '', 'address_1' => (string) $xml->ProcessedOrder->ShippingAddress->AddressFieldOne, 'address_2' => (string) $xml->ProcessedOrder->ShippingAddress->AddressFieldTwo, 'city' => (string) $xml->ProcessedOrder->ShippingAddress->City, 'state' => (string) $xml->ProcessedOrder->ShippingAddress->State, 'postcode' => (string) $xml->ProcessedOrder->ShippingAddress->PostalCode, 'country' => (string) $xml->ProcessedOrder->ShippingAddress->CountryCode);
$order->set_address($shipping_address, 'shipping');
add_post_meta($order_id, '_payment_method', 'pwa');
add_post_meta($order_id, '_payment_method_title', 'Pay with Amazon');
$total_amount = 0;
$subtotal_amount = 0;
$shipping_amount = 0;
$ClientRequestId = 0;
try {
foreach ($xml->ProcessedOrder->ProcessedOrderItems->ProcessedOrderItem as $item) {
// XML DATA
$ClientRequestId = (int) $item->ClientRequestId;
foreach ($item->ItemCharges->Component as $amount_type) {
$item_charge_type = (string) $amount_type->Type;
if ($item_charge_type == 'Shipping') {
$Shipping = (string) $amount_type->Charge->Amount;
}
}
$shipping_amount = $shipping_amount + $Shipping;
}
} catch (Exception $e) {
$param['message'] = 'IOPN Notifications : Caught exception : ' . $e->getMessage() . '.';
$this->generate_log($param);
}
if ($ClientRequestId == 0) {
$order->set_address($billing_address, 'billing');
}
// CART DATA
$cartdata = '';
$user_id = 0;
$prefix = $wpdb->prefix;
$carts = $wpdb->get_results("SELECT * FROM `" . $prefix . "pwa_before_cart_save` WHERE id = {$ClientRequestId} ");
foreach ($carts as $key => $value) {
$cartdata = maybe_unserialize($value->cart_data);
$user_id = $value->user_id;
}
update_post_meta($order_id, '_customer_user', $user_id);
// ENTRY
try {
foreach ($cartdata->cart_contents as $key => $value) {
$product_id = $value['product_id'];
$cart_product = get_product($product_id);
$product = array();
$product['order_item_name'] = $cart_product->get_title();
$product['order_item_type'] = 'line_item';
$order_item_id = wc_add_order_item($order_id, $product);
wc_add_order_item_meta($order_item_id, '_qty', $value['quantity']);
wc_add_order_item_meta($order_item_id, '_product_id', $product_id);
wc_add_order_item_meta($order_item_id, '_line_total', $value['line_total']);
wc_add_order_item_meta($order_item_id, '_line_subtotal', $value['line_subtotal']);
wc_add_order_item_meta($order_item_id, '_line_tax', $value['line_tax']);
wc_add_order_item_meta($order_item_id, '_line_subtotal_tax', $value['line_subtotal_tax']);
wc_add_order_item_meta($order_item_id, '_line_tax_data', maybe_serialize($value['line_tax_data']));
foreach ($value['line_tax_data']['total'] as $tax_rate_id => $tax_data) {
$tax_class = $wpdb->get_results("SELECT * FROM `" . $prefix . "woocommerce_tax_rates` WHERE tax_rate_id = {$tax_rate_id} ");
wc_add_order_item_meta($order_item_id, '_tax_class', $tax_class[0]->tax_rate_class);
}
if ($value['variation_id'] > 0) {
wc_add_order_item_meta($order_item_id, '_variation_id', $value['variation_id']);
}
foreach ($value['variation'] as $attrib_key => $attrib_value) {
$meta_key = str_replace('attribute_', '', $attrib_key);
wc_add_order_item_meta($order_item_id, $meta_key, $attrib_value);
}
$this->reduce_order_stock($product_id, $value['quantity']);
$total_amount = $total_amount + $value['line_total'] + $value['line_tax'];
$subtotal_amount = $subtotal_amount + $value['line_subtotal'];
}
} catch (Exception $e) {
$param['message'] = 'IOPN Notifications : Caught exception : ' . $e->getMessage() . '.';
$this->generate_log($param);
}
add_post_meta($order_id, '_order_total', $total_amount + $shipping_amount);
add_post_meta($order_id, '_order_shipping', $shipping_amount);
add_post_meta($order_id, '_cart_discount', $cartdata->discount_cart);
add_post_meta($order_id, '_cart_discount_tax', $cartdata->discount_cart_tax);
add_post_meta($order_id, '_order_tax', $cartdata->tax_total);
$shipitem = array();
$shipitem['order_item_name'] = (string) $xml->ProcessedOrder->ShippingServiceLevel;
$shipitem['order_item_type'] = 'shipping';
$order_shipping_id = wc_add_order_item($order_id, $shipitem);
wc_add_order_item_meta($order_shipping_id, 'method_id', str_replace(' ', '_', strtolower((string) $xml->ProcessedOrder->ShippingServiceLevel)));
wc_add_order_item_meta($order_shipping_id, 'cost', $shipping_amount);
if (!empty($cartdata->taxes)) {
foreach ($cartdata->taxes as $key => $value) {
$order->add_tax($key, $value);
}
}
if (!empty($cartdata->applied_coupons)) {
foreach ($cartdata->applied_coupons as $key => $value) {
$order->add_coupon($value, $cartdata->coupon_discount_amounts[$value], $cartdata->coupon_discount_tax_amounts[$value]);
}
}
//.........这里部分代码省略.........
示例12: _import_line_items
//.........这里部分代码省略.........
// Manually import product order data and do not try to match to existing products
// Manually import product order data and do not try to match to existing products
default:
$is_product_founded = true;
foreach ($this->data['pmwi_order']['manual_products'][$index] as $productIndex => $productItem) {
$item_price = $productItem['price_per_unit'];
$item_qty = empty($productItem['qty']) ? 1 : $productItem['qty'];
$item_subtotal = $item_price * $item_qty;
$item_subtotal_tax = 0;
$line_taxes = array();
foreach ($productItem['tax_rates'] as $key => $tax_rate) {
if (empty($tax_rate['code'])) {
continue;
}
$line_tax = 0;
switch ($tax_rate['calculate_logic']) {
case 'percentage':
if (!empty($tax_rate['percentage_value']) and is_numeric($tax_rate['percentage_value'])) {
$line_tax = WC_Tax::round($item_subtotal / 100 * $tax_rate['percentage_value']);
$item_subtotal_tax += $line_tax;
}
break;
case 'per_unit':
if (!empty($tax_rate['amount_per_unit']) and is_numeric($tax_rate['amount_per_unit'])) {
$line_tax = WC_Tax::round($tax_rate['amount_per_unit'] * $item_qty);
$item_subtotal_tax += $line_tax;
}
break;
// Look up tax rate code
// Look up tax rate code
default:
$found_rates = WC_Tax::get_rates_for_tax_class($tax_rate['code']);
if (!empty($found_rates)) {
$matched_tax_rates = array();
$found_priority = array();
foreach ($found_rates as $found_rate) {
if (in_array($found_rate->tax_rate_priority, $found_priority)) {
continue;
}
$matched_tax_rates[$found_rate->tax_rate_id] = array('rate' => $found_rate->tax_rate, 'label' => $found_rate->tax_rate_name, 'shipping' => $found_rate->tax_rate_shipping ? 'yes' : 'no', 'compound' => $found_rate->tax_rate_compound ? 'yes' : 'no');
$found_priority[] = $found_rate->tax_rate_priority;
}
$line_tax = array_sum(WC_Tax::calc_tax($item_subtotal, $matched_tax_rates, true));
$item_subtotal_tax += $line_tax;
}
break;
}
if (!empty($this->tax_rates)) {
foreach ($this->tax_rates as $rate_id => $rate) {
$line_taxes[$rate->tax_rate_id] = $line_tax;
break;
}
}
}
$variation = array();
$product_item = new PMXI_Post_Record();
$product_item->getBy(array('import_id' => $this->import->id, 'post_id' => $order_id, 'unique_key' => 'manual-line-item-' . $productIndex . '-' . $productItem['sku']));
if ($product_item->isEmpty()) {
$item_id = wc_add_order_item($order_id, array('order_item_name' => $productItem['sku'], 'order_item_type' => 'line_item'));
if (!$item_id) {
$this->logger and call_user_func($this->logger, __('- <b>WARNING</b> Unable to create order line product.', 'wp_all_import_plugin'));
} else {
wc_add_order_item_meta($item_id, '_qty', wc_stock_amount($item_qty));
wc_add_order_item_meta($item_id, '_tax_class', '');
wc_add_order_item_meta($item_id, '_line_subtotal', wc_format_decimal($item_subtotal));
wc_add_order_item_meta($item_id, '_line_total', wc_format_decimal($item_subtotal));
wc_add_order_item_meta($item_id, '_line_subtotal_tax', wc_format_decimal($item_subtotal_tax));
wc_add_order_item_meta($item_id, '_line_tax', wc_format_decimal($item_subtotal_tax));
wc_add_order_item_meta($item_id, '_line_tax_data', array('total' => $line_taxes, 'subtotal' => array()));
if (!empty($productItem['meta_name'])) {
foreach ($productItem['meta_name'] as $key => $meta_name) {
wc_add_order_item_meta($item_id, $meta_name, isset($productItem['meta_value'][$key]) ? $productItem['meta_value'][$key] : '');
}
}
$product_item->set(array('import_id' => $this->import->id, 'post_id' => $order_id, 'unique_key' => 'manual-line-item-' . $productIndex . '-' . $productItem['sku'], 'product_key' => 'manual-line-item-' . $item_id, 'iteration' => $this->import->iteration))->save();
}
} else {
$item_id = str_replace('manual-line-item-', '', $product_item->product_key);
if (is_numeric($item_id)) {
wc_update_order_item($item_id, array('order_item_name' => $productItem['sku'], 'order_item_type' => 'line_item'));
wc_update_order_item_meta($item_id, '_qty', wc_stock_amount($item_qty));
wc_update_order_item_meta($item_id, '_tax_class', '');
wc_update_order_item_meta($item_id, '_line_subtotal', wc_format_decimal($item_subtotal));
wc_update_order_item_meta($item_id, '_line_total', wc_format_decimal($item_subtotal));
wc_update_order_item_meta($item_id, '_line_subtotal_tax', wc_format_decimal($item_subtotal_tax));
wc_update_order_item_meta($item_id, '_line_tax', wc_format_decimal($item_subtotal_tax));
wc_update_order_item_meta($item_id, '_line_tax_data', array('total' => $line_taxes, 'subtotal' => array()));
if (!empty($productItem['meta_name'])) {
foreach ($productItem['meta_name'] as $key => $meta_name) {
wc_update_order_item_meta($item_id, $meta_name, isset($productItem['meta_value'][$key]) ? $productItem['meta_value'][$key] : '');
}
}
$product_item->set(array('iteration' => $this->import->iteration))->save();
}
}
}
break;
}
return $is_product_founded;
}
示例13: wcs_create_order_from_subscription
/**
* Function to create an order from a subscription. It can be used for a renewal or for a resubscribe
* order creation. It is the common in both of those instances.
*
* @param WC_Subscription|int $subscription Subscription we're basing the order off of
* @param string $type Type of new order. Default values are 'renewal_order'|'resubscribe_order'
* @return WC_Order New order
*/
function wcs_create_order_from_subscription($subscription, $type)
{
$type = wcs_validate_new_order_type($type);
if (is_wp_error($type)) {
return $type;
}
global $wpdb;
try {
$wpdb->query('START TRANSACTION');
if (!is_object($subscription)) {
$subscription = wcs_get_subscription($subscription);
}
$new_order = wc_create_order(array('customer_id' => $subscription->get_user_id(), 'customer_note' => $subscription->customer_note));
$new_order->post->post_title = wcs_get_new_order_title($type);
wcs_copy_order_meta($subscription, $new_order, $type);
// Copy over line items and allow extensions to add/remove items or item meta
$items = apply_filters('wcs_new_order_items', $subscription->get_items(array('line_item', 'fee', 'shipping', 'tax')), $new_order, $subscription);
$items = apply_filters('wcs_' . $type . '_items', $items, $new_order, $subscription);
foreach ($items as $item_index => $item) {
$item_name = apply_filters('wcs_new_order_item_name', $item['name'], $item, $subscription);
$item_name = apply_filters('wcs_' . $type . '_item_name', $item_name, $item, $subscription);
// Create order line item on the renewal order
$recurring_item_id = wc_add_order_item($new_order->id, array('order_item_name' => $item_name, 'order_item_type' => $item['type']));
// Remove recurring line items and set item totals based on recurring line totals
foreach ($item['item_meta'] as $meta_key => $meta_values) {
foreach ($meta_values as $meta_value) {
wc_add_order_item_meta($recurring_item_id, $meta_key, maybe_unserialize($meta_value));
}
}
}
// If we got here, the subscription was created without problems
$wpdb->query('COMMIT');
return apply_filters('wcs_new_order_created', $new_order, $subscription);
} catch (Exception $e) {
// There was an error adding the subscription
$wpdb->query('ROLLBACK');
return new WP_Error('new-order-error', $e->getMessage());
}
}
示例14: update_cart_by_xml
public function update_cart_by_xml($order_id, $orderdetail)
{
global $wpdb, $woocommerce;
$AmazonOrderID = (string) $orderdetail->OrderReport->AmazonOrderID;
$order = new WC_Order($order_id);
$billing_address = array('first_name' => (string) $orderdetail->OrderReport->BillingData->BuyerName, 'last_name' => '', 'company' => '', 'email' => (string) $orderdetail->OrderReport->BillingData->BuyerEmailAddress, 'phone' => (string) $orderdetail->OrderReport->BillingData->BuyerPhoneNumber, 'address_1' => '', 'address_2' => '', 'city' => '', 'state' => '', 'postcode' => '', 'country' => '');
$shipping_address = array('first_name' => (string) $orderdetail->OrderReport->FulfillmentData->Address->Name, 'last_name' => '', 'company' => '', 'email' => '', 'phone' => (string) $orderdetail->OrderReport->FulfillmentData->Address->PhoneNumber, 'address_1' => (string) $orderdetail->OrderReport->FulfillmentData->Address->AddressFieldOne, 'address_2' => (string) $orderdetail->OrderReport->FulfillmentData->Address->AddressFieldTwo, 'city' => (string) $orderdetail->OrderReport->FulfillmentData->Address->City, 'state' => (string) $orderdetail->OrderReport->FulfillmentData->Address->State, 'postcode' => (string) $orderdetail->OrderReport->FulfillmentData->Address->PostalCode, 'country' => (string) $orderdetail->OrderReport->FulfillmentData->Address->CountryCode);
$order->set_address($shipping_address, 'shipping');
add_post_meta($order_id, '_payment_method', 'pwa');
add_post_meta($order_id, '_payment_method_title', 'Pay with Amazon');
$total_amount = 0;
$shipping_amount = 0;
$total_promo = 0;
foreach ($orderdetail->OrderReport->Item as $item) {
$SKU = (string) $item->SKU;
$Title = (string) $item->Title;
$Quantity = (int) $item->Quantity;
foreach ($item->ItemPrice->Component as $amount_type) {
$item_charge_type = (string) $amount_type->Type;
if ($item_charge_type == 'Principal') {
$Principal = (double) $amount_type->Amount;
}
if ($item_charge_type == 'Shipping') {
$Shipping = (double) $amount_type->Amount;
}
if ($item_charge_type == 'Tax') {
$Tax = (double) $amount_type->Amount;
}
if ($item_charge_type == 'ShippingTax') {
$ShippingTax = (double) $amount_type->Amount;
}
}
if (!empty($item->Promotion)) {
foreach ($item->Promotion->Component as $promotion_amount_type) {
$promotion_type = (string) $promotion_amount_type->Type;
if ($promotion_type == 'Shipping') {
$Shipping_Promotions = (double) $promotion_amount_type->Amount;
}
if ($promotion_type == 'Principal') {
$Principal_Promotions = (double) $promotion_amount_type->Amount;
}
}
}
$product = array();
$product['order_item_name'] = $Title;
$product['order_item_type'] = 'line_item';
$order_item_id = wc_add_order_item($order_id, $product);
wc_add_order_item_meta($order_item_id, '_qty', $Quantity);
wc_add_order_item_meta($order_item_id, '_line_total', $Principal + $Shipping_Promotions + $Principal_Promotions);
wc_add_order_item_meta($order_item_id, '_line_subtotal', $Principal);
wc_add_order_item_meta($order_item_id, '_line_subtotal_tax', 0);
wc_add_order_item_meta($order_item_id, '_line_tax', 0);
/*
* Total Item Charge = (Principal - PrincipalPromo) + (Shipping - ShippingPromo) + Tax + ShippingTax
*/
$total_amount += $Principal + $Principal_Promotions + ($Shipping + $Shipping_Promotions);
$shipping_amount += $Shipping + $Shipping_Promotions;
$total_promo += $Principal_Promotions + $Shipping_Promotions;
$ClientRequestId = 0;
foreach ($item->CustomizationInfo as $info) {
$info_type = (string) $info->Type;
if ($info_type == 'url') {
$info_array = explode(',', $info->Data);
$customerId_array = explode('=', $info_array[0]);
$ClientRequestId = $customerId_array[1];
}
}
if ($ClientRequestId == 0) {
$order->set_address($billing_address, 'billing');
} else {
if (UPDATE_ODER_FROM == 'xmlcart') {
unset($billing_address['email']);
$order->set_address($billing_address, 'billing');
update_post_meta($order_id, '_customer_user', $ClientRequestId);
}
//update_post_meta($order_id, '_customer_user' , $ClientRequestId);
}
$product = $wpdb->get_results("select post_id from {$wpdb->postmeta} where meta_key = '_sku' and meta_value = '{$SKU}' ");
if (!empty($product)) {
$product_id = $product[0]->post_id;
if ($product_id != '') {
wc_add_order_item_meta($order_item_id, '_product_id', $product_id);
$this->reduce_order_stock($product_id, $Quantity);
}
}
}
add_post_meta($order_id, '_order_total', $total_amount);
add_post_meta($order_id, '_order_shipping', $shipping_amount);
add_post_meta($order_id, '_cart_discount', abs($total_promo));
$shipitem = array();
$shipitem['order_item_name'] = (string) $orderdetail->OrderReport->FulfillmentData->FulfillmentServiceLevel;
$shipitem['order_item_type'] = 'shipping';
$order_shipping_id = wc_add_order_item($order_id, $shipitem);
wc_add_order_item_meta($order_shipping_id, 'method_id', str_replace(' ', '_', strtolower((string) $orderdetail->OrderReport->FulfillmentData->FulfillmentServiceLevel)));
wc_add_order_item_meta($order_shipping_id, 'cost', $shipping_amount);
// Send notification mails to seller and customer for order
//$mail_class = new WC_Emails();
//$mail_class->emails['WC_Email_New_Order']->trigger($order_id);
// Acknowledge the order in seller central using MWS FEED API
$param['AmazonOrderID'] = $AmazonOrderID;
//.........这里部分代码省略.........
示例15: dokan_create_seller_order
/**
* 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
}