Skip to content

Commit efd4393

Browse files
authored
Merge pull request #1433 from algolia/feature/MAGE-791
Feature/mage 791
2 parents c049898 + 618661b commit efd4393

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

Block/Algolia.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
1111
use Algolia\AlgoliaSearch\Helper\Entity\SuggestionHelper;
1212
use Algolia\AlgoliaSearch\Helper\LandingPageHelper;
13+
use Algolia\AlgoliaSearch\Registry\CurrentCategory;
1314
use Magento\Catalog\Model\Product;
1415
use Magento\Checkout\Model\Session as CheckoutSession;
1516
use Magento\Customer\Model\Context as CustomerContext;
@@ -97,6 +98,9 @@ class Algolia extends Template implements CollectionDataSourceInterface
9798
*/
9899
protected $date;
99100

101+
/** @var CurrentCategory */
102+
protected CurrentCategory $currentCategory;
103+
100104
protected $priceKey;
101105

102106
/**
@@ -139,6 +143,7 @@ public function __construct(
139143
PersonalizationHelper $personalizationHelper,
140144
CheckoutSession $checkoutSession,
141145
DateTime $date,
146+
CurrentCategory $currentCategory,
142147
array $data = []
143148
) {
144149
$this->config = $config;
@@ -158,6 +163,7 @@ public function __construct(
158163
$this->personalizationHelper = $personalizationHelper;
159164
$this->checkoutSession = $checkoutSession;
160165
$this->date = $date;
166+
$this->currentCategory = $currentCategory;
161167

162168
parent::__construct($context, $data);
163169
}
@@ -255,7 +261,7 @@ public function getStoreId()
255261

256262
public function getCurrentCategory()
257263
{
258-
return $this->registry->registry('current_category');
264+
return $this->currentCategory->get();
259265
}
260266

261267
/** @return Product */
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Observer;
4+
5+
use Algolia\AlgoliaSearch\Registry\CurrentCategory;
6+
use Magento\Catalog\Api\Data\CategoryInterface;
7+
use Magento\Framework\Event\Observer;
8+
use Magento\Framework\Event\ObserverInterface;
9+
10+
class RegisterCurrentCategoryObserver implements ObserverInterface
11+
{
12+
/** @var CurrentCategory */
13+
private CurrentCategory $currentCategory;
14+
15+
public function __construct(CurrentCategory $currentCategory) {
16+
$this->currentCategory = $currentCategory;
17+
}
18+
19+
/**
20+
* @inheritDoc
21+
*/
22+
public function execute(Observer $observer)
23+
{
24+
/** @var CategoryInterface */
25+
$category = $observer->getEvent()->getData('category');
26+
$this->currentCategory->set($category);
27+
}
28+
}

Registry/CurrentCategory.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Registry;
4+
5+
use Magento\Catalog\Api\CategoryRepositoryInterface;
6+
use Magento\Catalog\Api\Data\CategoryInterface;
7+
use Magento\Catalog\Api\Data\CategoryInterfaceFactory;
8+
9+
class CurrentCategory
10+
{
11+
private CategoryInterface $category;
12+
private CategoryRepositoryInterface $categoryRepository;
13+
private CategoryInterfaceFactory $categoryFactory;
14+
15+
public function __construct(
16+
CategoryRepositoryInterface $categoryRepository,
17+
CategoryInterfaceFactory $categoryFactory
18+
)
19+
{
20+
$this->categoryRepository = $categoryRepository;
21+
$this->categoryFactory = $categoryFactory;
22+
}
23+
24+
public function set(CategoryInterface $category): void {
25+
$this->category = $category;
26+
}
27+
28+
public function get(): CategoryInterface {
29+
return $this->category ?? $this->categoryFactory->create();
30+
}
31+
}

etc/frontend/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<event name="checkout_onepage_controller_success_action">
2424
<observer name="algoliasearch_insights_place_order_event" instance="Algolia\AlgoliaSearch\Observer\Insights\CheckoutOnepageControllerSuccessAction" />
2525
</event>
26+
<event name="catalog_controller_category_init_after">
27+
<observer name="algoliasearch_current_category" instance="Algolia\AlgoliaSearch\Observer\RegisterCurrentCategoryObserver"/>
28+
</event>
2629
<event name="customer_logout">
2730
<observer name="algoliasearch_personalization_unset_user_token" instance="Algolia\AlgoliaSearch\Observer\Insights\CustomerLogout" />
2831
</event>

0 commit comments

Comments
 (0)