Skip to content

Commit

Permalink
Don't allow archiving of a category if it has products assigned to it. (
Browse files Browse the repository at this point in the history
#817)

Fixes #803
  • Loading branch information
wernerkrauss committed Feb 20, 2024
1 parent 85f7f9c commit 8c2d512
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/Page/ProductCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SilverShop\Extension\ProductVariationsExtension;
use SilverStripe\i18n\i18nEntityProvider;
use SilverStripe\ORM\DataList;
use SilverStripe\Security\Security;

/**
* Product Category provides a way to hierartically categorise products.
Expand Down Expand Up @@ -57,16 +58,16 @@ public function ProductsShowable($recursive = true)
}
$products = Product::get()->filterAny(
[
'ParentID' => $groupids,
'ProductCategories.ID' => $groupids
'ParentID' => $groupids,
'ProductCategories.ID' => $groupids
]
);
if (self::config()->must_have_price) {
if (Product::has_extension(ProductVariationsExtension::class)) {
$products = $products->filterAny(
[
'BasePrice:GreaterThan' => 0,
'Variations.Price:GreaterThan' => 0
'BasePrice:GreaterThan' => 0,
'Variations.Price:GreaterThan' => 0
]
);
} else {
Expand Down Expand Up @@ -130,8 +131,8 @@ public function GroupsMenu()
/**
* Override the nested title defaults, to show deeper nesting in the CMS.
*
* @param integer $level nesting level
* @param string $separator seperate nesting with this string
* @param integer $level nesting level
* @param string $separator seperate nesting with this string
*/
public function NestedTitle($level = 10, $separator = ' > ', $field = 'MenuTitle')
{
Expand All @@ -158,4 +159,40 @@ public function provideI18nEntities()

return $entities;
}

/**
* Don't allow archiving of a category if it has products assigned to it.
*
* @param $member
* @return bool
*/
public function canArchive($member = null)
{
if (!$member) {
$member = Security::getCurrentUser();
}

$parentCanArchive = parent::canArchive($member);

//no need to look further, archiving is not allowed
if (!$parentCanArchive) {
return false;
}

$groupIDs = [$this->ID];
$groupIDs += $this->AllChildCategoryIDs();

//check, if the category or a subcategory has products assigned
$products = Product::get()->filterAny(
[
'ParentID' => $groupIDs,
'ProductCategories.ID' => $groupIDs
]
);
if ($products->exists()) {
return false;
}

return true;
}
}

0 comments on commit 8c2d512

Please sign in to comment.