added method that returns path categories
This commit is contained in:
parent
405c979375
commit
a2f2c650a4
|
|
@ -5,6 +5,7 @@ namespace Webkul\Category\Models;
|
|||
use Webkul\Core\Eloquent\TranslatableModel;
|
||||
use Kalnoy\Nestedset\NodeTrait;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Webkul\Category\Contracts\Category as CategoryContract;
|
||||
use Webkul\Attribute\Models\AttributeProxy;
|
||||
|
||||
|
|
@ -19,7 +20,7 @@ class Category extends TranslatableModel implements CategoryContract
|
|||
{
|
||||
use NodeTrait;
|
||||
|
||||
public $translatedAttributes = ['name', 'description', 'slug', 'meta_title', 'meta_description', 'meta_keywords'];
|
||||
public $translatedAttributes = ['name', 'description', 'slug', 'url_path', 'meta_title', 'meta_description', 'meta_keywords'];
|
||||
|
||||
protected $fillable = ['position', 'status', 'display_mode', 'parent_id'];
|
||||
|
||||
|
|
@ -51,4 +52,47 @@ class Category extends TranslatableModel implements CategoryContract
|
|||
{
|
||||
return $this->belongsToMany(AttributeProxy::modelClass(), 'category_filterable_attributes')->with('options');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all categories within the category's path
|
||||
*
|
||||
* @return Category[]
|
||||
*/
|
||||
public function getPathCategories(): array
|
||||
{
|
||||
$category = $this->findInTree();
|
||||
|
||||
$categories = [$category];
|
||||
|
||||
while (isset($category->parent)) {
|
||||
$category = $category->parent;
|
||||
$categories[] = $category;
|
||||
}
|
||||
|
||||
array_pop($categories);
|
||||
return array_reverse($categories);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and returns the category within a nested category tree
|
||||
* will search in root category by default
|
||||
*
|
||||
* @param Category[] $categoryTree
|
||||
* @return Category
|
||||
*/
|
||||
private function findInTree($categoryTree = null): Category
|
||||
{
|
||||
if (!$categoryTree) {
|
||||
$rootCategoryId = core()->getCurrentChannel()->root_category_id;
|
||||
$categoryTree = app('Webkul\Category\Repositories\CategoryRepository')->getVisibleCategoryTree($rootCategoryId);
|
||||
}
|
||||
|
||||
foreach ($categoryTree as $category) {
|
||||
if ($category->id === $this->id) {
|
||||
return $category;
|
||||
}
|
||||
return $this->findInTree($category->children);
|
||||
}
|
||||
throw new NotFoundHttpException('category not found in tree');
|
||||
}
|
||||
}
|
||||
|
|
@ -138,5 +138,18 @@ class CategoryCest
|
|||
'locale' => $this->localeEn->code,
|
||||
'url_path' => $expectedUrlPath,
|
||||
]);
|
||||
|
||||
// test if the url_path attribute is available in the model
|
||||
$this->grandChildCategory->refresh();
|
||||
$I->assertEquals($expectedUrlPath, $this->grandChildCategory->url_path);
|
||||
}
|
||||
|
||||
public function testGetPathCategories(UnitTester $I)
|
||||
{
|
||||
$pathCategories = $this->grandChildCategory->getPathCategories();
|
||||
$I->assertCount(3, $pathCategories);
|
||||
$I->assertEquals($pathCategories[0]->id, $this->category->id);
|
||||
$I->assertEquals($pathCategories[1]->id, $this->childCategory->id);
|
||||
$I->assertEquals($pathCategories[2]->id, $this->grandChildCategory->id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue