Skip to content

Commit 47622cc

Browse files
committed
Update deprecated registry for current product
1 parent 1998c91 commit 47622cc

File tree

4 files changed

+131
-9
lines changed

4 files changed

+131
-9
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Observer;
4+
5+
use Magento\Catalog\Api\Data\ProductInterface;
6+
use Magento\Framework\Event\Observer as Event;
7+
use Magento\Framework\Event\ObserverInterface;
8+
use Algolia\AlgoliaSearch\Registry\CurrentProduct;
9+
10+
/**
11+
* Class RegisterCurrentProductObserver
12+
*
13+
* Current product observer
14+
*/
15+
class RegisterCurrentProductObserver implements ObserverInterface
16+
{
17+
/**
18+
* @var CurrentProduct
19+
*/
20+
private $currentProduct;
21+
22+
/**
23+
* RegisterCurrentProductObserver constructor.
24+
*
25+
* @param CurrentProduct $currentProduct
26+
*/
27+
public function __construct(
28+
CurrentProduct $currentProduct
29+
) {
30+
$this->currentProduct = $currentProduct;
31+
}
32+
33+
/**
34+
* Trigger event
35+
*
36+
* @param Event $event
37+
*/
38+
public function execute(Event $event)
39+
{
40+
/** @var ProductInterface $product */
41+
$product = $event->getData('product');
42+
$this->currentProduct->set($product);
43+
}
44+
}

Registry/CurrentProduct.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Registry;
4+
5+
use Magento\Catalog\Api\Data\ProductInterface;
6+
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
7+
8+
/**
9+
* Class CurrentProduct
10+
*
11+
* Get current product without registry
12+
*/
13+
class CurrentProduct
14+
{
15+
/**
16+
* @var ProductInterface
17+
*/
18+
private $product;
19+
20+
/**
21+
* @var ProductInterfaceFactory
22+
*/
23+
private $productFactory;
24+
25+
/**
26+
* CurrentProduct constructor.
27+
*
28+
* @param ProductInterfaceFactory $productFactory
29+
*/
30+
public function __construct(
31+
ProductInterfaceFactory $productFactory
32+
)
33+
{
34+
$this->productFactory = $productFactory;
35+
}
36+
37+
/**
38+
* Setter
39+
*
40+
* @param ProductInterface $product
41+
*/
42+
public function set(ProductInterface $product): void
43+
{
44+
$this->product = $product;
45+
}
46+
47+
/**
48+
* Getter
49+
*
50+
* @return ProductInterface
51+
*/
52+
public function get(): ProductInterface
53+
{
54+
return $this->product ?? $this->createProduct();
55+
}
56+
57+
/**
58+
* Product factory
59+
*
60+
* @return ProductInterface
61+
*/
62+
private function createProduct(): ProductInterface
63+
{
64+
return $this->productFactory->create();
65+
}
66+
}

ViewModel/Recommend/ProductView.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Magento\Catalog\Model\Product;
66
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
7-
use Magento\Framework\Registry;
7+
use Algolia\AlgoliaSearch\Registry\CurrentProduct;
88
use Magento\Framework\View\Element\Block\ArgumentInterface;
99

1010
class ProductView implements ArgumentInterface
@@ -15,26 +15,24 @@ class ProductView implements ArgumentInterface
1515
protected $product = null;
1616

1717
/**
18-
* Core registry
19-
*
20-
* @var Registry
18+
* @var CurrentProduct
2119
*/
22-
protected $coreRegistry = null;
20+
protected $currentProduct;
2321

2422
/**
2523
* @var ConfigHelper
2624
*/
2725
protected $configHelper;
2826

2927
/**
30-
* @param Registry $registry
28+
* @param CurrentProduct $currentProduct
3129
* @param ConfigHelper $configHelper
3230
*/
3331
public function __construct(
34-
Registry $registry,
32+
CurrentProduct $currentProduct,
3533
ConfigHelper $configHelper
3634
) {
37-
$this->coreRegistry = $registry;
35+
$this->currentProduct = $currentProduct;
3836
$this->configHelper = $configHelper;
3937
}
4038
/**
@@ -45,11 +43,22 @@ public function __construct(
4543
public function getProduct()
4644
{
4745
if (!$this->product) {
48-
$this->product = $this->coreRegistry->registry('product');
46+
$this->product = $this->getCurrentProduct();
4947
}
5048
return $this->product;
5149
}
5250

51+
/**
52+
* Get product
53+
*
54+
* @return array
55+
* @throws \Magento\Framework\Exception\NoSuchEntityException
56+
*/
57+
public function getCurrentProduct()
58+
{
59+
return $this->currentProduct->get();
60+
}
61+
5362
/**
5463
* @return array
5564
*/

etc/frontend/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@
3232
<event name="persistent_session_expired">
3333
<observer name="refresh_customer_data" instance="Algolia\AlgoliaSearch\Observer\Insights\CustomerLogout" />
3434
</event>
35+
<event name="catalog_controller_product_init_after">
36+
<observer name="algoliasearch_current_product" instance="Algolia\AlgoliaSearch\Observer\RegisterCurrentProductObserver"/>
37+
</event>
3538
</config>

0 commit comments

Comments
 (0)