Since: 2.7.0
Author: WooThemes
Inheritance: extends WC_Order_Item
コード例 #1
1
ファイル: crud.php プロジェクト: woocommerce/woocommerce
 /**
  * Test: get_item
  */
 function test_get_item()
 {
     $object = new WC_Order();
     $item = new WC_Order_Item_Product();
     $item->set_props(array('product' => WC_Helper_Product::create_simple_product(), 'quantity' => 4));
     $item->save();
     $object->add_item($item->get_id());
     $object->save();
     $this->assertTrue($object->get_item($item->get_id()) instanceof WC_Order_Item_Product);
     $object = new WC_Order();
     $item = new WC_Order_Item_Coupon();
     $item->set_props(array('code' => '12345', 'discount' => 10, 'discount_tax' => 5));
     $item_id = $item->save();
     $object->add_item($item);
     $object->save();
     $this->assertTrue($object->get_item($item_id) instanceof WC_Order_Item_Coupon);
 }
コード例 #2
0
 /**
  * Add a product line item to the order. This is the only line item type with
  * it's own method because it saves looking up order amounts (costs are added up for you).
  * @param  \WC_Product $product
  * @param  int $qty
  * @param  array $args
  * @return int order item ID
  * @throws WC_Data_Exception
  */
 public function add_product($product, $qty = 1, $args = array())
 {
     if ($product) {
         $default_args = array('name' => $product->get_title(), 'tax_class' => $product->get_tax_class(), 'product_id' => $product->get_id(), 'variation_id' => isset($product->variation_id) ? $product->variation_id : 0, 'variation' => isset($product->variation_id) ? $product->get_variation_attributes() : array(), 'subtotal' => $product->get_price_excluding_tax($qty), 'total' => $product->get_price_excluding_tax($qty), 'quantity' => $qty);
     } else {
         $default_args = array('quantity' => $qty);
     }
     $args = wp_parse_args($args, $default_args);
     // BW compatibility with old args
     if (isset($args['totals'])) {
         foreach ($args['totals'] as $key => $value) {
             if ('tax' === $key) {
                 $args['total_tax'] = $value;
             } elseif ('tax_data' === $key) {
                 $args['taxes'] = $value;
             } else {
                 $args[$key] = $value;
             }
         }
     }
     $item = new WC_Order_Item_Product($args);
     $item->set_backorder_meta();
     $item->set_order_id($this->get_id());
     $item->save();
     $this->add_item($item);
     wc_do_deprecated_action('woocommerce_order_add_product', array($this->get_id(), $item->get_id(), $product, $qty, $args), '2.7', 'Use woocommerce_new_order_item action instead.');
     return $item->get_id();
 }
コード例 #3
0
 /**
  * Create or update a line item
  *
  * @since 2.2
  * @param \WC_Order $order
  * @param array $item line item data
  * @param string $action 'create' to add line item or 'update' to update it
  * @throws WC_API_Exception invalid data, server error
  */
 protected function set_line_item($order, $item, $action)
 {
     $creating = 'create' === $action;
     // product is always required
     if (!isset($item['product_id']) && !isset($item['sku'])) {
         throw new WC_API_Exception('woocommerce_api_invalid_product_id', __('Product ID or SKU is required', 'woocommerce'), 400);
     }
     // when updating, ensure product ID provided matches
     if ('update' === $action) {
         $item_product_id = wc_get_order_item_meta($item['id'], '_product_id');
         $item_variation_id = wc_get_order_item_meta($item['id'], '_variation_id');
         if ($item['product_id'] != $item_product_id && $item['product_id'] != $item_variation_id) {
             throw new WC_API_Exception('woocommerce_api_invalid_product_id', __('Product ID provided does not match this line item', 'woocommerce'), 400);
         }
     }
     if (isset($item['product_id'])) {
         $product_id = $item['product_id'];
     } elseif (isset($item['sku'])) {
         $product_id = wc_get_product_id_by_sku($item['sku']);
     }
     // variations must each have a key & value
     $variation_id = 0;
     if (isset($item['variations']) && is_array($item['variations'])) {
         foreach ($item['variations'] as $key => $value) {
             if (!$key || !$value) {
                 throw new WC_API_Exception('woocommerce_api_invalid_product_variation', __('The product variation is invalid', 'woocommerce'), 400);
             }
         }
         $variation_id = $this->get_variation_id(wc_get_product($product_id), $item['variations']);
     }
     $product = wc_get_product($variation_id ? $variation_id : $product_id);
     // must be a valid WC_Product
     if (!is_object($product)) {
         throw new WC_API_Exception('woocommerce_api_invalid_product', __('Product is invalid', 'woocommerce'), 400);
     }
     // quantity must be positive float
     if (isset($item['quantity']) && floatval($item['quantity']) <= 0) {
         throw new WC_API_Exception('woocommerce_api_invalid_product_quantity', __('Product quantity must be a positive float', 'woocommerce'), 400);
     }
     // quantity is required when creating
     if ($creating && !isset($item['quantity'])) {
         throw new WC_API_Exception('woocommerce_api_invalid_product_quantity', __('Product quantity is required', 'woocommerce'), 400);
     }
     if ($creating) {
         $item = new WC_Order_Item_Product();
     } else {
         $item = new WC_Order_Item_Product($item['id']);
     }
     $item->set_product($product);
     $item->set_order_id($order->id);
     if (isset($item['quantity'])) {
         $item->set_quantity($item['quantity']);
     }
     if (isset($item['total'])) {
         $item->set_total(floatval($item['total']));
     }
     if (isset($item['total_tax'])) {
         $item->set_total_tax(floatval($item['total_tax']));
     }
     if (isset($item['subtotal'])) {
         $item->set_subtotal(floatval($item['subtotal']));
     }
     if (isset($item['subtotal_tax'])) {
         $item->set_subtotal_tax(floatval($item['subtotal_tax']));
     }
     if ($variation_id) {
         $item->set_variation_id($variation_id);
         $item->set_variation($item['variations']);
     }
     $item_id = $item->save();
     if (!$item_id) {
         throw new WC_API_Exception('woocommerce_cannot_create_line_item', __('Cannot create line item, try again', 'woocommerce'), 500);
     }
 }
