|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\CatalogSearch\Setup\Patch\Data; |
| 8 | + |
| 9 | +use Magento\Catalog\Api\ProductAttributeRepositoryInterface; |
| 10 | +use Magento\Framework\App\State; |
| 11 | +use Magento\Framework\Indexer\IndexerInterfaceFactory; |
| 12 | +use Magento\Framework\Setup\Patch\DataPatchInterface; |
| 13 | +use Magento\Framework\Setup\Patch\PatchVersionInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * This patch sets up search weight for the product's system attributes, reindex required after patch applying. |
| 17 | + */ |
| 18 | +class SetInitialSearchWeightForAttributes implements DataPatchInterface, PatchVersionInterface |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var IndexerInterfaceFactory |
| 22 | + */ |
| 23 | + private $indexerFactory; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var ProductAttributeRepositoryInterface |
| 27 | + */ |
| 28 | + private $attributeRepository; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var State |
| 32 | + */ |
| 33 | + private $state; |
| 34 | + |
| 35 | + /** |
| 36 | + * SetInitialSearchWeightForAttributes constructor. |
| 37 | + * @param IndexerInterfaceFactory $indexerFactory |
| 38 | + * @param ProductAttributeRepositoryInterface $attributeRepository |
| 39 | + * @param State $state |
| 40 | + */ |
| 41 | + public function __construct( |
| 42 | + IndexerInterfaceFactory $indexerFactory, |
| 43 | + ProductAttributeRepositoryInterface $attributeRepository, |
| 44 | + State $state |
| 45 | + ) { |
| 46 | + $this->indexerFactory = $indexerFactory; |
| 47 | + $this->attributeRepository = $attributeRepository; |
| 48 | + $this->state = $state; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @inheritdoc |
| 53 | + */ |
| 54 | + public function apply() |
| 55 | + { |
| 56 | + $this->setWeight('sku', 6); |
| 57 | + $this->setWeight('name', 5); |
| 58 | + $indexer = $this->indexerFactory->create()->load('catalogsearch_fulltext'); |
| 59 | + $this->state->emulateAreaCode( |
| 60 | + \Magento\Framework\App\Area::AREA_CRONTAB, |
| 61 | + function () use ($indexer) { |
| 62 | + $indexer->getState() |
| 63 | + ->setStatus(\Magento\Framework\Indexer\StateInterface::STATUS_INVALID) |
| 64 | + ->save(); |
| 65 | + } |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @inheritdoc |
| 71 | + */ |
| 72 | + public static function getDependencies() |
| 73 | + { |
| 74 | + return []; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @inheritdoc |
| 79 | + */ |
| 80 | + public static function getVersion() |
| 81 | + { |
| 82 | + return '2.0.0'; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @inheritdoc |
| 87 | + */ |
| 88 | + public function getAliases() |
| 89 | + { |
| 90 | + return []; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Set attribute search weight. |
| 95 | + * |
| 96 | + * @param string $attributeCode |
| 97 | + * @param int $weight |
| 98 | + * @return void |
| 99 | + */ |
| 100 | + private function setWeight($attributeCode, $weight) |
| 101 | + { |
| 102 | + $attribute = $this->attributeRepository->get($attributeCode); |
| 103 | + $attribute->setSearchWeight($weight); |
| 104 | + $this->attributeRepository->save($attribute); |
| 105 | + } |
| 106 | +} |
0 commit comments