Skip to content

Commit

Permalink
Merge pull request #1433 from algolia/feature/MAGE-791
Browse files Browse the repository at this point in the history
Feature/mage 791
  • Loading branch information
mohitalgolia authored Dec 5, 2023
2 parents c049898 + 618661b commit efd4393
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Block/Algolia.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
use Algolia\AlgoliaSearch\Helper\Entity\SuggestionHelper;
use Algolia\AlgoliaSearch\Helper\LandingPageHelper;
use Algolia\AlgoliaSearch\Registry\CurrentCategory;
use Magento\Catalog\Model\Product;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Customer\Model\Context as CustomerContext;
Expand Down Expand Up @@ -97,6 +98,9 @@ class Algolia extends Template implements CollectionDataSourceInterface
*/
protected $date;

/** @var CurrentCategory */
protected CurrentCategory $currentCategory;

protected $priceKey;

/**
Expand Down Expand Up @@ -139,6 +143,7 @@ public function __construct(
PersonalizationHelper $personalizationHelper,
CheckoutSession $checkoutSession,
DateTime $date,
CurrentCategory $currentCategory,
array $data = []
) {
$this->config = $config;
Expand All @@ -158,6 +163,7 @@ public function __construct(
$this->personalizationHelper = $personalizationHelper;
$this->checkoutSession = $checkoutSession;
$this->date = $date;
$this->currentCategory = $currentCategory;

parent::__construct($context, $data);
}
Expand Down Expand Up @@ -255,7 +261,7 @@ public function getStoreId()

public function getCurrentCategory()
{
return $this->registry->registry('current_category');
return $this->currentCategory->get();
}

/** @return Product */
Expand Down
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();
}
}
3 changes: 3 additions & 0 deletions etc/frontend/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<event name="checkout_onepage_controller_success_action">
<observer name="algoliasearch_insights_place_order_event" instance="Algolia\AlgoliaSearch\Observer\Insights\CheckoutOnepageControllerSuccessAction" />
</event>
<event name="catalog_controller_category_init_after">
<observer name="algoliasearch_current_category" instance="Algolia\AlgoliaSearch\Observer\RegisterCurrentCategoryObserver"/>
</event>
<event name="customer_logout">
<observer name="algoliasearch_personalization_unset_user_token" instance="Algolia\AlgoliaSearch\Observer\Insights\CustomerLogout" />
</event>
Expand Down

0 comments on commit efd4393

Please sign in to comment.