Since: 2.7.0
Author: WooThemes
Inheritance: extends WC_Order_Item
Exemplo n.º 1
1
 /**
  * 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 coupon code to the order.
  * @param string $code
  * @param int $discount tax amount.
  * @param int $discount_tax amount.
  * @return int order item ID
  */
 public function add_coupon($code = array(), $discount = 0, $discount_tax = 0)
 {
     _deprecated_function('WC_Order::add_coupon', '2.7', 'Create new WC_Order_Item_Coupon object and add to order with WC_Order::add_item()');
     $item = new WC_Order_Item_Coupon(array('code' => $code, 'discount' => $discount, 'discount_tax' => $discount_tax, 'order_id' => $this->get_id()));
     $item->save();
     $this->add_item($item);
     wc_do_deprecated_action('woocommerce_order_add_coupon', array($this->get_id(), $item->get_id(), $code, $discount, $discount_tax), '2.7', 'Use woocommerce_new_order_item action instead.');
     return $item->get_id();
 }
 /**
  * Create or update an order coupon
  *
  * @since 2.2
  * @param \WC_Order $order
  * @param array $coupon item data
  * @param string $action 'create' to add coupon or 'update' to update it
  * @throws WC_API_Exception invalid data, server error
  */
 protected function set_coupon($order, $coupon, $action)
 {
     // coupon amount must be positive float
     if (isset($coupon['amount']) && floatval($coupon['amount']) < 0) {
         throw new WC_API_Exception('woocommerce_invalid_coupon_total', __('Coupon discount total must be a positive amount', 'woocommerce'), 400);
     }
     if ('create' === $action) {
         // coupon code is required
         if (empty($coupon['code'])) {
             throw new WC_API_Exception('woocommerce_invalid_coupon_coupon', __('Coupon code is required', 'woocommerce'), 400);
         }
         $item = new WC_Order_Item_Coupon(array('code' => $coupon['code'], 'discount' => isset($coupon['amount']) ? floatval($coupon['amount']) : 0, 'discount_tax' => 0, 'order_id' => $order->get_id()));
         $coupon_id = $item->save();
         if (!$coupon_id) {
             throw new WC_API_Exception('woocommerce_cannot_create_order_coupon', __('Cannot create coupon, try again', 'woocommerce'), 500);
         }
     } else {
         $item = new WC_Order_Item_Coupon($coupon['id']);
         if (isset($coupon['code'])) {
             $item->set_code($coupon['code']);
         }
         if (isset($coupon['amount'])) {
             $item->set_discount(floatval($coupon['amount']));
         }
         $coupon_id = $item->save();
         if (!$coupon_id) {
             throw new WC_API_Exception('woocommerce_cannot_update_order_coupon', __('Cannot update coupon, try again', 'woocommerce'), 500);
         }
     }
 }
Exemplo n.º 4
0
 /**
  * Add coupon lines to the order.
  *
  * @param  WC_Order $order
  */
 protected function create_order_coupon_lines(&$order)
 {
     foreach (WC()->cart->get_coupons() as $code => $coupon) {
         $item = new WC_Order_Item_Coupon();
         $item->set_props(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);
     }
 }