EmicoEcommerce / Magento2Tweakwise-archived

Magento 2 module for Tweakwise integration
Other
9 stars 25 forks source link

Out of stock Products are shown while "Display Out of Stock Products" is set to No #48

Closed duckchip closed 5 years ago

duckchip commented 6 years ago

He guys, We're having an issue with out-of-stock products shown at the product list while the setting to Display out-of-stock products is set to no. Environment

Environment

Steps to reproduce

  1. Enable tweakwise module
  2. Make sure the setting "Display Out of Stock Products" is set to No under Stores > Configuration > Catalog > Inventory
  3. Go to the product list page on frontend

Actual result

  1. Out of stock products are shown

Expected result

  1. Out of stock products are not shown

When we disable the tweakwise module, those products that are out of stock, are not shown anymore.

duckchip commented 6 years ago

We fixed this issue with following patch. This is logic that is also used in the core of magento. Don't know if this is the best solution.

Let me know what you guys think of it :)

diff --git a/src/Model/Catalog/Product/AbstractCollection.php b/src/Model/Catalog/Product/AbstractCollection.php
index d59682e..1834173 100644
--- a/src/Model/Catalog/Product/AbstractCollection.php
+++ b/src/Model/Catalog/Product/AbstractCollection.php
@@ -95,10 +95,33 @@ abstract class AbstractCollection extends ProductCollection

         $this->clearFilters();
         $this->applyEntityIdFilter();
-
+        $this->applyStockFilter();
         return $this;
     }

+
+    public function applyStockFilter(
+    ) {
+        if ($this->_isEnabledShowOutOfStock()) {
+            return;
+        }
+        $this->addFieldToFilter('quantity_and_stock_status', 1);
+    }
+
+    /**
+     * Get config value for 'display out of stock' option
+     *
+     * @return bool
+     */
+    protected function _isEnabledShowOutOfStock()
+    {
+        return $this->_scopeConfig->isSetFlag(
+            'cataloginventory/options/show_out_of_stock',
+            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
+        );
+    }
+
+
     /**
      * {@inheritdoc}
      */
hostep commented 6 years ago

Just to add some update, the fix @duckchip posted above does not fix the problem, at first we thought it fixed it but in the end it turned out it didn't fix it.

edwinljacobs commented 6 years ago

Hi,

Just a question regarding this issue: did you run the export after modifying the setting? The thing is that if the product is exported to Tweakwise then it will show regardless of which settings you modify. The point is that Tweakwise decides which products are visible and it does so based on the export. So if a product is exported to Tweakwise then it will be visible. At export time decisions are made based on stock and various other settings (such as visibility, product status, etc...) to determine if a product should be exported, if exported then it will be visible while navigating. In order to "refresh" navigation the export should run again. The reason we do not filter products at render time is that it would mess up pagers and the number of results as this is data that Tweakwise provides. Therefor it is important that the export runs frequently.

The conditions a product has to meet in order to be exported can be found at Model\Write\Products\ExportEntity.php lines 499 till 513.

Nevertheless we found an issue with determining if a product should be exported based on stock. This is should be fixed with: https://github.com/EmicoEcommerce/Magento2TweakwiseExport/commit/e60578d02b304068bfe1f6ed258f119661d62321

For testing purposes: this fix can be obtained using composer require emico/tweakwise-export "dev-test". After review this will be tagged and released.

With kind regards, Edwin Jacobs

hostep commented 6 years ago

Thanks @edwinljacobs

If the out of stock check is handled based on the exported data, then it might turn out that this problem is also caused by https://github.com/EmicoEcommerce/Magento2TweakwiseExport/issues/56, since if the wrong products data is send to Tweakwise, it wouldn't surprise me if the stock data was possibly also incorrect. If there is a fix for https://github.com/EmicoEcommerce/Magento2TweakwiseExport/issues/56, we can test this again to see if that was maybe the cause.

Thanks for the time you already put into this!

edwinljacobs commented 6 years ago

Yeah that could be the case. There is a fix for EmicoEcommerce/Magento2TweakwiseExport#56 which has been deployed to the test branch. This fix takes into account the sequence_product table. You should get that fix when updating the module.

edwinljacobs commented 6 years ago

This issue should be fixed, we just published release 1.4.0 where the data issue has been adressed