diff --git a/README.md b/README.md index db53846..723848e 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ of Evolution CMS for seamless and efficient **online commerce**. - [ ] Integration with Payment Systems. - [ ] Integration with Warehouses. - [ ] Integration with Trading Platforms. -- [x] sLang Integration. +- [x] **[sLang](https://github.com/Seiger/sLang)** Integration. - [x] sGallery Integration. - [x] sMultisite Integration. - [ ] Advanced Analytics and Reporting. diff --git a/docs/pages/index.md b/docs/pages/index.md index bfe9e8c..f11e84b 100644 --- a/docs/pages/index.md +++ b/docs/pages/index.md @@ -1,6 +1,6 @@ --- layout: page -title: sCommerce for Evolution CMS 3 +title: sCommerce for Evolution CMS permalink: / --- diff --git a/src/Controllers/sCommerceController.php b/src/Controllers/sCommerceController.php index 9f3c5f5..a7fe3c4 100644 --- a/src/Controllers/sCommerceController.php +++ b/src/Controllers/sCommerceController.php @@ -43,7 +43,7 @@ public function updateDBConfigs(): bool { $prf = 'scom_'; $tbl = evo()->getDatabase()->getFullTableName('system_settings'); - + if (request()->has('basic__catalog_root') && request()->input('basic__catalog_root') != evo()->getConfig($prf . 'catalog_root')) { $catalog_root = request()->input('basic__catalog_root'); evo()->getDatabase()->query("REPLACE INTO {$tbl} (`setting_name`, `setting_value`) VALUES ('{$prf}catalog_root', '{$catalog_root}')"); @@ -134,6 +134,18 @@ public function listCategories(): array return $this->categories; } + /** + * Retrieves a list of all active sub-categories within a specified category. + * + * @param int $category The ID of the category to retrieve sub-categories for. + * @param int $dept The depth of sub-categories to retrieve (default: 10). + * @return mixed An array of all active sub-category IDs within the specified category. + */ + public function listAllActiveSubCategories(int $category, int $dept = 10) + { + return $this->getActiveChildIds($category, $dept); + } + /** * Initializes and returns a rich text editor for the specified elements. * @@ -277,6 +289,28 @@ protected function categoryCrumb($resource, $crumb = ''): void } } + /** + * Retrieves the active child IDs of the specified ID(s). + * + * @param int|array $id The ID(s) of the parent element(s). + * @param int $dept The maximum depth to traverse when retrieving child IDs (default: 10). + * @return array The array of active child IDs. + */ + protected function getActiveChildIds(int|array $id, int $dept = 10): array + { + $id = is_array($id) ? $id : [$id]; + $res = SiteContent::select(['id'])->whereIn('parent', $id)->active()->get()->pluck('id')->toArray(); + + if (count($res)) { + $this->categories = array_merge($this->categories, $res); + if ($dept) { + $this->getActiveChildIds($res, $dept--); + } + } + + return $this->categories; + } + /** * Check if the given input is an integer. * diff --git a/src/Facades/sCommerce.php b/src/Facades/sCommerce.php index 211c89b..8411eb3 100644 --- a/src/Facades/sCommerce.php +++ b/src/Facades/sCommerce.php @@ -2,6 +2,9 @@ use Illuminate\Support\Facades\Facade; +/** + * @mixin \Seiger\sCommerce\sCommerce + */ class sCommerce extends Facade { /** diff --git a/src/Models/sProduct.php b/src/Models/sProduct.php index 0bc484f..64d3514 100644 --- a/src/Models/sProduct.php +++ b/src/Models/sProduct.php @@ -1,11 +1,24 @@ where('s_products.published', '1'); + } + /** * Get the categories associated with the product. * diff --git a/src/sCommerce.php b/src/sCommerce.php index af17a45..2772ebd 100644 --- a/src/sCommerce.php +++ b/src/sCommerce.php @@ -1,6 +1,7 @@ first() ?? new sProduct(); } + /** + * Retrieves the products belonging to a specific category. + * + * @param int|null $category The ID of the category. If not provided, it will default to the current document identifier. + * @param string|null $lang The language code for the product names. If not provided, it will default to the application's locale. + * @param int $dept The depth of sub-categories to include in the query. Defaults to 10. + * @return object The products belonging to the specified category, filtered by language and category ID. + */ + public function getCategoryProducts(int $category = null, string $lang = null, int $dept = 10): object + { + $sCommerceController = new sCommerceController(); + + if (!$lang) { + $lang = evo()->getLocale(); + } + + if (!$category) { + $category = evo()->documentIdentifier; + } + + $categories = array_merge([$category], $sCommerceController->listAllActiveSubCategories($category, $dept)); + $productIds = DB::table('s_product_category')->select(['product'])->whereIn('category', $categories)->get()->pluck('product')->toArray(); + + return sProduct::lang($lang)->whereIn('category', $categories)->orWhereIn('id', $productIds)->active()->get(); + } + /** * Retrieves the products listing from cache or sets it if not found. *