本文整理汇总了PHP中woocommerce_add_order_item函数的典型用法代码示例。如果您正苦于以下问题:PHP woocommerce_add_order_item函数的具体用法?PHP woocommerce_add_order_item怎么用?PHP woocommerce_add_order_item使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了woocommerce_add_order_item函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create_order
/**
* create_order function.
*
* @access public
* @return void
*/
public function create_order()
{
global $woocommerce, $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 ($woocommerce->session->order_awaiting_payment > 0) {
$order_id = absint($woocommerce->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);
if (is_wp_error($order_id)) {
throw new MyException('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]);
// User
if ($this->customer_id && !empty($this->posted[$key])) {
update_user_meta($this->customer_id, $key, $this->posted[$key]);
// Special fields
switch ($key) {
case "billing_email":
if (!email_exists($this->posted[$key])) {
wp_update_user(array('ID' => $this->customer_id, 'user_email' => $this->posted[$key]));
}
break;
case "billing_first_name":
wp_update_user(array('ID' => $this->customer_id, 'first_name' => $this->posted[$key]));
break;
case "billing_last_name":
wp_update_user(array('ID' => $this->customer_id, 'last_name' => $this->posted[$key]));
break;
}
}
}
}
if ($this->checkout_fields['shipping'] && ($woocommerce->cart->needs_shipping() || get_option('woocommerce_require_shipping_address') == 'yes')) {
foreach ($this->checkout_fields['shipping'] as $key => $field) {
$postvalue = false;
if ($this->posted['shiptobilling']) {
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) {
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 ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
// Add line item
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => $_product->get_title(), 'order_item_type' => 'line_item'));
// Add line item meta
if ($item_id) {
woocommerce_add_order_item_meta($item_id, '_qty', apply_filters('woocommerce_stock_amount', $values['quantity']));
woocommerce_add_order_item_meta($item_id, '_tax_class', $_product->get_tax_class());
woocommerce_add_order_item_meta($item_id, '_product_id', $values['product_id']);
//.........这里部分代码省略.........
示例2: unset
$wpdb->query($wpdb->prepare("\r\n\t\t\t\tUPDATE {$wpdb->postmeta}\r\n\t\t\t\tSET meta_key = '_order_items_old'\r\n\t\t\t\tWHERE meta_key = '_order_items'\r\n\t\t\t\tAND post_id = %d\r\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("\r\n\tSELECT * FROM {$wpdb->postmeta}\r\n\tWHERE meta_key = '_order_taxes'\r\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 = woocommerce_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) {
woocommerce_add_order_item_meta($item_id, 'compound', absint(isset($order_tax['compound']) ? $order_tax['compound'] : 0));
woocommerce_add_order_item_meta($item_id, 'tax_amount', woocommerce_clean($order_tax['cart_tax']));
woocommerce_add_order_item_meta($item_id, 'shipping_tax_amount', woocommerce_clean($order_tax['shipping_tax']));
}
// Delete from DB (rename)
$wpdb->query($wpdb->prepare("\r\n\t\t\t\tUPDATE {$wpdb->postmeta}\r\n\t\t\t\tSET meta_key = '_order_taxes_old'\r\n\t\t\t\tWHERE meta_key = '_order_taxes'\r\n\t\t\t\tAND post_id = %d\r\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) {
示例3: woocommerce_etsyi_submenu_page_callback
//.........这里部分代码省略.........
$product_id[$osc_id] = $p['ID'];
}
$country_data = $oscdb->get_results("SELECT * FROM countries", ARRAY_A);
$countries_name = array();
foreach ($country_data as $cdata) {
$countries_name[$cdata['countries_name']] = $cdata;
}
if ($orders = $oscdb->get_results("SELECT * FROM orders ORDER BY orders_id", ARRAY_A)) {
foreach ($orders as $order) {
$existing_order = get_posts(array('post_type' => 'shop_order', 'posts_per_page' => 1, 'post_status' => 'any', 'meta_query' => array(array('key' => 'osc_id', 'value' => $order['orders_id']))));
if (empty($existing_order)) {
$totals = array();
if ($total_query = $oscdb->get_results("SELECT * FROM orders_total WHERE orders_id='" . $order['orders_id'] . "'", ARRAY_A)) {
foreach ($total_query as $t) {
$totals[$t['class']] = $t;
}
}
$order_key = 'order_' . wp_generate_password(13);
$data = array('post_type' => 'shop_order', 'post_date' => $order['date_purchased'], 'post_author' => $customer_id[$order['customers_id']], 'post_password' => $order_key, 'post_title' => 'Order – ' . date("M d, Y @ h:i A", strtotime($order['date_purchased'])), 'post_status' => 'publish');
$order_id = wp_insert_post($data);
$billing_namebits = explode(' ', $order['billing_name']);
$billing_firstname = $billing_namebits[0];
$billing_lastname = trim(str_replace($billing_namebits[0], '', $order['billing_name']));
$shipping_namebits = explode(' ', $order['delivery_name']);
$shipping_firstname = $shipping_namebits[0];
$shipping_lastname = trim(str_replace($shipping_namebits[0], '', $order['delivery_name']));
$meta_data = array('_billing_address_1' => $order['billing_street_address'], '_billing_address_2' => $order['billing_suburb'], '_wpas_done_all' => 1, '_billing_country' => $countries_name[$order['billing_country']]['countries_iso_code_2'], '_billing_first_name' => $billing_firstname, '_billing_last_name' => $billing_lastname, '_billing_company' => $order['billing_company'], '_billing_city' => $order['billing_city'], '_billing_state' => $order['billing_state'], '_billing_postcode' => $order['billing_postcode'], '_billing_phone' => $order['customers_telephone'], '_billing_email' => $order['customers_email_address'], '_shipping_country' => $countries_name[$order['delivery_country']]['countries_iso_code_2'], '_shipping_first_name' => $shipping_firstname, '_shipping_last_name' => $shipping_lastname, '_shipping_company' => $order['delivery_company'], '_shipping_address_1' => $order['delivery_street_address'], '_shipping_address_2' => $order['delivery_suburb'], '_shipping_city' => $order['delivery_city'], '_shipping_state' => $order['delivery_state'], '_shipping_postcode' => $order['delivery_postcode'], '_shipping_method_title' => $totals['ot_shipping']['title'], '_payment_method_title' => $order['payment_method'], '_order_shipping' => $totals['ot_shipping']['value'], '_order_discount' => $totals['ot_coupon']['value'] + $totals['ot_discount']['value'], '_order_tax' => $totals['ot_tax']['value'], '_order_shipping_tax' => 0, '_order_total' => $totals['ot_total']['value'], '_order_key' => $order_key, '_customer_user' => $customer_id[$order['customers_id']], '_order_currency' => $order['currency'], '_prices_include_tax' => 'no', 'osc_id' => $order['orders_id']);
foreach ($meta_data as $k => $v) {
update_post_meta($order_id, $k, $v);
}
$order_import_counter++;
if ($order_products = $oscdb->get_results("SELECT * FROM orders_products WHERE orders_id='" . $order['orders_id'] . "'", ARRAY_A)) {
foreach ($order_products as $product) {
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => $product['products_name'], 'order_item_type' => 'line_item'));
if ($item_id) {
$item_meta = array('_qty' => $product['products_quantity'], '_product_id' => $product_id[$product['products_id']], '_line_subtotal' => $product['final_price'] * $product['products_quantity'], '_line_total' => $product['final_price'] * $product['products_quantity']);
foreach ($item_meta as $k => $v) {
woocommerce_add_order_item_meta($item_id, $k, $v);
}
}
}
}
}
}
}
}
if ($_POST['dtype']['pages'] == 1) {
$page_import_counter = 0;
if ($information_table = $oscdb->get_results("SHOW TABLES LIKE 'information'", ARRAY_A)) {
if ($information_pages = $oscdb->get_results("SELECT * FROM information WHERE language_id=1", ARRAY_A)) {
foreach ($information_pages as $information) {
$existing_page = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE post_type='page' AND LOWER(post_title)='" . strtolower(esc_sql($information['information_title'])) . "'", ARRAY_A);
if (!$existing_page) {
$existing_page = get_posts(array('post_type' => 'page', 'posts_per_page' => 1, 'post_status' => 'any', 'meta_query' => array(array('key' => 'osc_id', 'value' => $information['information_id']))));
if (!$existing_page) {
$data = array('post_type' => 'page', 'post_title' => $information['information_title'], 'post_content' => $information['information_description'], 'post_status' => 'publish');
$page_id = wp_insert_post($data);
update_post_meta($page_id, 'osc_id', $information['information_id']);
$page_import_counter++;
}
}
}
}
} else {
echo '<p class="notice">The information (pages) table does not exist in this osCommerce installation.</p>';
}
示例4: woocommerce_add_line_tax
/**
* woocommerce_add_line_tax function.
*
* @access public
* @return void
*/
function woocommerce_add_line_tax()
{
global $woocommerce, $wpdb;
check_ajax_referer('calc-totals', 'security');
$order_id = absint($_POST['order_id']);
$order = new WC_Order($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)));
}
// Add line item
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => '', 'order_item_type' => 'tax'));
// Add line item meta
if ($item_id) {
woocommerce_add_order_item_meta($item_id, 'rate_id', '');
woocommerce_add_order_item_meta($item_id, 'label', '');
woocommerce_add_order_item_meta($item_id, 'compound', '');
woocommerce_add_order_item_meta($item_id, 'tax_amount', '');
woocommerce_add_order_item_meta($item_id, 'shipping_tax_amount', '');
}
include 'admin/post-types/writepanels/order-tax-html.php';
// Quit out
die;
}
示例5: generate_renewal_order
//.........这里部分代码省略.........
// If there are outstanding payment amounts, add them to the order, otherwise set the order details to the values of the recurring totals
if ($outstanding_balance > 0 && 'yes' == get_option(WC_Subscriptions_Admin::$option_prefix . '_add_outstanding_balance', 'no')) {
$failed_payment_multiplier = WC_Subscriptions_Order::get_failed_payment_count($original_order, $product_id);
} else {
$failed_payment_multiplier = 1;
}
// Set order totals based on recurring totals from the original order
$cart_discount = $failed_payment_multiplier * get_post_meta($original_order->id, '_order_recurring_discount_cart', true);
$order_discount = $failed_payment_multiplier * get_post_meta($original_order->id, '_order_recurring_discount_total', true);
$order_shipping_tax = $failed_payment_multiplier * get_post_meta($original_order->id, '_order_recurring_shipping_tax_total', true);
$order_shipping = $failed_payment_multiplier * get_post_meta($original_order->id, '_order_recurring_shipping_total', true);
$order_tax = $failed_payment_multiplier * get_post_meta($original_order->id, '_order_recurring_tax_total', true);
$order_total = $failed_payment_multiplier * get_post_meta($original_order->id, '_order_recurring_total', true);
update_post_meta($renewal_order_id, '_cart_discount', $cart_discount);
update_post_meta($renewal_order_id, '_order_discount', $order_discount);
update_post_meta($renewal_order_id, '_order_shipping_tax', $order_shipping_tax);
update_post_meta($renewal_order_id, '_order_shipping', $order_shipping);
update_post_meta($renewal_order_id, '_order_tax', $order_tax);
update_post_meta($renewal_order_id, '_order_total', $order_total);
update_post_meta($renewal_order_id, '_shipping_method', $original_order->recurring_shipping_method);
update_post_meta($renewal_order_id, '_shipping_method_title', $original_order->recurring_shipping_method_title);
// Apply the recurring shipping & payment methods to child renewal orders
if ('child' == $args['new_order_role']) {
update_post_meta($renewal_order_id, '_payment_method', $original_order->recurring_payment_method);
update_post_meta($renewal_order_id, '_payment_method_title', $original_order->recurring_payment_method_title);
}
}
// Set order taxes based on recurring taxes from the original order
$recurring_order_taxes = WC_Subscriptions_Order::get_recurring_taxes($original_order);
foreach ($recurring_order_taxes as $index => $recurring_order_tax) {
if (function_exists('woocommerce_update_order_item_meta')) {
// WC 2.0+
$item_ids = array();
$item_ids[] = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $recurring_order_tax['name'], 'order_item_type' => 'tax'));
// Also set recurring taxes on parent renewal orders
if ('parent' == $args['new_order_role']) {
$item_ids[] = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $recurring_order_tax['name'], 'order_item_type' => 'recurring_tax'));
}
// Add line item meta
foreach ($item_ids as $item_id) {
woocommerce_add_order_item_meta($item_id, 'compound', absint(isset($recurring_order_tax['compound']) ? $recurring_order_tax['compound'] : 0));
woocommerce_add_order_item_meta($item_id, 'tax_amount', woocommerce_clean($failed_payment_multiplier * $recurring_order_tax['tax_amount']));
woocommerce_add_order_item_meta($item_id, 'shipping_tax_amount', woocommerce_clean($failed_payment_multiplier * $recurring_order_tax['shipping_tax_amount']));
if (isset($recurring_order_tax['rate_id'])) {
woocommerce_add_order_item_meta($item_id, 'rate_id', $recurring_order_tax['rate_id']);
}
if (isset($recurring_order_tax['label'])) {
woocommerce_add_order_item_meta($item_id, 'label', $recurring_order_tax['label']);
}
}
} else {
// WC 1.x
if (isset($recurring_order_tax['cart_tax']) && $recurring_order_tax['cart_tax'] > 0) {
$recurring_order_taxes[$index]['cart_tax'] = $failed_payment_multiplier * $recurring_order_tax['cart_tax'];
} else {
$recurring_order_taxes[$index]['cart_tax'] = 0;
}
if (isset($recurring_order_tax['shipping_tax']) && $recurring_order_tax['shipping_tax'] > 0) {
$recurring_order_taxes[$index]['shipping_tax'] = $failed_payment_multiplier * $recurring_order_tax['shipping_tax'];
} else {
$recurring_order_taxes[$index]['shipping_tax'] = 0;
}
// Inefficient but keeps WC 1.x code grouped together
update_post_meta($renewal_order_id, '_order_taxes', $recurring_order_taxes);
}
}
示例6: output
/**
* Output the form
*/
public function output()
{
$this->errors = array();
$step = 1;
try {
if (!empty($_POST) && !check_admin_referer('create_booking_notification')) {
throw new Exception(__('Error - please try again', 'woocommerce-bookings'));
}
if (!empty($_POST['create_booking'])) {
$customer_id = absint($_POST['customer_id']);
$bookable_product_id = absint($_POST['bookable_product_id']);
$booking_order = wc_clean($_POST['booking_order']);
if (!$bookable_product_id) {
throw new Exception(__('Please choose a bookable product', 'woocommerce-bookings'));
}
if ($booking_order === 'existing') {
$order_id = absint($_POST['booking_order_id']);
$booking_order = $order_id;
if (!$booking_order || get_post_type($booking_order) !== 'shop_order') {
throw new Exception(__('Invalid order ID provided', 'woocommerce-bookings'));
}
}
$step++;
$product = get_product($bookable_product_id);
$booking_form = new WC_Booking_Form($product);
} elseif (!empty($_POST['create_booking_2'])) {
$customer_id = absint($_POST['customer_id']);
$bookable_product_id = absint($_POST['bookable_product_id']);
$booking_order = wc_clean($_POST['booking_order']);
$product = get_product($bookable_product_id);
$booking_form = new WC_Booking_Form($product);
$booking_data = $booking_form->get_posted_data($_POST);
$booking_cost = ($cost = $booking_form->calculate_booking_cost($_POST)) && !is_wp_error($cost) ? number_format($cost, 2, '.', '') : 0;
$create_order = false;
if ('yes' === get_option('woocommerce_prices_include_tax')) {
if (version_compare(WOOCOMMERCE_VERSION, '2.3', '<')) {
$base_tax_rates = WC_Tax::get_shop_base_rate($product->tax_class);
} else {
$base_tax_rates = WC_Tax::get_base_tax_rates($product->tax_class);
}
$base_taxes = WC_Tax::calc_tax($booking_cost, $base_tax_rates, true);
$booking_cost = round($booking_cost - array_sum($base_taxes), absint(get_option('woocommerce_price_num_decimals')));
}
// Data to go into the booking
$new_booking_data = array('user_id' => $customer_id, 'product_id' => $product->id, 'resource_id' => isset($booking_data['_resource_id']) ? $booking_data['_resource_id'] : '', 'persons' => $booking_data['_persons'], 'cost' => $booking_cost, 'start_date' => $booking_data['_start_date'], 'end_date' => $booking_data['_end_date'], 'all_day' => $booking_data['_all_day'] ? 1 : 0);
// Create order
if ($booking_order === 'new') {
$create_order = true;
$order_id = $this->create_order($booking_cost, $customer_id);
if (!$order_id) {
throw new Exception(__('Error: Could not create order', 'woocommerce-bookings'));
}
} elseif ($booking_order > 0) {
$order_id = absint($booking_order);
if (!$order_id || get_post_type($order_id) !== 'shop_order') {
throw new Exception(__('Invalid order ID provided', 'woocommerce-bookings'));
}
$order = new WC_Order($order_id);
update_post_meta($order_id, '_order_total', $order->get_total() + $booking_cost);
update_post_meta($order_id, '_booking_order', '1');
} else {
$order_id = 0;
}
if ($order_id) {
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => $product->get_title(), 'order_item_type' => 'line_item'));
if (!$item_id) {
throw new Exception(__('Error: Could not create item', 'woocommerce-bookings'));
}
// Add line item meta
woocommerce_add_order_item_meta($item_id, '_qty', 1);
woocommerce_add_order_item_meta($item_id, '_tax_class', $product->get_tax_class());
woocommerce_add_order_item_meta($item_id, '_product_id', $product->id);
woocommerce_add_order_item_meta($item_id, '_variation_id', '');
woocommerce_add_order_item_meta($item_id, '_line_subtotal', $booking_cost);
woocommerce_add_order_item_meta($item_id, '_line_total', $booking_cost);
woocommerce_add_order_item_meta($item_id, '_line_tax', 0);
woocommerce_add_order_item_meta($item_id, '_line_subtotal_tax', 0);
// We have an item id
$new_booking_data['order_item_id'] = $item_id;
// Add line item data
foreach ($booking_data as $key => $value) {
if (strpos($key, '_') !== 0) {
woocommerce_add_order_item_meta($item_id, get_wc_booking_data_label($key, $product), $value);
}
}
}
// Create the booking itself
$new_booking = get_wc_booking($new_booking_data);
$new_booking->create($create_order ? 'unpaid' : 'pending-confirmation');
wp_safe_redirect(admin_url('post.php?post=' . ($create_order ? $order_id : $new_booking->id) . '&action=edit'));
exit;
}
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
}
switch ($step) {
case 1:
//.........这里部分代码省略.........
示例7: process_orders
/**
* Create new orders based on the parsed data
*/
private function process_orders()
{
global $wpdb;
$this->imported = $this->merged = 0;
// peforming a dry run?
$dry_run = isset($_POST['dry_run']) && $_POST['dry_run'] ? true : false;
$this->log->add('---');
$this->log->add(__('Processing orders.', WC_Customer_CSV_Import_Suite::TEXT_DOMAIN));
foreach ($this->posts as $post) {
// orders with custom order order numbers can be checked for existance, otherwise there's not much we can do
if (!empty($post['order_number_formatted']) && isset($this->processed_posts[$post['order_number_formatted']])) {
$this->skipped++;
$this->log->add(sprintf(__('> Order %s already processed. Skipping.', WC_Customer_CSV_Import_Suite::TEXT_DOMAIN), $post['order_number_formatted']), true);
continue;
}
// see class-wc-checkout.php for reference
$order_data = array('post_date' => date('Y-m-d H:i:s', $post['date']), 'post_type' => 'shop_order', 'post_title' => 'Order – ' . date('F j, Y @ h:i A', $post['date']), 'post_status' => 'publish', 'ping_status' => 'closed', 'post_excerpt' => $post['order_comments'], 'post_author' => 1, 'post_password' => uniqid('order_'));
if (!$dry_run) {
// track whether download permissions need to be granted
$add_download_permissions = false;
$order_id = wp_insert_post($order_data);
if (is_wp_error($order_id)) {
$this->errored++;
$this->log->add(sprintf(__('> Error inserting %s: %s', WC_Customer_CSV_Import_Suite::TEXT_DOMAIN), $post['order_number_formatted'], $order_id->get_error_message()), true);
}
// empty update to bump up the post_modified date to today's date (otherwise it would match the post_date, which isn't quite right)
wp_update_post(array('ID' => $order_id));
// set order status
wp_set_object_terms($order_id, $post['status'], 'shop_order_status');
// handle special meta fields
update_post_meta($order_id, '_order_key', apply_filters('woocommerce_generate_order_key', uniqid('order_')));
update_post_meta($order_id, '_order_currency', get_woocommerce_currency());
// TODO: fine to use store default?
if (!SV_WC_Plugin_Compatibility::is_wc_version_gte_2_1()) {
update_post_meta($order_id, '_order_taxes', array());
// pre-2.1
}
update_post_meta($order_id, '_prices_include_tax', get_option('woocommerce_prices_include_tax'));
// add order postmeta
foreach ($post['postmeta'] as $meta) {
$meta_processed = false;
// we don't set the "download permissions granted" meta, we call the woocommerce function to take care of this for us
if (('Download Permissions Granted' == $meta['key'] || '_download_permissions_granted' == $meta['key']) && $meta['value']) {
$add_download_permissions = true;
$meta_processed = true;
}
if (!$meta_processed) {
update_post_meta($order_id, $meta['key'], $meta['value']);
}
// set the paying customer flag on the user meta if applicable
if ('_customer_user' == $meta['key'] && $meta['value'] && in_array($post['status'], array('processing', 'completed', 'refunded'))) {
update_user_meta($meta['value'], "paying_customer", 1);
}
}
// handle order items
$order_items = array();
$order_item_meta = null;
foreach ($post['order_items'] as $item) {
$product = null;
$variation_item_meta = array();
// if there's a product_id then we've already determined during parsing that this product exists
if ($item['product_id']) {
$product = get_product($item['product_id']);
// handle variations
if (($product->is_type('variable') || $product->is_type('variation') || $product->is_type('subscription_variation')) && method_exists($product, 'get_variation_id')) {
foreach ($product->get_variation_attributes() as $key => $value) {
$variation_item_meta[] = array('meta_name' => esc_attr(substr($key, 10)), 'meta_value' => $value);
// remove the leading 'attribute_' from the name to get 'pa_color' for instance
}
}
}
// order item
$order_items[] = array('order_item_name' => $product ? $product->get_title() : __('Unknown Product', WC_Customer_CSV_Import_Suite::TEXT_DOMAIN), 'order_item_type' => 'line_item');
// standard order item meta
$_order_item_meta = array('_qty' => (int) $item['qty'], '_tax_class' => '', '_product_id' => $item['product_id'], '_variation_id' => $product && method_exists($product, 'get_variation_id') ? $product->get_variation_id() : 0, '_line_subtotal' => number_format((double) $item['total'], 2, '.', ''), '_line_subtotal_tax' => 0, '_line_total' => number_format((double) $item['total'], 2, '.', ''), '_line_tax' => 0);
// add any product variation meta
foreach ($variation_item_meta as $meta) {
$_order_item_meta[$meta['meta_name']] = $meta['meta_value'];
}
// include any arbitrary order item meta
$_order_item_meta = array_merge($_order_item_meta, $item['meta']);
$order_item_meta[] = $_order_item_meta;
}
foreach ($order_items as $key => $order_item) {
$order_item_id = woocommerce_add_order_item($order_id, $order_item);
if ($order_item_id) {
foreach ($order_item_meta[$key] as $meta_key => $meta_value) {
if (strpos($meta_value, ':{i')) {
$meta_value = unserialize(stripslashes($meta_value));
//'a:1:{i:0;s:19:"2014-05-01 08:00:00";}';
}
woocommerce_add_order_item_meta($order_item_id, $meta_key, $meta_value);
}
}
}
// create the shipping order items (WC 2.1+)
foreach ($post['order_shipping'] as $order_shipping) {
//.........这里部分代码省略.........
示例8: add_order_meta
/**
* When a new order is inserted, add subscriptions related order meta.
*
* @since 1.0
*/
public static function add_order_meta($order_id, $posted)
{
global $woocommerce;
if (!WC_Subscriptions_Cart::cart_contains_subscription_renewal('child') && WC_Subscriptions_Order::order_contains_subscription($order_id)) {
// This works because the 'woocommerce_add_order_item_meta' runs before the 'woocommerce_checkout_update_order_meta' hook
// Set the recurring totals so totals display correctly on order page
update_post_meta($order_id, '_order_recurring_discount_cart', WC_Subscriptions_Cart::get_recurring_discount_cart());
update_post_meta($order_id, '_order_recurring_discount_cart_tax', WC_Subscriptions_Cart::get_recurring_discount_cart_tax());
update_post_meta($order_id, '_order_recurring_discount_total', WC_Subscriptions_Cart::get_recurring_discount_total());
update_post_meta($order_id, '_order_recurring_shipping_tax_total', WC_Subscriptions_Cart::get_recurring_shipping_tax_total());
update_post_meta($order_id, '_order_recurring_shipping_total', WC_Subscriptions_Cart::get_recurring_shipping_total());
update_post_meta($order_id, '_order_recurring_tax_total', WC_Subscriptions_Cart::get_recurring_total_tax());
update_post_meta($order_id, '_order_recurring_total', WC_Subscriptions_Cart::get_recurring_total());
// Set the recurring payment method - it starts out the same as the original by may change later
update_post_meta($order_id, '_recurring_payment_method', get_post_meta($order_id, '_payment_method', true));
update_post_meta($order_id, '_recurring_payment_method_title', get_post_meta($order_id, '_payment_method_title', true));
$order = new WC_Order($order_id);
$order_fees = $order->get_fees();
// the fee order items have already been set, we just need to to add the recurring total meta
$cart_fees = $woocommerce->cart->get_fees();
foreach ($order->get_fees() as $item_id => $order_fee) {
// Find the matching fee in the cart
foreach ($cart_fees as $fee_index => $cart_fee) {
if (sanitize_title($order_fee['name']) == $cart_fee->id) {
woocommerce_add_order_item_meta($item_id, '_recurring_line_total', wc_format_decimal($cart_fee->recurring_amount));
woocommerce_add_order_item_meta($item_id, '_recurring_line_tax', wc_format_decimal($cart_fee->recurring_tax));
unset($cart_fees[$fee_index]);
break;
}
}
}
// Get recurring taxes into same format as _order_taxes
$order_recurring_taxes = array();
foreach (WC_Subscriptions_Cart::get_recurring_taxes() as $tax_key => $tax_amount) {
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => WC_Tax::get_rate_code($tax_key), 'order_item_type' => 'recurring_tax'));
if ($item_id) {
wc_add_order_item_meta($item_id, 'rate_id', $tax_key);
wc_add_order_item_meta($item_id, 'label', WC_Tax::get_rate_label($tax_key));
wc_add_order_item_meta($item_id, 'compound', absint(WC_Tax::is_compound($tax_key) ? 1 : 0));
wc_add_order_item_meta($item_id, 'tax_amount', wc_format_decimal(isset(WC()->cart->recurring_taxes[$tax_key]) ? WC()->cart->recurring_taxes[$tax_key] : 0));
wc_add_order_item_meta($item_id, 'shipping_tax_amount', wc_format_decimal(isset(WC()->cart->recurring_shipping_taxes[$tax_key]) ? WC()->cart->recurring_shipping_taxes[$tax_key] : 0));
}
}
$payment_gateways = $woocommerce->payment_gateways->payment_gateways();
if ('yes' == get_option(WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments', 'no')) {
update_post_meta($order_id, '_wcs_requires_manual_renewal', 'true');
} elseif (isset($payment_gateways[$posted['payment_method']]) && !$payment_gateways[$posted['payment_method']]->supports('subscriptions')) {
update_post_meta($order_id, '_wcs_requires_manual_renewal', 'true');
}
$cart_item = WC_Subscriptions_Cart::cart_contains_subscription_renewal();
if (isset($cart_item['subscription_renewal']) && 'parent' == $cart_item['subscription_renewal']['role']) {
update_post_meta($order_id, '_original_order', $cart_item['subscription_renewal']['original_order']);
}
// WC 2.1+
if (!WC_Subscriptions::is_woocommerce_pre('2.1')) {
// Recurring coupons
if ($applied_coupons = $woocommerce->cart->get_coupons()) {
foreach ($applied_coupons as $code => $coupon) {
if (!isset($woocommerce->cart->recurring_coupon_discount_amounts[$code])) {
continue;
}
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => $code, 'order_item_type' => 'recurring_coupon'));
// Add line item meta
if ($item_id) {
woocommerce_add_order_item_meta($item_id, 'discount_amount', isset($woocommerce->cart->recurring_coupon_discount_amounts[$code]) ? $woocommerce->cart->recurring_coupon_discount_amounts[$code] : 0);
}
}
}
// Add recurring shipping order items
if (WC_Subscriptions_Cart::cart_contains_subscriptions_needing_shipping()) {
$packages = $woocommerce->shipping->get_packages();
$checkout = $woocommerce->checkout();
foreach ($packages as $i => $package) {
if (isset($package['rates'][$checkout->shipping_methods[$i]])) {
$method = $package['rates'][$checkout->shipping_methods[$i]];
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => $method->label, 'order_item_type' => 'recurring_shipping'));
if ($item_id) {
woocommerce_add_order_item_meta($item_id, 'method_id', $method->id);
woocommerce_add_order_item_meta($item_id, 'cost', WC_Subscriptions::format_total($method->cost));
woocommerce_add_order_item_meta($item_id, 'taxes', array_map('wc_format_decimal', $method->taxes));
do_action('woocommerce_subscriptions_add_recurring_shipping_order_item', $order_id, $item_id, $i);
}
}
}
}
// Remove shipping on original order if it was added but is not required
if (!WC_Subscriptions_Cart::charge_shipping_up_front()) {
foreach ($order->get_shipping_methods() as $order_item_id => $shipping_method) {
woocommerce_update_order_item_meta($order_item_id, 'cost', WC_Subscriptions::format_total(0));
}
}
} else {
update_post_meta($order_id, '_recurring_shipping_method', get_post_meta($order_id, '_shipping_method', true), true);
update_post_meta($order_id, '_recurring_shipping_method_title', get_post_meta($order_id, '_shipping_method_title', true), true);
}
//.........这里部分代码省略.........
示例9: ajax_update_recurring_tax
/**
* Update recurring line taxes via AJAX
* @see WC_Subscriptions_Order::calculate_recurring_line_taxes()
*
* @since 4.4
* @return JSON object with updated tax data
*/
public static function ajax_update_recurring_tax()
{
global $wpdb;
$woo_22_plus = version_compare(WOOCOMMERCE_VERSION, '2.2', '>=');
check_ajax_referer('woocommerce-subscriptions', 'security');
$order_id = absint($_POST['order_id']);
$country = strtoupper(esc_attr($_POST['country']));
// Step out of the way if the customer is not located in the US
if ($country != 'US') {
return;
}
$shipping = $_POST['shipping'];
$line_subtotal = isset($_POST['line_subtotal']) ? esc_attr($_POST['line_subtotal']) : 0;
$line_total = isset($_POST['line_total']) ? esc_attr($_POST['line_total']) : 0;
// Set up WC_WooTax_Order object
$order = self::get_order($order_id);
// We only need to instantiate a WC_Tax object if we are using WooCommerce < 2.3
if (!$woo_22_plus) {
$tax = new WC_Tax();
}
$taxes = $shipping_taxes = array();
$return = array();
$item_data = array();
$type_array = array();
$product_id = '';
if (isset($_POST['order_item_id'])) {
$product_id = woocommerce_get_order_item_meta($_POST['order_item_id'], '_product_id');
} elseif (isset($_POST['product_id'])) {
$product_id = esc_attr($_POST['product_id']);
}
if (!empty($product_id) && WC_Subscriptions_Product::is_subscription($product_id)) {
// Get product details
$product = WC_Subscriptions::get_product($product_id);
// Add product to items array
$tic = get_post_meta($product->id, 'wootax_tic', true);
$item_info = array('Index' => '', 'ItemID' => isset($_POST['order_item_id']) ? $_POST['order_item_id'] : $product_id, 'Qty' => 1, 'Price' => $line_subtotal > 0 ? $line_subtotal : $product->get_price(), 'Type' => 'cart');
if (!empty($tic) && $tic) {
$item_info['TIC'] = $tic;
}
$item_data[] = $item_info;
$type_array[$_POST['order_item_id']] = 'cart';
// Add shipping to items array
if ($shipping > 0) {
$item_data[] = array('Index' => '', 'ItemID' => WT_SHIPPING_ITEM, 'TIC' => WT_SHIPPING_TIC, 'Qty' => 1, 'Price' => $shipping, 'Type' => 'shipping');
$type_array[WT_SHIPPING_ITEM] = 'shipping';
}
// Issue Lookup request
$res = $order->do_lookup($item_data, $type_array, true);
if (is_array($res)) {
$return['recurring_shipping_tax'] = 0;
$return['recurring_line_subtotal_tax'] = 0;
$return['recurring_line_tax'] = 0;
foreach ($res as $item) {
$item_id = $item->ItemID;
$item_tax = $item->TaxAmount;
if ($item_id == WT_SHIPPING_ITEM) {
$return['recurring_shipping_tax'] += $item_tax;
} else {
$return['recurring_line_subtotal_tax'] += $item_tax;
$return['recurring_line_tax'] += $item_tax;
}
}
$taxes[WT_RATE_ID] = $return['recurring_line_tax'];
$shipping_taxes[WT_RATE_ID] = $return['recurring_shipping_tax'];
// Get tax rates
$tax_codes = array(WT_RATE_ID => apply_filters('wootax_rate_code', 'WOOTAX-RATE-DO-NOT-REMOVE'));
// 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 = 'recurring_tax' )", $order_id));
$wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = 'recurring_tax'", $order_id));
// 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'] = $woo_22_plus ? WC_Tax::get_rate_label($key) : $tax->get_rate_label($key);
$item['compound'] = $woo_22_plus ? WC_Tax::is_compound($key) : $tax->is_compound($key) ? 1 : 0;
$item['tax_amount'] = wc_round_tax_total(isset($taxes[$key]) ? $taxes[$key] : 0);
$item['shipping_tax_amount'] = wc_round_tax_total(isset($shipping_taxes[$key]) ? $shipping_taxes[$key] : 0);
if (!$item['label']) {
$item['label'] = WC()->countries->tax_or_vat();
}
// Add line item
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => $item['name'], 'order_item_type' => 'recurring_tax'));
// Add line item meta
if ($item_id) {
woocommerce_add_order_item_meta($item_id, 'rate_id', $item['rate_id']);
woocommerce_add_order_item_meta($item_id, 'label', $item['label']);
woocommerce_add_order_item_meta($item_id, 'compound', $item['compound']);
woocommerce_add_order_item_meta($item_id, 'tax_amount', $item['tax_amount']);
woocommerce_add_order_item_meta($item_id, 'shipping_tax_amount', $item['shipping_tax_amount']);
}
include plugin_dir_path(WC_Subscriptions::$plugin_file) . 'templates/admin/post-types/writepanels/order-tax-html.php';
//.........这里部分代码省略.........
示例10: output
/**
* Output the form
*/
public function output()
{
global $woocommerce;
$this->errors = array();
$step = 1;
try {
if (!empty($_POST) && !check_admin_referer('create_booking_notification')) {
throw new Exception(__('Error - please try again', 'woocommerce-bookings'));
}
if (!empty($_POST['create_booking'])) {
$customer_id = absint($_POST['customer_id']);
$bookable_product_id = absint($_POST['bookable_product_id']);
$create_order = isset($_POST['create_order']) ? 1 : 0;
if (!$bookable_product_id) {
throw new Exception(__('Please choose a bookable product', 'woocommerce-bookings'));
}
$step++;
$product = get_product($bookable_product_id);
$booking_form = new WC_Booking_Form($product);
} elseif (!empty($_POST['create_booking_2'])) {
$customer_id = absint($_POST['customer_id']);
$bookable_product_id = absint($_POST['bookable_product_id']);
$create_order = absint($_POST['create_order']);
$product = get_product($bookable_product_id);
$booking_form = new WC_Booking_Form($product);
$booking_data = $booking_form->get_posted_data($_POST);
$booking_cost = number_format($booking_form->calculate_booking_cost($_POST), 2, '.', '');
// Data to go into the booking
$new_booking_data = array('product_id' => $product->id, 'resource_id' => isset($booking_data['_resource_id']) ? $booking_data['_resource_id'] : '', 'persons' => $booking_data['_persons'], 'cost' => $booking_cost, 'start_date' => $booking_data['_start_date'], 'end_date' => $booking_data['_end_date'], 'all_day' => $booking_data['_all_day'] ? 1 : 0);
// Create order
if ($create_order) {
$order_id = $this->create_order($booking_cost, $customer_id);
if (!$order_id) {
throw new Exception(__('Error: Could not create order', 'woocommerce-bookings'));
}
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => $product->get_title(), 'order_item_type' => 'line_item'));
if (!$item_id) {
throw new Exception(__('Error: Could not create item', 'woocommerce-bookings'));
}
// Add line item meta
woocommerce_add_order_item_meta($item_id, '_qty', 1);
woocommerce_add_order_item_meta($item_id, '_tax_class', $product->get_tax_class());
woocommerce_add_order_item_meta($item_id, '_product_id', $product->id);
woocommerce_add_order_item_meta($item_id, '_variation_id', '');
woocommerce_add_order_item_meta($item_id, '_line_subtotal', $booking_cost);
woocommerce_add_order_item_meta($item_id, '_line_total', $booking_cost);
woocommerce_add_order_item_meta($item_id, '_line_tax', 0);
woocommerce_add_order_item_meta($item_id, '_line_subtotal_tax', 0);
// We have an item id
$new_booking_data['order_item_id'] = $item_id;
// Add line item data
foreach ($booking_data as $key => $value) {
if (strpos($key, '_') !== 0) {
woocommerce_add_order_item_meta($item_id, get_wc_booking_data_label($key, $product), $value);
}
}
}
// Create the booking itself
$new_booking = get_wc_booking($new_booking_data);
$new_booking->create($create_order ? 'unpaid' : 'pending');
wp_safe_redirect(admin_url('post.php?post=' . ($create_order ? $order_id : $new_booking->id) . '&action=edit'));
exit;
}
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
}
switch ($step) {
case 1:
include 'views/html-create-booking-page.php';
break;
case 2:
include 'views/html-create-booking-page-2.php';
break;
}
}
示例11: upgrade_to_latest_wc
/**
* Runs necessary database updates for working with WooCommerce updates. This function should be called
* immediately after WooCommerce has run it's own updates.
*
* @since 1.2.5
*/
public static function upgrade_to_latest_wc()
{
global $wpdb;
// Update recurring tax structure to 2.0 format - item meta
if ('true' != self::$updated_to_wc_2_0) {
$upgraded_orders = $wpdb->get_col("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_order_recurring_taxes_old'");
$query = "SELECT * FROM {$wpdb->postmeta} WHERE meta_key = '_order_recurring_taxes'";
if (!empty($upgraded_orders)) {
$query .= "AND post_id NOT IN (" . implode(',', $upgraded_orders) . ")";
}
$order_tax_rows = $wpdb->get_results($query);
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 = woocommerce_add_order_item($order_tax_row->post_id, array('order_item_name' => $order_tax['label'], 'order_item_type' => 'recurring_tax'));
// Add line item meta
if ($item_id) {
woocommerce_add_order_item_meta($item_id, 'compound', absint(isset($order_tax['compound']) ? $order_tax['compound'] : 0));
woocommerce_add_order_item_meta($item_id, 'tax_amount', woocommerce_clean($order_tax['cart_tax']));
woocommerce_add_order_item_meta($item_id, 'shipping_tax_amount', woocommerce_clean($order_tax['shipping_tax']));
}
// Delete from DB (rename)
$wpdb->query($wpdb->prepare("\n\t\t\t\t\t\t\tUPDATE {$wpdb->postmeta}\n\t\t\t\t\t\t\tSET meta_key = '_order_recurring_taxes_old'\n\t\t\t\t\t\t\tWHERE meta_key = '_order_recurring_taxes'\n\t\t\t\t\t\t\tAND post_id = %d\n\t\t\t\t\t\t", $order_tax_row->post_id));
}
}
}
// Maybe add the "Variable Subscriptions" product type if it wasn't added in the v1.3 upgrade
if (!get_term_by('slug', 'variable-subscription', 'product_type')) {
wp_insert_term(__('Variable Subscription', 'woocommerce-subscriptions'), 'product_type');
}
update_option('wcs_updated_to_wc_2_0', 'true');
}
}
示例12: add_order_meta
/**
* When a new order is inserted, add subscriptions related order meta.
*
* @since 1.0
*/
public static function add_order_meta($order_id, $posted)
{
global $woocommerce;
if (!WC_Subscriptions_Cart::cart_contains_subscription_renewal('child') && WC_Subscriptions_Order::order_contains_subscription($order_id)) {
// This works because the 'woocommerce_add_order_item_meta' runs before the 'woocommerce_checkout_update_order_meta' hook
// Set the recurring totals so totals display correctly on order page
update_post_meta($order_id, '_order_recurring_discount_cart', WC_Subscriptions_Cart::get_recurring_discount_cart());
update_post_meta($order_id, '_order_recurring_discount_total', WC_Subscriptions_Cart::get_recurring_discount_total());
update_post_meta($order_id, '_order_recurring_shipping_tax_total', WC_Subscriptions_Cart::get_recurring_shipping_tax_total());
update_post_meta($order_id, '_order_recurring_tax_total', WC_Subscriptions_Cart::get_recurring_total_tax());
update_post_meta($order_id, '_order_recurring_total', WC_Subscriptions_Cart::get_recurring_total());
// Get recurring taxes into same format as _order_taxes
$order_recurring_taxes = array();
foreach (WC_Subscriptions_Cart::get_recurring_taxes() as $tax_key => $tax_amount) {
$is_compound = $woocommerce->cart->tax->is_compound($tax_key) ? 1 : 0;
if (isset($woocommerce->cart->taxes[$tax_key])) {
$cart_tax = $tax_amount;
$shipping_tax = 0;
} else {
$cart_tax = 0;
$shipping_tax = $tax_amount;
}
if (function_exists('woocommerce_add_order_item_meta')) {
// WC 2.0+
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => $woocommerce->cart->tax->get_rate_code($tax_key), 'order_item_type' => 'recurring_tax'));
if ($item_id) {
woocommerce_add_order_item_meta($item_id, 'rate_id', $tax_key);
woocommerce_add_order_item_meta($item_id, 'label', $woocommerce->cart->tax->get_rate_label($tax_key));
woocommerce_add_order_item_meta($item_id, 'compound', $is_compound);
woocommerce_add_order_item_meta($item_id, 'tax_amount', woocommerce_clean($cart_tax));
woocommerce_add_order_item_meta($item_id, 'shipping_tax_amount', woocommerce_clean($shipping_tax));
}
} else {
// WC 1.x
$order_recurring_taxes[] = array('label' => $woocommerce->cart->tax->get_rate_label($tax_key), 'compound' => $is_compound, 'cart_tax' => woocommerce_format_total($cart_tax), 'shipping_tax' => woocommerce_format_total($shipping_tax));
// Inefficient but keeps WC 1.x code grouped together
update_post_meta($order_id, '_order_recurring_taxes', $order_recurring_taxes);
}
}
$payment_gateways = $woocommerce->payment_gateways->payment_gateways();
if ('yes' == get_option(WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments', 'no')) {
update_post_meta($order_id, '_wcs_requires_manual_renewal', 'true');
} elseif (isset($payment_gateways[$posted['payment_method']]) && !$payment_gateways[$posted['payment_method']]->supports('subscriptions')) {
update_post_meta($order_id, '_wcs_requires_manual_renewal', 'true');
}
$cart_item = WC_Subscriptions_Cart::cart_contains_subscription_renewal();
if (isset($cart_item['subscription_renewal']) && 'parent' == $cart_item['subscription_renewal']['role']) {
update_post_meta($order_id, '_original_order', $cart_item['subscription_renewal']['original_order']);
}
}
}
示例13: add_line_tax
/**
* Adds a line tax item from an order by ID. Hooked to
* an Ajax call from the "Edit Order" page and mirrors the
* @see woocommerce_add_line_tax() function.
*
* @return void
*/
public static function add_line_tax()
{
global $woocommerce, $wpdb;
check_ajax_referer(WC_Subscriptions::$text_domain, 'security');
if (function_exists('woocommerce_add_order_item_meta')) {
$order_id = absint($_POST['order_id']);
$order = new WC_Order($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)));
}
// Add line item
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => '', 'order_item_type' => 'recurring_tax'));
// Add line item meta
if ($item_id) {
woocommerce_add_order_item_meta($item_id, 'rate_id', '');
woocommerce_add_order_item_meta($item_id, 'label', '');
woocommerce_add_order_item_meta($item_id, 'compound', '');
woocommerce_add_order_item_meta($item_id, 'tax_amount', '');
woocommerce_add_order_item_meta($item_id, 'shipping_tax_amount', '');
}
include plugin_dir_path(WC_Subscriptions::$plugin_file) . 'templates/admin/post-types/writepanels/order-tax-html.php';
} else {
// WC 1.x
$size = absint($_POST['size']);
?>
<div class="tax_row">
<p class="first">
<label><?php
_e('Recurring Tax Label:', WC_Subscriptions::$text_domain);
?>
</label>
<input type="text" name="_order_recurring_taxes[<?php
echo $size;
?>
][label]" placeholder="<?php
echo $woocommerce->countries->tax_or_vat();
?>
" value="" />
</p>
<p class="last">
<label><?php
_e('Compound:', WC_Subscriptions::$text_domain);
?>
<input type="checkbox" name="_order_recurring_taxes[<?php
echo $size;
?>
][compound]" /></label>
</p>
<p class="first">
<label><?php
_e('Recurring Cart Tax:', WC_Subscriptions::$text_domain);
?>
</label>
<input type="text" name="_order_recurring_taxes[<?php
echo $size;
?>
][cart_tax]" placeholder="0.00" value="" />
</p>
<p class="last">
<label><?php
_e('Shipping Tax:', WC_Subscriptions::$text_domain);
?>
</label>
<input type="text" name="_order_recurring_taxes[<?php
echo $size;
?>
][shipping_tax]" placeholder="0.00" value="" />
</p>
<a href="#" class="delete_tax_row">×</a>
<div class="clear"></div>
</div>
<?php
}
die;
}
示例14: woocommerce_ajax_add_order_item
function woocommerce_ajax_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 || $post->post_type !== 'product' && $post->post_type !== 'product_variation') {
die;
}
$_product = get_product($post->ID);
$order = new WC_Order($order_id);
$class = 'new_row';
// Set values
$item = array();
$item['product_id'] = $_product->id;
$item['variation_id'] = isset($_product->variation_id) ? $_product->variation_id : '';
$item['name'] = $_product->get_title();
$item['tax_class'] = $_product->get_tax_class();
$item['qty'] = 1;
$item['line_subtotal'] = number_format((double) $_product->get_price_excluding_tax(), 2, '.', '');
$item['line_subtotal_tax'] = '';
$item['line_total'] = number_format((double) $_product->get_price_excluding_tax(), 2, '.', '');
$item['line_tax'] = '';
$item = apply_filters('woocommerce_ajax_before_add_order_item', $item, $_product, $order);
// Add line item
$item_id = woocommerce_add_order_item($order_id, array('order_item_name' => $item['name'], 'order_item_type' => 'line_item'));
$class = apply_filters('woocommerce_admin_order_items_class', $class, $item, $order);
// Add line item meta
if ($item_id) {
woocommerce_add_order_item_meta($item_id, '_qty', $item['qty']);
woocommerce_add_order_item_meta($item_id, '_tax_class', $item['tax_class']);
woocommerce_add_order_item_meta($item_id, '_product_id', $item['product_id']);
woocommerce_add_order_item_meta($item_id, '_variation_id', $item['variation_id']);
woocommerce_add_order_item_meta($item_id, '_line_subtotal', $item['line_subtotal']);
woocommerce_add_order_item_meta($item_id, '_line_subtotal_tax', $item['line_subtotal_tax']);
woocommerce_add_order_item_meta($item_id, '_line_total', $item['line_total']);
woocommerce_add_order_item_meta($item_id, '_line_tax', $item['line_tax']);
}
do_action('woocommerce_ajax_add_order_item_meta', $item_id, $item);
//include( 'admin/post-types/writepanels/order-item-html.php' );
include apply_filters('qsot-woo-template', 'post-types/meta-boxes/views/html-order-item.php', 'admin');
// Quit out
die;
}
示例15: generate_renewal_order
//.........这里部分代码省略.........
}
// Set order totals based on recurring totals from the original order
$cart_discount = $failed_payment_multiplier * wc_format_decimal(get_post_meta($original_order->id, '_order_recurring_discount_cart', true));
$order_discount = $failed_payment_multiplier * wc_format_decimal(get_post_meta($original_order->id, '_order_recurring_discount_total', true));
$order_shipping_tax = $failed_payment_multiplier * wc_format_decimal(get_post_meta($original_order->id, '_order_recurring_shipping_tax_total', true));
$order_shipping = $failed_payment_multiplier * wc_format_decimal(get_post_meta($original_order->id, '_order_recurring_shipping_total', true));
$order_tax = $failed_payment_multiplier * wc_format_decimal(get_post_meta($original_order->id, '_order_recurring_tax_total', true));
$order_total = $failed_payment_multiplier * wc_format_decimal(get_post_meta($original_order->id, '_order_recurring_total', true));
update_post_meta($renewal_order_id, '_cart_discount', $cart_discount);
update_post_meta($renewal_order_id, '_order_discount', $order_discount);
update_post_meta($renewal_order_id, '_order_shipping_tax', $order_shipping_tax);
update_post_meta($renewal_order_id, '_order_shipping', $order_shipping);
update_post_meta($renewal_order_id, '_order_tax', $order_tax);
update_post_meta($renewal_order_id, '_order_total', $order_total);
// Set shipping for orders created with WC 2.0.n (or when we are using WC 2.0.n)
if (WC_Subscriptions::is_woocommerce_pre_2_1() || isset($original_order->recurring_shipping_method)) {
update_post_meta($renewal_order_id, '_shipping_method', $original_order->recurring_shipping_method);
update_post_meta($renewal_order_id, '_shipping_method_title', $original_order->recurring_shipping_method_title);
// Also set recurring shipping as it's a parent renewal order
if ('parent' == $args['new_order_role']) {
update_post_meta($renewal_order_id, '_recurring_shipping_method', $original_order->recurring_shipping_method);
update_post_meta($renewal_order_id, '_recurring_shipping_method_title', $original_order->recurring_shipping_method_title);
}
}
// Apply the recurring shipping & payment methods to child renewal orders
if ('child' == $args['new_order_role']) {
update_post_meta($renewal_order_id, '_payment_method', $original_order->recurring_payment_method);
update_post_meta($renewal_order_id, '_payment_method_title', $original_order->recurring_payment_method_title);
}
// Set order taxes based on recurring taxes from the original order
$recurring_order_taxes = WC_Subscriptions_Order::get_recurring_taxes($original_order);
foreach ($recurring_order_taxes as $index => $recurring_order_tax) {
$item_ids = array();
$item_ids[] = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $recurring_order_tax['name'], 'order_item_type' => 'tax'));
// Also set recurring taxes on parent renewal orders
if ('parent' == $args['new_order_role']) {
$item_ids[] = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $recurring_order_tax['name'], 'order_item_type' => 'recurring_tax'));
}
// Add line item meta
foreach ($item_ids as $item_id) {
woocommerce_add_order_item_meta($item_id, 'compound', absint(isset($recurring_order_tax['compound']) ? $recurring_order_tax['compound'] : 0));
woocommerce_add_order_item_meta($item_id, 'tax_amount', $failed_payment_multiplier * WC_Subscriptions::format_total($recurring_order_tax['tax_amount']));
woocommerce_add_order_item_meta($item_id, 'shipping_tax_amount', $failed_payment_multiplier * WC_Subscriptions::format_total($recurring_order_tax['shipping_tax_amount']));
if (isset($recurring_order_tax['rate_id'])) {
woocommerce_add_order_item_meta($item_id, 'rate_id', $recurring_order_tax['rate_id']);
}
if (isset($recurring_order_tax['label'])) {
woocommerce_add_order_item_meta($item_id, 'label', $recurring_order_tax['label']);
}
}
}
// Set up shipping items on renewal order
$recurring_shipping_items = WC_Subscriptions_Order::get_recurring_shipping_methods($original_order);
foreach ($recurring_shipping_items as $recurring_shipping_item_id => $recurring_shipping_item) {
$item_ids = array();
$item_ids[] = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $recurring_shipping_item['name'], 'order_item_type' => 'shipping'));
// Also set recurring shipping as it's a parent renewal order
if ('parent' == $args['new_order_role']) {
$item_ids[] = woocommerce_add_order_item($renewal_order_id, array('order_item_name' => $recurring_shipping_item['name'], 'order_item_type' => 'recurring_shipping'));
}
// Add shipping item meta
foreach ($item_ids as $item_id) {
woocommerce_add_order_item_meta($item_id, 'method_id', $recurring_shipping_item['method_id']);
woocommerce_add_order_item_meta($item_id, 'cost', $failed_payment_multiplier * WC_Subscriptions::format_total($recurring_shipping_item['cost']));
// Set line taxes for shipping
if (isset($recurring_shipping_item['taxes'])) {