Skip to content

Commit 3f951b8

Browse files
authored
Prepare MSI's handling (#841)
Adding some changes to handle MSI's Algolia external module
1 parent 0ef3d72 commit 3f951b8

File tree

5 files changed

+66
-48
lines changed

5 files changed

+66
-48
lines changed

Helper/Entity/ProductHelper.php

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ProductHelper
4141
private $eventManager;
4242
private $visibility;
4343
private $stockHelper;
44-
private $stockRegistry;
44+
protected $stockRegistry;
4545
private $objectManager;
4646
private $currencyManager;
4747
private $categoryHelper;
@@ -217,18 +217,11 @@ public function getProductCollectionQuery(
217217
->addAttributeToFilter('visibility', ['in' => $this->visibility->getVisibleInSiteIds()]);
218218
}
219219

220-
if ($this->configHelper->getShowOutOfStock($storeId) === false) {
221-
$this->stockHelper->addInStockFilterToCollection($products);
222-
}
220+
$this->addStockFilter($products, $storeId);
223221
}
224222

225223
/* @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
226-
$products = $products->addFinalPrice()
227-
->addAttributeToSelect('special_price')
228-
->addAttributeToSelect('special_from_date')
229-
->addAttributeToSelect('special_to_date')
230-
->addAttributeToSelect('visibility')
231-
->addAttributeToSelect('status');
224+
$this->addMandatoryAttributes($products);
232225

233226
$additionalAttr = $this->getAdditionalAttributes($storeId);
234227

@@ -263,6 +256,24 @@ public function getProductCollectionQuery(
263256
return $products;
264257
}
265258

259+
protected function addStockFilter($products, $storeId)
260+
{
261+
if ($this->configHelper->getShowOutOfStock($storeId) === false) {
262+
$this->stockHelper->addInStockFilterToCollection($products);
263+
}
264+
}
265+
266+
protected function addMandatoryAttributes($products)
267+
{
268+
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $products */
269+
$products->addFinalPrice()
270+
->addAttributeToSelect('special_price')
271+
->addAttributeToSelect('special_from_date')
272+
->addAttributeToSelect('special_to_date')
273+
->addAttributeToSelect('visibility')
274+
->addAttributeToSelect('status');
275+
}
276+
266277
public function getAdditionalAttributes($storeId = null)
267278
{
268279
return $this->configHelper->getProductAdditionalAttributes($storeId);
@@ -728,7 +739,7 @@ private function addImageData(array $customData, Product $product, $additionalAt
728739
return $customData;
729740
}
730741

731-
private function addInStock($defaultData, $customData, Product $product)
742+
protected function addInStock($defaultData, $customData, Product $product)
732743
{
733744
if (isset($defaultData['in_stock']) === false) {
734745
$stockItem = $this->stockRegistry->getStockItem($product->getId());
@@ -1125,15 +1136,24 @@ public function canProductBeReindexed($product, $storeId, $isChildProduct = fals
11251136
->withStoreId($storeId);
11261137
}
11271138

1139+
$isInStock = true;
11281140
if (!$this->configHelper->getShowOutOfStock($storeId)) {
1129-
$stockItem = $this->stockRegistry->getStockItem($product->getId());
1130-
if (! $stockItem->getIsInStock()) {
1131-
throw (new ProductOutOfStockException())
1132-
->withProduct($product)
1133-
->withStoreId($storeId);
1134-
}
1141+
$isInStock = $this->productIsInStock($product, $storeId);
1142+
}
1143+
1144+
if (!$isInStock) {
1145+
throw (new ProductOutOfStockException())
1146+
->withProduct($product)
1147+
->withStoreId($storeId);
11351148
}
11361149

11371150
return true;
11381151
}
1152+
1153+
public function productIsInStock($product, $storeId)
1154+
{
1155+
$stockItem = $this->stockRegistry->getStockItem($product->getId());
1156+
1157+
return $stockItem->getIsInStock();
1158+
}
11391159
}

Model/Indexer/CategoryObserver.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ public function __construct(
4242
}
4343

4444
/**
45-
* Using "before" method here instead of "after", because M2.1 doesn't pass "$product" argument
46-
* to "after" methods. When M2.1 support will be removed, this method can be rewriten to:
47-
* afterSave(CategoryResourceModel $categoryResource, CategoryResourceModel $result, CategoryModel $category)
48-
*
4945
* @param CategoryResourceModel $categoryResource
46+
* @param CategoryResourceModel $result
5047
* @param CategoryModel $category
5148
*
52-
* @return CategoryModel[]
49+
* @return CategoryResourceModel
5350
*/
54-
public function beforeSave(CategoryResourceModel $categoryResource, CategoryModel $category)
55-
{
51+
public function afterSave(
52+
CategoryResourceModel $categoryResource,
53+
CategoryResourceModel $result,
54+
CategoryModel $category
55+
) {
5656
$categoryResource->addCommitCallback(function() use ($category) {
5757
$collectionIds = [];
5858
// To reduce the indexing operation for products, only update if these values have changed
@@ -77,17 +77,21 @@ public function beforeSave(CategoryResourceModel $categoryResource, CategoryMode
7777
}
7878
});
7979

80-
return [$category];
80+
return $result;
8181
}
8282

8383
/**
8484
* @param CategoryResourceModel $categoryResource
85+
* @param CategoryResourceModel $result
8586
* @param CategoryModel $category
8687
*
87-
* @return CategoryModel[]
88+
* @return CategoryResourceModel
8889
*/
89-
public function beforeDelete(CategoryResourceModel $categoryResource, CategoryModel $category)
90-
{
90+
public function afterDelete(
91+
CategoryResourceModel $categoryResource,
92+
CategoryResourceModel $result,
93+
CategoryModel $category
94+
) {
9195
$categoryResource->addCommitCallback(function() use ($category) {
9296
// mview should be able to handle the changes for catalog_category_product relationship
9397
if (!$this->indexer->isScheduled()) {
@@ -99,7 +103,7 @@ public function beforeDelete(CategoryResourceModel $categoryResource, CategoryMo
99103
}
100104
});
101105

102-
return [$category];
106+
return $result;
103107
}
104108

105109
/**

Model/Indexer/ProductObserver.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,39 @@ public function __construct(IndexerRegistry $indexerRegistry, ConfigHelper $conf
2727
}
2828

2929
/**
30-
* Using "before" method here instead of "after", because M2.1 doesn't pass "$product" argument
31-
* to "after" methods. When M2.1 support will be removed, this method can be rewriten to:
32-
* afterSave(ProductResource $productResource, ProductResource $result, ProductModel $product)
33-
*
3430
* @param ProductResource $productResource
31+
* @param ProductResource $result
3532
* @param ProductModel $product
3633
*
3734
* @return ProductModel[]
3835
*/
39-
public function beforeSave(ProductResource $productResource, ProductModel $product)
36+
public function afterSave(ProductResource $productResource, ProductResource $result, ProductModel $product)
4037
{
4138
$productResource->addCommitCallback(function () use ($product) {
4239
if (!$this->indexer->isScheduled()) {
4340
$this->indexer->reindexRow($product->getId());
4441
}
4542
});
4643

47-
return [$product];
44+
return $result;
4845
}
4946

5047
/**
51-
* Using "before" method here instead of "after", because M2.1 doesn't pass "$product" argument
52-
* to "after" methods. When M2.1 support will be removed, this method can be rewriten to:
53-
* public function afterDelete(ProductResource $productResource, ProductResource $result, ProductModel $product)
54-
*
5548
* @param ProductResource $productResource
49+
* @param ProductResource $result
5650
* @param ProductModel $product
5751
*
5852
* @return ProductModel[]
5953
*/
60-
public function beforeDelete(ProductResource $productResource, ProductModel $product)
54+
public function afterDelete(ProductResource $productResource, ProductResource $result, ProductModel $product)
6155
{
6256
$productResource->addCommitCallback(function () use ($product) {
6357
if (!$this->indexer->isScheduled()) {
6458
$this->indexer->reindexRow($product->getId());
6559
}
6660
});
6761

68-
return [$product];
62+
return $result;
6963
}
7064

7165
/**

Model/Indexer/StockItemObserver.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public function __construct(IndexerRegistry $indexerRegistry)
1313
$this->indexer = $indexerRegistry->get('algolia_products');
1414
}
1515

16-
public function aroundSave(
16+
public function afterSave(
1717
\Magento\CatalogInventory\Model\ResourceModel\Stock\Item $stockItemModel,
18-
\Closure $proceed,
18+
\Magento\CatalogInventory\Model\ResourceModel\Stock\Item $result,
1919
\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem
2020
) {
2121
$stockItemModel->addCommitCallback(function () use ($stockItem) {
@@ -24,12 +24,12 @@ public function aroundSave(
2424
}
2525
});
2626

27-
return $proceed($stockItem);
27+
return $result;
2828
}
2929

30-
public function aroundDelete(
30+
public function afterDelete(
3131
\Magento\CatalogInventory\Model\ResourceModel\Stock\Item $stockItemResource,
32-
\Closure $proceed,
32+
\Magento\CatalogInventory\Model\ResourceModel\Stock\Item $result,
3333
\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem
3434
) {
3535
$stockItemResource->addCommitCallback(function () use ($stockItem) {
@@ -38,6 +38,6 @@ public function aroundDelete(
3838
}
3939
});
4040

41-
return $proceed($stockItem);
41+
return $result;
4242
}
4343
}

etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<plugin name="algoliaProductsMassAction" type="Algolia\AlgoliaSearch\Model\Indexer\ProductObserver"/>
88
</type>
99
<type name="Magento\CatalogInventory\Model\ResourceModel\Stock\Item">
10-
<plugin name="algoliaProducts" type="Algolia\AlgoliaSearch\Model\Indexer\StockItemObserver"/>
10+
<plugin name="algoliaStockItems" type="Algolia\AlgoliaSearch\Model\Indexer\StockItemObserver"/>
1111
</type>
1212

1313
<type name="Magento\Catalog\Model\ResourceModel\Category">

0 commit comments

Comments
 (0)