Skip to content

Commit

Permalink
[ADD] Get Products from Category.
Browse files Browse the repository at this point in the history
  • Loading branch information
Seiger committed Jan 11, 2024
1 parent 6100d5a commit bb3b372
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: page
title: sCommerce for Evolution CMS 3
title: sCommerce for Evolution CMS
permalink: /
---

Expand Down
36 changes: 35 additions & 1 deletion src/Controllers/sCommerceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}')");
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Facades/sCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

use Illuminate\Support\Facades\Facade;

/**
* @mixin \Seiger\sCommerce\sCommerce
*/
class sCommerce extends Facade
{
/**
Expand Down
24 changes: 24 additions & 0 deletions src/Models/sProduct.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
<?php namespace Seiger\sCommerce\Models;

use EvolutionCMS\Facades\UrlProcessor;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use ReflectionClass;
use Seiger\sCommerce\Facades\sCommerce;

/**
* Class sProduct
*
* This class represents a product in the sCommerce application.
* It extends the base Model class.
*
* @package App\Models
*
* @method Builder|sProduct active()
* @property-read string $coverSrc The URL of the cover image source attribute.
* @property-read string $link The URL of the product.
*/
class sProduct extends Model
{
/**
Expand Down Expand Up @@ -87,6 +100,17 @@ public function scopeLang($query, $locale)
});
}

/**
* Apply the active scope to the given query builder.
*
* @param \Illuminate\Database\Query\Builder $builder The query builder to apply the scope to.
* @return \Illuminate\Database\Query\Builder The modified query builder.
*/
public function scopeActive($builder)
{
return $builder->where('s_products.published', '1');
}

/**
* Get the categories associated with the product.
*
Expand Down
27 changes: 27 additions & 0 deletions src/sCommerce.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Seiger\sCommerce;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Seiger\sCommerce\Controllers\sCommerceController;
use Seiger\sCommerce\Models\sProduct;
Expand Down Expand Up @@ -35,6 +36,32 @@ public function getProductByAlias(string $alias): object
return sProduct::whereAlias($alias)->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.
*
Expand Down

0 comments on commit bb3b372

Please sign in to comment.