Skip to content

Commit 4eb40c0

Browse files
committed
Show products which are out of stock depending on the configuration for search results, category pages and autosuggest results
1 parent b18e0ee commit 4eb40c0

File tree

10 files changed

+74
-14
lines changed

10 files changed

+74
-14
lines changed

categories/src/etc/adminhtml/system.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
<comment>Value between 0 and 1. Smaller values mean more fuzzy search results.</comment>
3737
<validate>validate-number validate-zero-or-greater</validate>
3838
</field>
39+
<field id="show_outofstock" translate="label,comment" sortOrder="80" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
40+
<label>Show products which are out of stock</label>
41+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
42+
</field>
3943
</group>
4044
<group id="autosuggest">
4145
<field id="max_number_category_suggestions">

categories/src/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<max_number_results>10</max_number_results>
1111
<fuzzy_is_active>1</fuzzy_is_active>
1212
<fuzzy_sensitivity>0.75</fuzzy_sensitivity>
13+
<show_outofstock>1</show_outofstock>
1314
</category>
1415
</integernet_solr>
1516
</default>

categories/src/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="IntegerNet_SolrCategories" setup_version="0.1.0">
3+
<module name="IntegerNet_SolrCategories" setup_version="1.1.0">
44
<sequence>
55
<module name="IntegerNet_Solr"/>
66
</sequence>

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "",
44
"require": {
55
"php": "~5.6.0|^7.0",
6-
"integer-net/solr-base": "^2.0.0",
6+
"integer-net/solr-base": "^3.0.0",
77
"integer-net/solr-pro": "^1.0.0",
88
"integer-net/solr-magento2-autosuggest" : "^1.0.0",
99
"magento/module-catalog": "^101.0.0",

main/src/Model/Bridge/Config.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ public function getAutosuggestConfig()
210210
$this->_getConfig($prefix . 'max_number_cms_page_suggestions'),
211211
$this->_getConfigFlag($prefix . 'show_complete_category_path'),
212212
$this->_getConfigFlag($prefix . 'category_link_type'),
213-
(array)@unserialize($this->_getConfig($prefix . 'attribute_filter_suggestions'))
213+
(array)@unserialize($this->_getConfig($prefix . 'attribute_filter_suggestions')),
214+
$this->_getConfigFlag($prefix . 'show_outofstock')
214215
);
215216
}
216217
return $this->autosuggest;
@@ -265,7 +266,8 @@ public function getResultsConfig()
265266
$this->_getConfig($prefix . 'price_step_size'),
266267
$this->_getConfig($prefix . 'max_price'),
267268
$this->_getConfigFlag($prefix . 'use_custom_price_intervals'),
268-
explode(',', $this->_getConfig($prefix . 'custom_price_intervals'))
269+
explode(',', $this->_getConfig($prefix . 'custom_price_intervals')),
270+
$this->_getConfigFlag($prefix . 'show_outofstock')
269271
);
270272
}
271273
return $this->results;
@@ -307,7 +309,8 @@ public function getCategoryConfig()
307309
$this->_getConfigFlag($prefix . 'use_in_search_results'),
308310
$this->_getConfig($prefix . 'max_number_results'),
309311
$this->_getConfigFlag($prefix . 'fuzzy_is_active'),
310-
$this->_getConfig($prefix . 'fuzzy_sensitivity')
312+
$this->_getConfig($prefix . 'fuzzy_sensitivity'),
313+
$this->_getConfigFlag($prefix . 'show_outofstock')
311314
);
312315
}
313316
return $this->category;

main/src/Model/Bridge/Product.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
use IntegerNet\Solr\Implementor\Product as ProductInterface;
1515
use Magento\Catalog\Api\Data\ProductInterface as MagentoProductInterface;
1616
use Magento\Catalog\Model\Product as MagentoProduct;
17+
use Magento\CatalogInventory\Api\Data\StockItemInterface;
1718
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
1819
use Magento\CatalogInventory\Api\StockRegistryInterface;
20+
use Magento\Framework\App\Config\ScopeConfigInterface;
1921
use Magento\Framework\App\ObjectManager;
2022
use Magento\Framework\Event\ManagerInterface;
2123

