/** * create new order items and related entities * @param array $orderItem */ private function createOrderItems($orderItem) { $order = $this->getRepository('MarelloOrderBundle:Order')->createQueryBuilder('o')->where('o.orderNumber LIKE :orderId')->setParameter('orderId', $orderItem['order_number'])->getQuery()->getSingleResult(); $productResult = $this->getRepository('MarelloProductBundle:Product')->findBySku($orderItem['sku']); if (is_array($productResult)) { /** @var \Marello\Bundle\ProductBundle\Entity\Product $product */ $product = array_shift($productResult); } $itemEntity = new OrderItem(); $itemEntity->setProduct($product); $itemEntity->setOrder($order); $itemEntity->setQuantity($orderItem['qty']); $itemEntity->setPrice($orderItem['price']); $itemEntity->setTotalPrice($orderItem['total_price']); $itemEntity->setTax($orderItem['tax']); $order->addItem($itemEntity); // accumulate the totals for order $subtotal = $order->getSubtotal() + $itemEntity->getTotalPrice(); $tax = $order->getTotalTax() + $itemEntity->getTax(); $total = $order->getGrandTotal() + $itemEntity->getTotalPrice(); $order->setSubtotal($subtotal)->setTotalTax($tax)->setGrandTotal($total); $this->manager->persist($order); }