Example #1
0
 public function ajaxAddProduct()
 {
     try {
         /** @var \Jigoshop\Entity\Order $order */
         $order = $this->orderService->find((int) $_POST['order']);
         if ($order->getId() === null) {
             throw new Exception(__('Order not found.', 'jigoshop'));
         }
         /** @var ProductEntity|ProductEntity\Purchasable $product */
         $post = $this->wp->getPost((int) $_POST['product']);
         if ($post->post_type == 'product_variation' && $post->post_parent > 0) {
             $post = $this->wp->getPost($post->post_parent);
             //TODO: change this!!!
             $_POST['variation_id'] = (int) $_POST['product'];
             $_POST['quantity'] = 1;
         }
         /** @var Product\* $product */
         $product = $this->productService->findforPost($post);
         if ($product->getId() === null) {
             throw new Exception(__('Product not found.', 'jigoshop'));
         }
         /** @var Item $item */
         $item = $this->wp->applyFilters('jigoshop\\cart\\add', null, $product);
         if ($item === null) {
             throw new Exception(__('Product cannot be added to the order.', 'jigoshop'));
         }
         $key = $this->productService->generateItemKey($item);
         $item->setKey($key);
         $order->addItem($item);
         $this->orderService->save($order);
         $row = Render::get('admin/order/item/' . $item->getType(), array('item' => $item));
         $result = $this->getAjaxResponse($order);
         $result['html']['row'] = $row;
     } catch (Exception $e) {
         $result = array('success' => false, 'error' => $e->getMessage());
     }
     echo json_encode($result);
     exit;
 }