/**
  * Creates an instance of order product
  *
  * @param array          $productValues
  * @param OrderInterface $order
  *
  * @return \WellCommerce\Bundle\OrderBundle\Entity\OrderProductInterface
  */
 protected function createOrderProduct(array $productValues, OrderInterface $order)
 {
     $productId = (int) $productValues['product_id'];
     $product = $this->productRepository->find($productId);
     if (!$product instanceof ProductInterface) {
         throw new \InvalidArgumentException(sprintf('Cannot add product to order. ID "%s" does not exists.', $productId));
     }
     $orderProduct = $this->factory->create();
     $orderProduct->setBuyPrice($product->getBuyPrice());
     $orderProduct->setOrder($order);
     $orderProduct->setProduct($product);
     $orderProduct->setProductAttribute(null);
     $orderProduct->setQuantity($productValues['quantity']);
     $orderProduct->setWeight($productValues['weight']);
     $sellPrice = new Price();
     $grossAmount = $productValues['gross_amount'];
     $taxRate = $product->getSellPriceTax()->getValue();
     $netAmount = TaxHelper::calculateNetPrice($grossAmount, $taxRate);
     $sellPrice->setTaxRate($taxRate);
     $sellPrice->setTaxAmount($grossAmount - $netAmount);
     $sellPrice->setNetAmount($netAmount);
     $sellPrice->setGrossAmount($grossAmount);
     $sellPrice->setCurrency($order->getCurrency());
     $orderProduct->setSellPrice($sellPrice);
     return $orderProduct;
 }
Esempio n. 2
0
 /**
  * Adds all products to order
  *
  * @param CartInterface  $cart
  * @param OrderInterface $order
  */
 protected function prepareOrderProducts(CartInterface $cart, OrderInterface $order)
 {
     $cart->getProducts()->map(function (CartProductInterface $cartProduct) use($order) {
         $orderProduct = $this->orderProductFactory->createFromCartProduct($cartProduct);
         $orderProduct->setOrder($order);
         $order->addProduct($orderProduct);
     });
 }