-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added binding to category - enable / disable + delete support./
- Loading branch information
1 parent
58a11b9
commit 692488a
Showing
4 changed files
with
149 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace ScandiPWA\MenuOrganizer\Observer; | ||
|
||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Framework\Exception\AlreadyExistsException; | ||
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item\CollectionFactory; | ||
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item; | ||
|
||
/** | ||
* Class DeleteCategoryItem | ||
* @package ScandiPWA\MenuOrganizer\Observer | ||
*/ | ||
class DeleteCategoryItem implements ObserverInterface | ||
{ | ||
/** | ||
* @var Item | ||
*/ | ||
private $data; | ||
|
||
/** | ||
* @var CollectionFactory | ||
*/ | ||
private $collection; | ||
|
||
/** | ||
* UpdatePageIdentifier constructor. | ||
* @param Item $data | ||
* @param CollectionFactory $collection | ||
*/ | ||
public function __construct( | ||
Item $data, | ||
CollectionFactory $collection | ||
) | ||
{ | ||
$this->collection = $collection; | ||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* @param Observer $observer | ||
* @throws AlreadyExistsException | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
/** @var $category \Magento\Catalog\Model\Category */ | ||
$category = $observer->getEvent()->getCategory(); | ||
|
||
$items = $this->collection | ||
->create() | ||
->addFieldToFilter('category_id', ['eq' => $category->getId()]) | ||
->getItems(); | ||
|
||
foreach ($items as $item) { | ||
// disable all menu items assigned to category | ||
$item->setData('is_active', 0); | ||
$this->data->save($item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace ScandiPWA\MenuOrganizer\Observer; | ||
|
||
use Magento\Framework\Event\Observer; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Framework\Exception\AlreadyExistsException; | ||
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item\CollectionFactory; | ||
use ScandiPWA\MenuOrganizer\Model\ResourceModel\Item; | ||
|
||
/** | ||
* Class DeleteCategoryItem | ||
* @package ScandiPWA\MenuOrganizer\Observer | ||
*/ | ||
class UpdateCategoryItem implements ObserverInterface | ||
{ | ||
/** | ||
* @var Item | ||
*/ | ||
private $data; | ||
|
||
/** | ||
* @var CollectionFactory | ||
*/ | ||
private $collection; | ||
|
||
/** | ||
* UpdatePageIdentifier constructor. | ||
* @param Item $data | ||
* @param CollectionFactory $collection | ||
*/ | ||
public function __construct( | ||
Item $data, | ||
CollectionFactory $collection | ||
) | ||
{ | ||
$this->collection = $collection; | ||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* @param Observer $observer | ||
* @throws AlreadyExistsException | ||
*/ | ||
public function execute(Observer $observer) | ||
{ | ||
/** @var $category \Magento\Catalog\Model\Category */ | ||
$category = $observer->getEvent()->getCategory(); | ||
$isCatActive = $category->getIsActive(); | ||
|
||
$items = $this->collection | ||
->create() | ||
->addFieldToFilter('category_id', ['eq' => $category->getId()]) | ||
->getItems(); | ||
|
||
foreach ($items as $item) { | ||
// ensure menu items bound to category respect category's "is_active" field | ||
$item->setData('is_active', $isCatActive); | ||
$this->data->save($item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters