Skip to content

Commit

Permalink
MAGE-791 Add alt registry for current category
Browse files Browse the repository at this point in the history
  • Loading branch information
cammonro committed Oct 31, 2023
1 parent b3b50e3 commit 917d1d9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Observer/RegisterCurrentCategoryObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Algolia\AlgoliaSearch\Observer;

use Algolia\AlgoliaSearch\Registry\CurrentCategory;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class RegisterCurrentCategoryObserver implements ObserverInterface
{
/** @var CurrentCategory */
private CurrentCategory $currentCategory;

public function __construct(CurrentCategory $currentCategory) {
$this->currentCategory = $currentCategory;
}

/**
* @inheritDoc
*/
public function execute(Observer $observer)
{
/** @var CategoryInterface */
$category = $observer->getEvent()->getData('category');
$this->currentCategory->set($category);
}
}
31 changes: 31 additions & 0 deletions Registry/CurrentCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Algolia\AlgoliaSearch\Registry;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Api\Data\CategoryInterfaceFactory;

class CurrentCategory
{
private CategoryInterface $category;
private CategoryRepositoryInterface $categoryRepository;
private CategoryInterfaceFactory $categoryFactory;

public function __construct(
CategoryRepositoryInterface $categoryRepository,
CategoryInterfaceFactory $categoryFactory
)
{
$this->categoryRepository = $categoryRepository;
$this->categoryFactory = $categoryFactory;
}

public function set(CategoryInterface $category): void {
$this->category = $category;
}

public function get(): CategoryInterface {
return $this->category ?? $this->categoryFactory->create();
}
}
5 changes: 5 additions & 0 deletions etc/frontend/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@
<event name="checkout_onepage_controller_success_action">
<observer name="algoliasearch_insights_place_order_event" instance="Algolia\AlgoliaSearch\Observer\Insights\CheckoutOnepageControllerSuccessAction" />
</event>

<!-- Registry -->
<event name="catalog_controller_category_init_after">
<observer name="algoliasearch_current_category" instance="Algolia\AlgoliaSearch\Observer\RegisterCurrentCategoryObserver"/>
</event>
</config>

0 comments on commit 917d1d9

Please sign in to comment.