public function listProductByCate($domain, $slug, $tagId, Request $request) { $productService = new productService(); $tagService = new tagService(); $userId = \Kacana\Util::isLoggedIn() ? $this->_user->id : 0; try { $tag = $request->input('tagId'); $page = $request->input('page', 1); $tag = $tag != "" ? $tag : $tagId; $sort = $request->input('sort'); $options = ['tagId' => $tag, 'sort' => $sort, 'product_tag_type_id' => TAG_RELATION_TYPE_MENU]; $limit = KACANA_PRODUCT_ITEM_PER_TAG; $data['items'] = $productService->getProductByTagId($tagId, $limit, $userId, $page, $options); $tags = $tagService->getTagById($tagId, false); $tags->allChilds = $tagService->getAllChildTagHaveProduct($tagId); $tagIdRelated = []; $tags->tagKeyword = $tagService->formatMetaKeyword($tags->allChilds, $tagIdRelated); $data['tag'] = $tags; $data['options'] = $options; } catch (\Exception $e) { if ($request->ajax()) { $result['error'] = $e->getMessage(); return $result; } else { return view('errors.404', ['error_message' => $e->getMessage()]); } } return view('client.product.listproduct', $data); }
public function getGroupTag(Request $request) { $tagService = new tagService(); $tagIds = $request->input('tagIds'); $return['ok'] = 0; try { $tags = array(); for ($i = 0; $i < count($tagIds); $i++) { array_push($tags, $tagService->getTagById($tagIds[$i], TAG_RELATION_TYPE_GROUP)); } $return['data'] = $tags; $return['ok'] = 1; } catch (\Exception $e) { // @codeCoverageIgnoreStart $return['error'] = $e->getMessage(); $return['errorMsg'] = $e->getMessage(); // @codeCoverageIgnoreEnd } return response()->json($return); }
/** * @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]); }
public function formatProductPropertiesWhenSearch(&$product) { $tagService = new tagService(); $product->image = AWS_CDN_URL . $product->image; $productGalleryModel = new productGalleryModel(); $productPropertiesModel = new productPropertiesModel(); $productProperties = $productPropertiesModel->getPropertiesByProductId($product->id); $properties = array(); $propertiesSize = array(); foreach ($productProperties as $property) { if (isset($property->product->status) && $property->product->status == KACANA_PRODUCT_STATUS_ACTIVE) { if (!isset($properties[$property->tag_color_id])) { $properties[$property->tag_color_id] = new \stdClass(); $properties[$property->tag_color_id]->color_name = $property->color->name; $properties[$property->tag_color_id]->color_id = $property->tag_color_id; $properties[$property->tag_color_id]->product_gallery_id = $property->product_gallery_id; $properties[$property->tag_color_id]->product_gallery = $productGalleryModel->getById($property->product_gallery_id); $properties[$property->tag_color_id]->product_gallery_array = $productGalleryModel->getById($property->product_gallery_id) ? $productGalleryModel->getById($property->product_gallery_id)->toArray() : 0; } if (!isset($properties[$property->tag_color_id]->size)) { $properties[$property->tag_color_id]->size = []; $properties[$property->tag_color_id]->sizeIds = []; } if ($property->tag_size_id) { $tagSize = $tagService->getTagById($property->tag_size_id, TAG_RELATION_TYPE_SIZE); $size = new \stdClass(); $size->name = $tagSize->name; $size->id = $tagSize->id; if (!isset($propertiesSize[$property->tag_size_id])) { $propertiesSize[$property->tag_size_id] = new \stdClass(); $propertiesSize[$property->tag_size_id]->color = []; $propertiesSize[$property->tag_size_id]->colorIds = []; } $propertiesSize[$property->tag_size_id]->name = $tagSize->name; $propertiesSize[$property->tag_size_id]->id = $property->tag_size_id; $colorSize = new \stdClass(); $colorSize->name = $property->name; $colorSize->id = $property->tag_color_id; array_push($propertiesSize[$property->tag_size_id]->color, $colorSize); array_push($propertiesSize[$property->tag_size_id]->colorIds, $property->tag_color_id); array_push($properties[$property->tag_color_id]->size, $size); array_push($properties[$property->tag_color_id]->sizeIds, $size->id); } } } if (count($propertiesSize)) { $product->propertiesSize = $propertiesSize; } $product->properties = $properties; return $product; }