/** * 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); }
/** * 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); } }
/** * 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_name(), 'tax_class' => $product->get_tax_class(), 'product_id' => $product->is_type('variation') ? $product->get_parent_id() : $product->get_id(), 'variation_id' => $product->is_type('variation') ? $product->get_id() : 0, 'variation' => $product->is_type('variation') ? $product->get_attributes() : array(), 'subtotal' => wc_get_price_excluding_tax($product, array('qty' => $qty)), 'total' => wc_get_price_excluding_tax($product, array('qty' => $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(); $item->set_props($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(); }