@@ -48,22 +50,40 @@ class Product implements ProductInterface
4850
*/
4951
const PARAM_MAGENTO_PRODUCT = 'magentoProduct';
5052
const PARAM_STORE_ID = 'storeId';
53+
/**
54+
* @var StockRegistryInterface
55+
*/
56+
private $stockRegistry;
57+
/**
58+
* @var ScopeConfigInterface
59+
*/
60+
private $scopeConfig;
61+
5162
/**#@-*/
5263
/**
5364
* Note: needs concrete Product model class for attribute frontend model, which expects a data object (Magento 2.0).
5465
*
5566
* @param MagentoProduct $magentoProduct
5667
* @param AttributeRepository $attributeRepository
5768
* @param ManagerInterface $eventManager
69+
* @param StockRegistryInterface $stockRegistry
70+
* @param ScopeConfigInterface $scopeConfig
5871
* @param int|null $storeId store id for store specific values (null for default)
5972
*/
60-
public function __construct(MagentoProduct $magentoProduct, AttributeRepository $attributeRepository,
61-
ManagerInterface $eventManager, $storeId = null)
62-
{
73+
public function __construct(
74+
MagentoProduct $magentoProduct,
75+
AttributeRepository $attributeRepository,
76+
ManagerInterface $eventManager,
77+
StockRegistryInterface $stockRegistry,
78+
ScopeConfigInterface $scopeConfig,
79+
$storeId = null
80+
) {
6381
$this->magentoProduct = $magentoProduct;
6482
$this->attributeRepository = $attributeRepository;
6583
$this->eventManager = $eventManager;
6684
$this->storeId = $storeId;
85+
$this->stockRegistry = $stockRegistry;
86+
$this->scopeConfig = $scopeConfig;
6787
}
6888

6989
/**
@@ -89,10 +109,6 @@ public function isIndexable()
89109
if (! \in_array($this->magentoProduct->getStore()->getWebsiteId(), $this->magentoProduct->getWebsiteIds())) {
90110
return false;
91111
}
92-
// stock status joined on collection does not give stock item, just is_salable
93-
if (! $this->magentoProduct->getData('is_salable')) {
94-
return false;
95-
}
96112
if ($this->getMagentoProduct()->getExtensionAttributes()->getSolrExclude()) {
97113
return false;
98114
}
@@ -127,7 +143,14 @@ public function isVisibleInSearch()
127143

128144
public function getSolrBoost()
129145
{
130-
return $this->getMagentoProduct()->getExtensionAttributes()->getSolrBoost();
146+
$boost = $this->getMagentoProduct()->getExtensionAttributes()->getSolrBoost();
147+
if (!$this->isInStock()) {
148+
if ($boost === null) {
149+
$boost = 1;
150+
}
151+
$boost *= floatval($this->scopeConfig->getValue('integernet_solr/results/priority_outofstock', 'stores', $this->storeId));
152+
}
153+
return $boost;
131154
}
132155

133156
public function getPrice()
@@ -190,4 +213,14 @@ public function getSku()
190213
{
191214
return $this->getMagentoProduct()->getSku();
192215
}
216+
217+
/**
218+
* @return bool
219+
*/
220+
public function isInStock()
221+
{
222+
/** @var StockItemInterface $stockItem */
223+
$stockItem = $this->stockRegistry->getStockItem($this->getMagentoProduct()->getId(), $this->storeId);
224+
return (bool)$stockItem->getIsInStock();
225+
}
193226
}

main/src/etc/adminhtml/system.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@
167167
<comment>1 is default, use higher numbers for higher priority.</comment>
168168
<validate>validate-number validate-zero-or-greater</validate>
169169
</field>
170+
<field id="show_outofstock" translate="label,comment" sortOrder="27" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
171+
<label>Show products which are out of stock</label>
172+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
173+
</field>
174+
<field id="priority_outofstock" translate="label,comment" sortOrder="28" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
175+
<label>Solr Priority Multiplier for Products being out of Stock</label>
176+
<comment>0 = don't show at all, 1 = don't modify, anything between = lower priority</comment>
177+
<validate>validate-number validate-zero-or-greater</validate>
178+
</field>
170179
<field id="price_step_size" translate="label,comment" sortOrder="30" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
171180
<label>Size of Price Steps</label>
172181
<comment>i.e. 100</comment>
@@ -245,6 +254,10 @@
245254
<frontend_model>IntegerNet\Solr\Block\Config\AttributeFilterSuggestions</frontend_model>
246255
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
247256
</field>
257+
<field id="show_outofstock" translate="label,comment" sortOrder="90" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
258+
<label>Show products which are out of stock</label>
259+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
260+
</field>
248261
</group>
249262
<!--
250263
<group id="seo" translate="label,comment" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">

main/src/etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
<search_operator>AND</search_operator>
3232
<filter_position>1</filter_position>
3333
<priority_categories>2</priority_categories>
34+
<show_outofstock>1</show_outofstock>
35+
<priority_outofstock>1</priority_outofstock>
3436
<price_step_size>10</price_step_size>
3537
<max_price>200</max_price>
3638
<use_custom_price_intervals>0</use_custom_price_intervals>
@@ -48,6 +50,7 @@
4850
<show_complete_category_path>0</show_complete_category_path>
4951
<category_link_type>filter</category_link_type>
5052
<attribute_filter_suggestions>a:0:{}</attribute_filter_suggestions>
53+
<show_outofstock>1</show_outofstock>
5154
</autosuggest>
5255
<seo>
5356
<hide_from_robots>search_results_all,search_results_filtered,categories_filtered</hide_from_robots>

main/src/etc/module.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
3-
<module name="IntegerNet_Solr" setup_version="1.2.0">
3+
<module name="IntegerNet_Solr" setup_version="1.3.0">
44
<sequence>
55
<module name="Magento_Search"/>
66
<module name="Magento_CatalogSearch"/>

main/src/i18n/de_DE.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,6 @@
185185
"Show complete category path","Kompletten Kategorie-Pfad anzeigen"
186186
"Use HTML from Solr Index","HTML vom Solr-Index verwenden"
187187
"Advantages: faster search<br />Disadvantages: slower indexing, no user dependant results","Vorteile: schnellere Suche<br />Nachteile: Langsamere Indizierung, keine benutzerabhängigen Suchergebnisse"
188+
"Show products which are out of stock","Produkte anzeigen, die nicht auf Lager sind"
189+
"Solr Priority Multiplier for Products being out of Stock","Solr-Prioritäts-Multiplikator für ausverkaufte Produkte"
190+
"0 = don't show at all, 1 = don't modify, anything between = lower priority","0 = gar nicht anzeigen, 1 = wie Produkte auf Lager, alles dazwischen = niedrigere Priorität"

0 commit comments

Comments
 (0)