-
Notifications
You must be signed in to change notification settings - Fork 82
/
WebsiteSearchProductIndexerListener.php
93 lines (80 loc) · 3.42 KB
/
WebsiteSearchProductIndexerListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace Oro\Bundle\ProductBundle\EventListener;
use Oro\Bundle\ProductBundle\Entity\Product;
use Oro\Bundle\WebsiteBundle\Provider\AbstractWebsiteLocalizationProvider;
use Oro\Bundle\WebsiteBundle\Provider\WebsiteLocalizationProvider;
use Oro\Bundle\WebsiteSearchBundle\Engine\IndexDataProvider;
use Oro\Bundle\WebsiteSearchBundle\Event\IndexEntityEvent;
use Oro\Bundle\WebsiteSearchBundle\Manager\WebsiteContextManager;
use Oro\Bundle\WebsiteSearchBundle\Placeholder\LocalizationIdPlaceholder;
class WebsiteSearchProductIndexerListener
{
/**
* @var WebsiteContextManager
*/
private $websiteContextManger;
/**
* @var WebsiteLocalizationProvider
*/
private $websiteLocalizationProvider;
/**
* @param AbstractWebsiteLocalizationProvider $websiteLocalizationProvider
* @param WebsiteContextManager $websiteContextManager
*/
public function __construct(
AbstractWebsiteLocalizationProvider $websiteLocalizationProvider,
WebsiteContextManager $websiteContextManager
) {
$this->websiteLocalizationProvider = $websiteLocalizationProvider;
$this->websiteContextManger = $websiteContextManager;
}
/**
* @param IndexEntityEvent $event
*/
public function onWebsiteSearchIndex(IndexEntityEvent $event)
{
$websiteId = $this->websiteContextManger->getWebsiteId($event->getContext());
if (!$websiteId) {
$event->stopPropagation();
return;
}
/** @var Product[] $products */
$products = $event->getEntities();
$localizations = $this->websiteLocalizationProvider->getLocalizationsByWebsiteId($websiteId);
foreach ($products as $product) {
// Non localized fields
$event->addField($product->getId(), 'product_id', $product->getId());
$event->addField($product->getId(), 'sku', $product->getSku(), true);
$event->addField($product->getId(), 'sku_uppercase', strtoupper($product->getSku()), true);
$event->addField($product->getId(), 'status', $product->getStatus());
$event->addField($product->getId(), 'inventory_status', $product->getInventoryStatus()->getId());
$event->addField($product->getId(), 'type', $product->getType());
$event->addField($product->getId(), 'new_arrival', (int)$product->isNewArrival());
foreach ($localizations as $localization) {
$localizationId = $localization->getId();
$placeholders = [LocalizationIdPlaceholder::NAME => $localizationId];
$event->addPlaceholderField(
$product->getId(),
'name_LOCALIZATION_ID',
(string)$product->getName($localization),
$placeholders,
true
);
$event->addPlaceholderField(
$product->getId(),
'short_description_LOCALIZATION_ID',
(string)$product->getShortDescription($localization),
$placeholders,
true
);
$event->addPlaceholderField(
$product->getId(),
IndexDataProvider::ALL_TEXT_L10N_FIELD,
(string)$product->getDescription($localization),
$placeholders,
true
);
}
}
}
}