/** * Create an order using Cart cookie * @param boolean $changed */ private function getOrder(&$changed) { $request = $this->container->get('request_stack')->getCurrentRequest(); $em = $this->container->get('doctrine.orm.entity_manager'); $cookieName = $this->container->getParameter('shop.shop_cart'); $cart = array(); if ($request->cookies->has($cookieName)) { $cart = json_decode(base64_decode($request->cookies->get($cookieName)), true); } $ids = array_keys($cart); $products = array(); if (!empty($ids)) { $qb = $em->getRepository('CetenCetenBundle:Product')->createQueryBuilder('p'); $products = $qb->where($qb->expr()->in('p.id', $ids))->getQuery()->getResult(); } $shopOrder = new ShopOrder(); foreach ($products as $product) { $order = new ProductOrder(); $count = $cart[$product->getId()]; if ($count > $product->getStock()) { $count = $product->getStock(); $changed = true; } if ($count > $this->container->getParameter('shop.max_order')) { $count = $this->container->getParameter('shop.max_order'); $changed = true; } $order->setCount($count); $order->setProduct($product); if (!$count) { continue; } $shopOrder->addOrder($order); } return $shopOrder; }
/** * Add orders * * @param \Ceten\CetenBundle\Entity\ProductOrder $orders * @return ShopOrder */ public function addOrder(ProductOrder $orders) { $this->orders[] = $orders; $orders->setOrder($this); return $this; }