/** * edit product * * @param $domain * @param $id * @param Request $request * @return \BladeView|bool|\Illuminate\Http\RedirectResponse|\Illuminate\View\View */ public function editProduct($domain, $id, Request $request) { $productService = new productService(); $addressService = new addressService(); $productGalleryModel = new productGalleryModel(); $tagService = new tagService(); $results = []; try { if ($request->isMethod('post')) { $productService->updateProduct($request->all(), $id); return redirect('/product/editProduct/' . $id)->with('success', 'Cập nhật sản phẩm thành công!'); } $results['product'] = $productService->getProductById($id, 0, false); $results['tagColor'] = $tagService->getColorTag(); $results['groupTag'] = $tagService->getTagGroup(); $results['tagSize'] = $tagService->getSizeTag(); $results['tagStyle'] = $tagService->getStyleTag(); $results['countries'] = $addressService->getListCountry(); $results['images'] = $productGalleryModel->getImagesProductByProductId($id); return view('admin.product.edit-product', $results); } catch (\Exception $e) { if ($request->ajax()) { $result['error'] = $e->getMessage(); return $result; } else { return view('errors.404', ['error_message' => $e->getMessage()]); } } }
public function productDetail($domain, $slug, $id, $tagId, Request $request) { $productService = new productService(); $tagService = new tagService(); $productGallery = new productGalleryModel(); $userId = \Kacana\Util::isLoggedIn() ? $this->_user->id : 0; try { $product = $productService->getProductById($id, $userId); $tagIdRelateds = []; $product->metaKeyword = $tagService->formatMetaKeyword($product->tag, $tagIdRelateds); $data['productRelated'] = []; $productRelationIds = []; foreach ($tagIdRelateds as $tagIdRelated => $numberProductByTagId) { $productRelations = $productService->getProductByTagId($tagIdRelated, 100); foreach ($productRelations as $productRelation) { if (!in_array($productRelation->id, $productRelationIds)) { array_push($data['productRelated'], $productRelation); array_push($productRelationIds, $productRelation->id); } if (count($data['productRelated']) > 8) { break; } } if (count($data['productRelated']) > 8) { break; } } $data['product'] = $product; $data['tag'] = $tagService->getTagById($tagId, false); $data['productSlide'] = $productGallery->getImagesProductByProductId($id, PRODUCT_IMAGE_TYPE_SLIDE); return view('client.product.detail', $data); } catch (\Exception $e) { if ($request->ajax()) { $result['error'] = $e->getMessage(); return $result; } else { return view('errors.404', ['error_message' => $e->getMessage()]); } } }
public function addProductToOrder(Request $request) { $productService = new productService(); $orderService = new orderService(); $orderId = $request->input('orderId'); $productId = $request->input('productId'); try { $order = $orderService->getOrderById($orderId); $total = $order->total; $discount = $order->discount; $originTotal = $order->origin_total; $quantity = $order->quantity; $product = $productService->getProductById($productId); $productDiscount = 0; if ($product->mainDiscount) { $productDiscount = $product->mainDiscount; } elseif ($product->discount) { $productDiscount = $product->discount; } $orderDetailData = new \stdClass(); $orderDetailData->order_id = $orderId; $orderDetailData->name = $product->name; $orderDetailData->price = $product->sell_price; $orderDetailData->discount = $product->discount; $orderDetailData->quantity = 1; $orderDetailData->product_id = $product->id; $orderDetailData->product_url = urlProductDetail($product); $orderDetailData->image = $product->image; $orderDetailData->subtotal = $product->sell_price - $productDiscount; $orderService->createOrderDetailAdmin($orderDetailData); $total += $product->sell_price - $productDiscount; $discount += $productDiscount; $originTotal += $product->sell_price; $quantity += 1; $dataOrder = ['total' => $total, 'origin_total' => $originTotal, 'quantity' => $quantity, 'discount' => $discount]; $orderService->updateOrder($orderId, $dataOrder); return redirect('/order/edit/' . $order->order_code); } catch (\Exception $e) { if ($request->ajax()) { $result['error'] = $e->getMessage(); return $result; } else { return view('errors.404', ['error_message' => $e]); } } }
/** * @param $productId * @param $colorId * @param $sizeId * @param $quantity * @param $tagId * @return \Gloudemans\Shoppingcart\Gloudemans\Shoppingcart\CartCollection * @throws \Exception */ public function addProductToCart($productId, $colorId, $sizeId, $quantity, $tagId) { $productService = new productService(); $tagService = new tagService(); if (!$productId) { throw new \Exception('có lỗi xảy ra trong quá trình đặt hàng!'); } $product = $productService->getProductById($productId); if (!$product) { throw new \Exception('sản phẩm không tồn tại!'); } $productName = $product->name; $productDiscount = 0; if (intval($product->discount)) { $productDiscount = $product->discount; $productPrice = $product->sell_price - $product->discount; } if (intval($product->mainDiscount)) { $productDiscount = $product->mainDiscount; $productPrice = $product->sell_price - $product->mainDiscount; } else { $productPrice = $product->sell_price; } $cartId = $this->generateCartId($productId, $colorId, $sizeId); $productImage = $product->image; $properties = $product->properties; $options = []; $options['url'] = urlProductDetail($product) . $tagId; $options['priceShow'] = formatMoney($productPrice); $options['productId'] = $productId; // check properties of product if (count($properties)) { if (!isset($properties[$colorId])) { throw new \Exception('sản phẩm không tồn tại colorId: ' . $colorId . '!'); } else { $colorTag = $tagService->getTagById($colorId, TAG_RELATION_TYPE_COLOR); $property = $properties[$colorId]; $sizeIds = $property->sizeIds; if ($colorTag) { $options['colorId'] = $colorId; $options['colorName'] = $colorTag->name; if ($property->product_gallery_id) { $galleries = $product->galleries; if ($galleries && count($galleries)) { foreach ($galleries as $gallery) { if ($gallery->id == $property->product_gallery_id) { $productImage = $gallery->image; } } } } if (count($sizeIds) && !in_array($sizeId, $sizeIds)) { throw new \Exception('sản phẩm không tồn tại colorId: ' . $colorId . ' với sizeId: ' . $sizeId . '!'); } else { if (count($sizeIds) && in_array($sizeId, $sizeIds)) { $sizeTag = $tagService->getTagById($sizeId, TAG_RELATION_TYPE_SIZE); if ($sizeTag) { $options['sizeId'] = $sizeId; $options['sizeName'] = $sizeTag->name; } else { throw new \Exception('sizeId: ' . $sizeId . ' không tồn tại trong hệ thống hoặc bị tắt!'); } } } } else { throw new \Exception('colorId: ' . $colorId . ' không tồn tại trong hệ thống hoặc bị tắt!'); } } } $options['image'] = $productImage; $options['discount'] = $productDiscount; $options['discountShow'] = formatMoney($productDiscount); $options['origin_price'] = $product->sell_price; $options['origin_price_show'] = formatMoney($product->sell_price); $order = Cart::add(array('id' => $cartId, 'name' => $productName, 'qty' => $quantity, 'price' => $productPrice, 'options' => $options)); return Cart::get(Cart::search(['id' => $cartId])[0]); }