コード例 #4
0
 /**
  * Add line items to the order.
  *
  * @param  WC_Order $order
  */
 protected function create_order_line_items(&$order)
 {
     foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
         $product = $values['data'];
         $item = new WC_Order_Item_Product();
         $item->set_props(array('quantity' => $values['quantity'], 'name' => $product ? $product->get_name() : '', 'tax_class' => $product ? $product->get_tax_class() : '', 'product_id' => $product ? $product->is_type('variation') ? $product->get_parent_id() : $product->get_id() : 0, 'variation_id' => $product && $product->is_type('variation') ? $product->get_id() : 0, 'variation' => $values['variation'], 'subtotal' => $values['line_subtotal'], 'total' => $values['line_total'], 'subtotal_tax' => $values['line_subtotal_tax'], 'total_tax' => $values['line_tax'], 'taxes' => $values['line_tax_data']));
         $item->set_backorder_meta();
         // Set this to pass to legacy actions.
         $item->legacy_values = $values;
         $item->legacy_cart_item_key = $cart_item_key;
         $order->add_item($item);
     }
 }
コード例 #5
0
 /**
  * Create or update a line item.
  *
  * @param array $posted Line item data.
  * @param string $action 'create' to add line item or 'update' to update it.
  * @throws WC_REST_Exception Invalid data, server error.
  */
 protected function prepare_line_items($posted, $action = 'create')
 {
     $item = new WC_Order_Item_Product(!empty($posted['id']) ? $posted['id'] : '');
     $product = wc_get_product($this->get_product_id($posted));
     if ($product !== $item->get_product()) {
         $item->set_product($product);
         if ('create' === $action) {
             $total = $product->get_price() * (isset($posted['quantity']) ? $posted['quantity'] : 1);
             $item->set_total($total);
             $item->set_subtotal($total);
         }
     }
     $this->maybe_set_item_props($item, array('name', 'quantity', 'total', 'subtotal', 'tax_class'), $posted);
     return $item;
 }
