public function create(Discount $discount) { // Set create authorship data if not already set if (!$discount->authorship->createdAt()) { $discount->authorship->create(new DateTimeImmutable(), $this->_currentUser->id); } $result = $this->_query->run(' INSERT INTO order_discount SET order_id = :orderID?i, created_at = :createdAt?d, created_by = :createdBy?in, `type` = :type?sn, code = :code?sn, amount = :amount?f, percentage = :percentage?fn, `name` = :name?sn, description = :description?sn ', array('orderID' => $discount->order->id, 'createdAt' => $discount->authorship->createdAt(), 'createdBy' => $discount->authorship->createdBy(), 'type' => $discount->getType(), 'code' => $discount->code, 'amount' => $discount->amount, 'percentage' => $discount->percentage, 'name' => $discount->name, 'description' => $discount->description)); if ($this->_query instanceof DB\Transaction) { return $discount; } return $this->_loader->getByID($result->id(), $discount->order); }
/** * 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; }