|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Algolia\AlgoliaSearch\Observer; |
| 5 | + |
| 6 | +use Algolia\AlgoliaSearch\Api\RecommendManagementInterface; |
| 7 | +use Algolia\AlgoliaSearch\Helper\ConfigHelper; |
| 8 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 9 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 10 | +use Magento\Framework\App\Config\Storage\WriterInterface; |
| 11 | +use Magento\Framework\Event\ObserverInterface; |
| 12 | +use Magento\Framework\Event\Observer; |
| 13 | +use Magento\Framework\Exception\LocalizedException; |
| 14 | + |
| 15 | +class RecommendSettings implements ObserverInterface |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var string |
| 19 | + */ |
| 20 | + private $productId = ''; |
| 21 | + |
| 22 | + /** |
| 23 | + * @param ConfigHelper $configHelper |
| 24 | + * @param WriterInterface $configWriter |
| 25 | + * @param ProductRepositoryInterface $productRepository |
| 26 | + * @param RecommendManagementInterface $recommendManagement |
| 27 | + * @param SearchCriteriaBuilder $searchCriteriaBuilder |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + private readonly ConfigHelper $configHelper, |
| 31 | + private readonly WriterInterface $configWriter, |
| 32 | + private readonly ProductRepositoryInterface $productRepository, |
| 33 | + private readonly RecommendManagementInterface $recommendManagement, |
| 34 | + private readonly SearchCriteriaBuilder $searchCriteriaBuilder |
| 35 | + ){} |
| 36 | + |
| 37 | + /** |
| 38 | + * @param Observer $observer |
| 39 | + * @throws LocalizedException |
| 40 | + */ |
| 41 | + public function execute(Observer $observer) |
| 42 | + { |
| 43 | + foreach ($observer->getData('changed_paths') as $changedPath) { |
| 44 | + // Validate before enable FBT on PDP or on cart page |
| 45 | + if (( |
| 46 | + $changedPath == ConfigHelper::IS_RECOMMEND_FREQUENTLY_BOUGHT_TOGETHER_ENABLED |
| 47 | + && $this->configHelper->isRecommendFrequentlyBroughtTogetherEnabled() |
| 48 | + ) || ( |
| 49 | + $changedPath == ConfigHelper::IS_RECOMMEND_FREQUENTLY_BOUGHT_TOGETHER_ENABLED_ON_CART_PAGE |
| 50 | + && $this->configHelper->isRecommendFrequentlyBroughtTogetherEnabledOnCartPage() |
| 51 | + )) { |
| 52 | + $this->validateFrequentlyBroughtTogether($changedPath); |
| 53 | + } |
| 54 | + |
| 55 | + // Validate before enable related products on PDP or on cart page |
| 56 | + if (( |
| 57 | + $changedPath == ConfigHelper::IS_RECOMMEND_RELATED_PRODUCTS_ENABLED |
| 58 | + && $this->configHelper->isRecommendRelatedProductsEnabled() |
| 59 | + ) || ( |
| 60 | + $changedPath == ConfigHelper::IS_RECOMMEND_RELATED_PRODUCTS_ENABLED_ON_CART_PAGE |
| 61 | + && $this->configHelper->isRecommendRelatedProductsEnabledOnCartPage() |
| 62 | + )) { |
| 63 | + $this->validateRelatedProducts($changedPath); |
| 64 | + } |
| 65 | + |
| 66 | + // Validate before enable trending items on PDP or on cart page |
| 67 | + if (( |
| 68 | + $changedPath == ConfigHelper::IS_TREND_ITEMS_ENABLED_IN_PDP |
| 69 | + && $this->configHelper->isTrendItemsEnabledInPDP() |
| 70 | + ) || ( |
| 71 | + $changedPath == ConfigHelper::IS_TREND_ITEMS_ENABLED_IN_SHOPPING_CART |
| 72 | + && $this->configHelper->isTrendItemsEnabledInShoppingCart() |
| 73 | + )) { |
| 74 | + $this->validateTrendingItems($changedPath); |
| 75 | + } |
| 76 | + |
| 77 | + // Validate before enable looking similar on PDP or on cart page |
| 78 | + if (( |
| 79 | + $changedPath == ConfigHelper::IS_LOOKING_SIMILAR_ENABLED_IN_PDP |
| 80 | + && $this->configHelper->isLookingSimilarEnabledInPDP() |
| 81 | + ) || ( |
| 82 | + $changedPath == ConfigHelper::IS_LOOKING_SIMILAR_ENABLED_IN_SHOPPING_CART |
| 83 | + && $this->configHelper->isLookingSimilarEnabledInShoppingCart() |
| 84 | + )) { |
| 85 | + $this->validateLookingSimilar($changedPath); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param string $changedPath |
| 92 | + * @return void |
| 93 | + * @throws LocalizedException |
| 94 | + */ |
| 95 | + protected function validateFrequentlyBroughtTogether(string $changedPath): void |
| 96 | + { |
| 97 | + try { |
| 98 | + $recommendations = $this->recommendManagement->getBoughtTogetherRecommendation($this->getProductId()); |
| 99 | + if (empty($recommendations['renderingContent'])) { |
| 100 | + throw new LocalizedException(__( |
| 101 | + "It appears that there is no trained model available for the AppID: %1.", |
| 102 | + $this->configHelper->getApplicationID() |
| 103 | + )); |
| 104 | + } |
| 105 | + } catch (\Exception $e) { |
| 106 | + $this->configWriter->save($changedPath, 0); |
| 107 | + throw new LocalizedException(__("Unable to save FBT Recommend configuration due to the following error: " . $e->getMessage())); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param string $changedPath |
| 113 | + * @return void |
| 114 | + * @throws LocalizedException |
| 115 | + */ |
| 116 | + protected function validateRelatedProducts(string $changedPath): void |
| 117 | + { |
| 118 | + try { |
| 119 | + $recommendations = $this->recommendManagement->getRelatedProductsRecommendation($this->getProductId()); |
| 120 | + if (empty($recommendations['renderingContent'])) { |
| 121 | + throw new LocalizedException(__( |
| 122 | + "It appears that there is no trained model available for the AppID: %1.", |
| 123 | + $this->configHelper->getApplicationID() |
| 124 | + )); |
| 125 | + } |
| 126 | + } catch (\Exception $e) { |
| 127 | + $this->configWriter->save($changedPath, 0); |
| 128 | + throw new LocalizedException(__("Unable to save Related Products Recommend configuration due to the following error: ". $e->getMessage())); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @param string $changedPath |
| 134 | + * @return void |
| 135 | + * @throws LocalizedException |
| 136 | + */ |
| 137 | + protected function validateTrendingItems(string $changedPath): void |
| 138 | + { |
| 139 | + try { |
| 140 | + $recommendations = $this->recommendManagement->getTrendingItemsRecommendation(); |
| 141 | + // When no recommendations suggested, most likely trained model is missing |
| 142 | + if (empty($recommendations['renderingContent'])) { |
| 143 | + throw new LocalizedException(__( |
| 144 | + "It appears that there is no trained model available for the AppID: %1.", |
| 145 | + $this->configHelper->getApplicationID() |
| 146 | + )); |
| 147 | + } |
| 148 | + } catch (\Exception $e) { |
| 149 | + $this->configWriter->save($changedPath, 0); |
| 150 | + throw new LocalizedException(__("Unable to save Trending Items Recommend configuration due to the following error: ". $e->getMessage())); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * @param string $changedPath |
| 156 | + * @return void |
| 157 | + * @throws LocalizedException |
| 158 | + */ |
| 159 | + protected function validateLookingSimilar(string $changedPath): void |
| 160 | + { |
| 161 | + try { |
| 162 | + $recommendations = $this->recommendManagement->getLookingSimilarRecommendation($this->getProductId()); |
| 163 | + if (empty($recommendations['renderingContent'])) { |
| 164 | + throw new LocalizedException(__( |
| 165 | + "It appears that there is no trained model available for the AppID: %1.", |
| 166 | + $this->configHelper->getApplicationID() |
| 167 | + )); |
| 168 | + } |
| 169 | + } catch (\Exception $e) { |
| 170 | + $this->configWriter->save($changedPath, 0); |
| 171 | + throw new LocalizedException(__("Unable to save Looking Similar Recommend configuration due to the following error: ". $e->getMessage())); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @return string |
| 177 | + */ |
| 178 | + private function getProductId(): string |
| 179 | + { |
| 180 | + if ($this->productId === '') { |
| 181 | + $searchCriteria = $this->searchCriteriaBuilder |
| 182 | + ->addFilter('status', 1) |
| 183 | + ->addFilter('quantity_and_stock_status', 1) |
| 184 | + ->addFilter('visibility', [2, 3, 4], 'in') |
| 185 | + ->setPageSize(10) |
| 186 | + ->create(); |
| 187 | + $result = $this->productRepository->getList($searchCriteria); |
| 188 | + if ($result->getTotalCount()) { |
| 189 | + $products = array_reverse($result->getItems()); |
| 190 | + $firstProduct = array_pop($products); |
| 191 | + $this->productId = (string)$firstProduct->getId(); |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return $this->productId; |
| 196 | + } |
| 197 | +} |
0 commit comments