コード例 #6
0
 /**
  * Create an order. Error codes:
  * 		520 - Cannot insert order into the database.
  * 		521 - Cannot get order after creation.
  * 		522 - Cannot update order.
  * 		525 - Cannot create line item.
  * 		526 - Cannot create fee item.
  * 		527 - Cannot create shipping item.
  * 		528 - Cannot create tax item.
  * 		529 - Cannot create coupon item.
  * @throws Exception
  * @return int|WP_ERROR
  */
 public function create_order()
 {
     global $wpdb;
     // Give plugins the opportunity to create an order themselves
     if ($order_id = apply_filters('woocommerce_create_order', null, $this)) {
         return $order_id;
     }
     try {
         // Start transaction if available
         wc_transaction_query('start');
         // Insert or update the post data
         $order_id = absint(WC()->session->order_awaiting_payment);
         $cart_hash = md5(json_encode(wc_clean(WC()->cart->get_cart_for_session())) . WC()->cart->total);
         /**
          * If there is an order pending payment, we can resume it here so
          * long as it has not changed. If the order has changed, i.e.
          * different items or cost, create a new order. We use a hash to
          * detect changes which is based on cart items + order total.
          */
         if ($order_id && ($order = wc_get_order($order_id)) && $order->has_cart_hash($cart_hash) && $order->has_status(array('pending', 'failed'))) {
             // Action for 3rd parties.
             do_action('woocommerce_resume_order', $order_id);
             // Remove all items - we will re-add them later.
             $order->remove_order_items();
             /**
              * Not resuming - lets create a new order object.
              */
         } else {
             $order = new WC_Order();
         }
         $order->set_created_via('checkout');
         $order->set_cart_hash($cart_hash);
         $order->set_customer_id($this->customer_id);
         $order->set_currency(get_woocommerce_currency());
         $order->set_prices_include_tax('yes' === get_option('woocommerce_prices_include_tax'));
         $order->set_customer_ip_address(WC_Geolocation::get_ip_address());
         $order->set_customer_user_agent(wc_get_user_agent());
         $order->set_customer_note(isset($this->posted['order_comments']) ? $this->posted['order_comments'] : '');
         $order->set_payment_method($this->payment_method);
         $order->set_shipping_total(WC()->cart->shipping_total);
         $order->set_discount_total(WC()->cart->get_cart_discount_total());
         $order->set_discount_tax(WC()->cart->get_cart_discount_tax_total());
         $order->set_cart_tax(WC()->cart->tax_total);
         $order->set_shipping_tax(WC()->cart->shipping_tax_total);
         $order->set_total(WC()->cart->total);
         // Billing and shipping addresses
         if ($address_keys = array_merge(array_keys($this->checkout_fields['billing']), array_keys($this->checkout_fields['shipping']))) {
             foreach ($address_keys as $key) {
                 if (is_callable(array($order, "set_{$key}"))) {
                     $order->{"set_{$key}"}($this->get_posted_address_data(str_replace(array('billing_', 'shipping_'), '', $key), strstr($key, 'billing_') ? 'billing' : 'shipping'));
                 }
             }
         }
         // Add line items.
         foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
             $product = $values['data'];
             $item = new WC_Order_Item_Product(array('quantity' => $values['quantity'], 'name' => $product ? $product->get_title() : '', 'tax_class' => $product ? $product->get_tax_class() : '', 'product_id' => $product && isset($product->id) ? $product->id : 0, 'variation_id' => $product && isset($product->variation_id) ? $product->variation_id : 0, 'variation' => $values['variation'], 'subtotal' => $values['line_subtotal'], 'total' => $values['line_total'], 'subtotal_tax' => $values['line_subtotal_tax'], 'total_tax' => $values['line_tax'], 'taxes' => $values['line_tax_data']));
             $item->set_backorder_meta();
             // Set this to pass to legacy actions @todo remove in future release
             $item->legacy_values = $values;
             $item->legacy_cart_item_key = $cart_item_key;
             $order->add_item($item);
         }
         // Add fees
         foreach (WC()->cart->get_fees() as $fee_key => $fee) {
             $item = new WC_Order_Item_Fee(array('name' => $fee->name, 'tax_class' => $fee->taxable ? $fee->tax_class : 0, 'total' => $fee->amount, 'total_tax' => $fee->tax, 'taxes' => array('total' => $fee->tax_data)));
             // Set this to pass to legacy actions @todo remove in future release
             $item->legacy_fee = $fee;
             $item->legacy_fee_key = $fee_key;
             $order->add_item($item);
         }
         // Store shipping for all packages
         foreach (WC()->shipping->get_packages() as $package_key => $package) {
             if (isset($package['rates'][$this->shipping_methods[$package_key]])) {
                 $shipping_rate = $package['rates'][$this->shipping_methods[$package_key]];
                 $item = new WC_Order_Item_Shipping(array('method_title' => $shipping_rate->label, 'method_id' => $shipping_rate->id, 'total' => wc_format_decimal($shipping_rate->cost), 'taxes' => $shipping_rate->taxes, 'meta_data' => $shipping_rate->get_meta_data()));
                 // Set this to pass to legacy actions @todo remove in future release
                 $item->legacy_package_key = $package_key;
                 $order->add_item($item);
             }
         }
         // Store tax rows
         foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) {
             if ($tax_rate_id && apply_filters('woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated') !== $tax_rate_id) {
                 $order->add_item(new WC_Order_Item_Tax(array('rate_id' => $tax_rate_id, 'tax_total' => WC()->cart->get_tax_amount($tax_rate_id), 'shipping_tax_total' => WC()->cart->get_shipping_tax_amount($tax_rate_id), 'rate_code' => WC_Tax::get_rate_code($tax_rate_id), 'label' => WC_Tax::get_rate_label($tax_rate_id), 'compound' => WC_Tax::is_compound($tax_rate_id))));
             }
         }
         // Store coupons
         foreach (WC()->cart->get_coupons() as $code => $coupon) {
             $item = new WC_Order_Item_Coupon(array('code' => $code, 'discount' => WC()->cart->get_coupon_discount_amount($code), 'discount_tax' => WC()->cart->get_coupon_discount_tax_amount($code)));
             $order->add_item($item);
         }
         // Save the order
         $order_id = $order->save();
         // Update user meta
         $this->update_customer_data();
         // Let plugins add their own meta data
         do_action('woocommerce_checkout_update_order_meta', $order_id, $this->posted);
         // If we got here, the order was created without problems!
         wc_transaction_query('commit');
     } catch (Exception $e) {
         // There was an error adding order data!
         wc_transaction_query('rollback');
         return new WP_Error('checkout-error', $e->getMessage());
     }
     return $order_id;
 }