/** * Create a discount order entity to apply to the * * @param Order\Order $order * @param Bundle $bundle * * @return Order\Entity\Discount\Discount */ public function createOrderDiscount(Order\Order $order, Bundle $bundle) { $discount = new Order\Entity\Discount\Discount(); $type = $bundle->allowsCodes() ? self::CODES_ALLOWED : self::NO_CODES; $discount->setType($type); $discount->order = $order; $discount->amount = $this->_calculateAmount($order, $bundle); $discount->name = $bundle->getName(); $discount->description = 'Bundle ' . $bundle->getID() . ': ' . $bundle->getName(); $discount->order = $order; return $discount; }
/** * Creates order discount from $this->_order and $this->_discount * * @throws \Exception if $this->_order or $this->_discount are not set */ public function createOrderDiscount() { if ($this->_order === null) { throw new \Exception('Order must be set to create order discount!'); } if ($this->_discount === null) { throw new \Exception('Discount must be set to create order discount!'); } $orderDiscount = new Order\Entity\Discount\Discount(); $orderDiscount->setType(self::TYPE); $orderDiscount->discount = $this->_discount; $orderDiscount->code = $this->_discount->code; $orderDiscount->name = $this->_discount->name; $orderDiscount->description = $this->_discount->description; $orderDiscount->percentage = $this->_discount->percentage; $orderDiscount->order = $this->_order; // add discountAmount if it has the right locale and currencyID for the order foreach ($this->_discount->discountAmounts as $currencyID => $discountAmount) { if ($currencyID === $this->_order->currencyID) { $orderDiscount->amount = $discountAmount; } } // If this is a free shipping discount, apply it as the equivalent of // the shipping list price. if ($this->_discount->freeShipping) { $this->_order->shippingDiscount = $this->_order->shippingListPrice; } if ($this->_discount->appliesToOrder) { $orderDiscount->items = $this->_order->items; } else { foreach ($this->_order->items->all() as $item) { foreach ($this->_discount->products as $product) { if ($item->productID === $product->id) { $orderDiscount->items->append($item); continue; } } } } return $orderDiscount; }