/** * @param $tagId * @param $limit * @param int $page * @param int $userId * @param bool $options * @return array|bool|static[] * @throws \Exception */ public function getProductByTagId($tagId, $limit = 20, $userId = 0, $page = 1, $options = false) { $productModel = new productModel(); $userProductLike = new userProductLikeModel(); $tagService = new tagService(); if (!$tagId) { throw new \Exception('Tag id is not available'); } $tagIdList = array(); $tagIds = $tagService->getAllChildTag($tagId, $tagIdList, $options['product_tag_type_id']); //caculate offset from page number if ($page) { $offset = ($page - 1) * $limit; } else { $offset = 0; } $products = $productModel->getProductByTagId($tagIds, $limit, $offset, $page, $options); if ($page && !$products->total() || !count($products)) { unset($options['product_tag_type_id']); $products = $productModel->getProductByTagId($tagIds, $limit, $offset, $page, $options); } foreach ($products as &$product) { if ($userId) { $product->isLiked = $userProductLike->getItem($userId, $product->id) ? true : false; } $this->formatProductProperties($product); } return $products; }