cataloginventory_stock_status randomly has missing entriesProducts out of stock with inventoryMagento Search...

The effect of fishing on total land area needed to feed an island settlement

Is there a verb that means to inject with poison?

Why do we have to make "peinlich" start with a capital letter and also end with -s in this sentence?

How to not let the Identify spell spoil everything?

False written accusations not made public - is there law to cover this?

Removing whitespace between consecutive numbers

In Linux what happens if 1000 files in a directory are moved to another location while another 300 files were added to the source directory?

Count repetitions of an array

Has Britain negotiated with any other countries outside the EU in preparation for the exit?

The probability of reaching the absorbing states from a particular transient state?

Do authors have to be politically correct in article-writing?

Translation needed for 130 years old church document

Existence of Riemann surface, holomorphic maps

Sprint is 2 week and 40-stories

What is a good reason for every spaceship to carry a weapon on board?

How would an AI self awareness kill switch work?

What language shall they sing in?

I have trouble understanding this fallacy: "If A, then B. Therefore if not-B, then not-A."

Why avoid shared user accounts?

A Missing Symbol for This Logo

Square Root Distance from Integers

What is the difference between "...", '...', $'...', and $"..." quotes?

What is the industry term for house wiring diagrams?

What game did these black and yellow dice come from?



cataloginventory_stock_status randomly has missing entries


Products out of stock with inventoryMagento Search returning no resultsConfigurable options disappearing from front end after stock status re-indexMagento 1.9.1 cron_schedule is not picked for everCron Constantly RunningCron Permissions Error Magento2Stock management problems with downloadable products added programmatically (Magento 1.9.2.2)M2.1.3 - Importing a configurable product doesn't work, but adding via admin area works fineMagento2 Cron job not runningMagento 2.2.3 Cron job won't run. Status is missed. One is stuck in running. How to fix?













1















What could cause cataloginventory_stock_status to have missing entries I have to fix manually? It makes the product appear out of stock even though it isn't. I've tried replicating by flushing, cleaning, and re-indexing and running cron but none of these cause the missing entries. The products will exist for weeks, even months and just randomly have this issue.










share|improve this question





























    1















    What could cause cataloginventory_stock_status to have missing entries I have to fix manually? It makes the product appear out of stock even though it isn't. I've tried replicating by flushing, cleaning, and re-indexing and running cron but none of these cause the missing entries. The products will exist for weeks, even months and just randomly have this issue.










    share|improve this question



























      1












      1








      1








      What could cause cataloginventory_stock_status to have missing entries I have to fix manually? It makes the product appear out of stock even though it isn't. I've tried replicating by flushing, cleaning, and re-indexing and running cron but none of these cause the missing entries. The products will exist for weeks, even months and just randomly have this issue.










      share|improve this question
















      What could cause cataloginventory_stock_status to have missing entries I have to fix manually? It makes the product appear out of stock even though it isn't. I've tried replicating by flushing, cleaning, and re-indexing and running cron but none of these cause the missing entries. The products will exist for weeks, even months and just randomly have this issue.







      magento2 cron catalogsearch stock






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 19 '18 at 6:25









      Nagaraju Kasa

      2,63221539




      2,63221539










      asked Dec 19 '18 at 0:49









      WolfeWolfe

      251113




      251113






















          1 Answer
          1






          active

          oldest

          votes


















          0














          So far, unfortunately the only true answer I have found is to write my own cataloginventory_stock indexer.



          Note Vendor/Module is for whatever your custom module is, plugin your own names.



          In your Vendor/Module/etc/di.xml file



          <preference for="MagentoCatalogInventoryModelIndexerStockActionFull" type="VendorModuleIndexerCatalogInventoryFull" />


          Vendor/Module/Indexer/CatalogInventoryFull.php



          <?php
          namespace VendorModuleIndexer;

          use MagentoCatalogModelResourceModelIndexerActiveTableSwitcher;
          use MagentoFrameworkAppResourceConnection;
          use MagentoCatalogInventoryModelResourceModelIndexerStockFactory;
          use MagentoCatalogModelProductType as ProductType;
          use MagentoFrameworkIndexerCacheContext;
          use MagentoFrameworkEventManagerInterface as EventManager;
          use MagentoFrameworkEntityManagerMetadataPool;
          use MagentoFrameworkIndexerBatchSizeManagementInterface;
          use MagentoFrameworkIndexerBatchProviderInterface;
          use MagentoFrameworkAppObjectManager;
          use MagentoFrameworkExceptionLocalizedException;
          use MagentoCatalogInventoryModelIndexerStockAbstractAction;
          use MagentoCatalogInventoryModelResourceModelIndexerStockStockInterface;

          class CatalogInventoryFull extends MagentoCatalogInventoryModelIndexerStockActionFull {
          public function execute($ids = null){
          //custom indexer code goes here
          return;
          }
          }


          As for the custom indexer code it will vary for you. Basically however you decide to determine stock and stock status is fine. Here's my script but it depends on there being the amasty multiinventory warehouse module.



          <?php
          require_once("bootstrapm2.php");

          //now use the total field to make sure the cataloginventory_stock_status and cataloginventory_stock_status_idx tables are correct
          $sql = "select product_id,available_qty from amasty_multiinventory_warehouse_item where warehouse_id=1";
          $rows = $connection->fetchAll($sql);
          $count = 0;
          $missing = $fixed = $unchanged = 0;
          $missing2 = $fixed2 = $unchanged2 = 0;
          $sql = "SET FOREIGN_KEY_CHECKS=0;";
          $connection->query($sql);
          foreach($rows as $k=>$v){
          echo "working on $pidn";
          $pid = $v['product_id'];
          $qty = (int)$v['available_qty'];
          $sql = "select * from cataloginventory_stock_status where product_id=$pid";
          $r = $connection->fetchAll($sql);
          if(!empty($r)) $r = $r[0];

          $status = 0;
          if($qty > 0) $status = 1;
          if(empty($r)){
          $missing++;
          $sql = "insert into cataloginventory_stock_status values($pid,0,1,$qty,$status)";
          echo "n$sqln";
          $connection->query($sql);
          }
          else if($r['qty'] !== $qty || ($qty > 0 && $r['stock_status'] == 0) || ($qty == 0 && $r['stock_status']==1)){
          $fixed++;
          $sql = "replace into cataloginventory_stock_status values($pid,0,1,$qty,$status)";
          echo "n$sqln";
          $connection->query($sql);
          }
          else $unchanged++;

          $sql = "select * from cataloginventory_stock_item where product_id=$pid";
          $r2 = $connection->fetchAll($sql);
          if(!empty($r2)) $r2 = $r2[0];

          if(empty($r2)){
          $missing2++;
          $sql = "insert into cataloginventory_stock_item values(null,$pid,1,$qty,0,1,0,0,1,1,1,10000,1,1,null,1,1,1,1,0,1,1,1,0,0,0)";
          echo "n$sqln";
          $connection->query($sql);
          }
          else if($qty !== $r2['qty']){
          $fixed2++;
          $sql = "update cataloginventory_stock_item set qty=$qty where product_id=$pid";
          echo "n$qty != $r2[qty] r2n";
          echo "n$sqln";
          $connection->query($sql);
          }
          else $unchanged2++;

          $count++;
          }

          $sql = "truncate cataloginventory_stock_status_idx";
          $connection->query($sql);
          $sql = "insert into cataloginventory_stock_status_idx select * from cataloginventory_stock_status";
          $connection->query($sql);
          echo "nrebuilt cataloginventory_stock_status_idxn";

          $sql = "SET FOREIGN_KEY_CHECKS=1;";
          $connection->query($sql);
          echo "ncataloginventory_stock_status $missing missing $fixed fixed $unchanged unchangedn";
          echo "ncataloginventory_stock_item $missing2 missing $fixed2 fixed $unchanged2 unchangedn";
          }
          $sql = "update cataloginventory_stock_status set stock_status=1 where qty > 0";
          echo "$sqln";
          $connection->query($sql);
          $sql = "update cataloginventory_stock_status_idx set stock_status=1 where qty > 0";
          echo "$sqln";
          $connection->query($sql);


          This is actually called by my script above via exec, because this is also a standalone cron script if I need it to be. I haven't had issues with the stock status since this change and inventory levels are actually correct now. The bootstrapm2 script it calls is really long, but anything that can just setup the dependencies and bootstrap m2 is fine.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f255117%2fcataloginventory-stock-status-randomly-has-missing-entries%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            So far, unfortunately the only true answer I have found is to write my own cataloginventory_stock indexer.



            Note Vendor/Module is for whatever your custom module is, plugin your own names.



            In your Vendor/Module/etc/di.xml file



            <preference for="MagentoCatalogInventoryModelIndexerStockActionFull" type="VendorModuleIndexerCatalogInventoryFull" />


            Vendor/Module/Indexer/CatalogInventoryFull.php



            <?php
            namespace VendorModuleIndexer;

            use MagentoCatalogModelResourceModelIndexerActiveTableSwitcher;
            use MagentoFrameworkAppResourceConnection;
            use MagentoCatalogInventoryModelResourceModelIndexerStockFactory;
            use MagentoCatalogModelProductType as ProductType;
            use MagentoFrameworkIndexerCacheContext;
            use MagentoFrameworkEventManagerInterface as EventManager;
            use MagentoFrameworkEntityManagerMetadataPool;
            use MagentoFrameworkIndexerBatchSizeManagementInterface;
            use MagentoFrameworkIndexerBatchProviderInterface;
            use MagentoFrameworkAppObjectManager;
            use MagentoFrameworkExceptionLocalizedException;
            use MagentoCatalogInventoryModelIndexerStockAbstractAction;
            use MagentoCatalogInventoryModelResourceModelIndexerStockStockInterface;

            class CatalogInventoryFull extends MagentoCatalogInventoryModelIndexerStockActionFull {
            public function execute($ids = null){
            //custom indexer code goes here
            return;
            }
            }


            As for the custom indexer code it will vary for you. Basically however you decide to determine stock and stock status is fine. Here's my script but it depends on there being the amasty multiinventory warehouse module.



            <?php
            require_once("bootstrapm2.php");

            //now use the total field to make sure the cataloginventory_stock_status and cataloginventory_stock_status_idx tables are correct
            $sql = "select product_id,available_qty from amasty_multiinventory_warehouse_item where warehouse_id=1";
            $rows = $connection->fetchAll($sql);
            $count = 0;
            $missing = $fixed = $unchanged = 0;
            $missing2 = $fixed2 = $unchanged2 = 0;
            $sql = "SET FOREIGN_KEY_CHECKS=0;";
            $connection->query($sql);
            foreach($rows as $k=>$v){
            echo "working on $pidn";
            $pid = $v['product_id'];
            $qty = (int)$v['available_qty'];
            $sql = "select * from cataloginventory_stock_status where product_id=$pid";
            $r = $connection->fetchAll($sql);
            if(!empty($r)) $r = $r[0];

            $status = 0;
            if($qty > 0) $status = 1;
            if(empty($r)){
            $missing++;
            $sql = "insert into cataloginventory_stock_status values($pid,0,1,$qty,$status)";
            echo "n$sqln";
            $connection->query($sql);
            }
            else if($r['qty'] !== $qty || ($qty > 0 && $r['stock_status'] == 0) || ($qty == 0 && $r['stock_status']==1)){
            $fixed++;
            $sql = "replace into cataloginventory_stock_status values($pid,0,1,$qty,$status)";
            echo "n$sqln";
            $connection->query($sql);
            }
            else $unchanged++;

            $sql = "select * from cataloginventory_stock_item where product_id=$pid";
            $r2 = $connection->fetchAll($sql);
            if(!empty($r2)) $r2 = $r2[0];

            if(empty($r2)){
            $missing2++;
            $sql = "insert into cataloginventory_stock_item values(null,$pid,1,$qty,0,1,0,0,1,1,1,10000,1,1,null,1,1,1,1,0,1,1,1,0,0,0)";
            echo "n$sqln";
            $connection->query($sql);
            }
            else if($qty !== $r2['qty']){
            $fixed2++;
            $sql = "update cataloginventory_stock_item set qty=$qty where product_id=$pid";
            echo "n$qty != $r2[qty] r2n";
            echo "n$sqln";
            $connection->query($sql);
            }
            else $unchanged2++;

            $count++;
            }

            $sql = "truncate cataloginventory_stock_status_idx";
            $connection->query($sql);
            $sql = "insert into cataloginventory_stock_status_idx select * from cataloginventory_stock_status";
            $connection->query($sql);
            echo "nrebuilt cataloginventory_stock_status_idxn";

            $sql = "SET FOREIGN_KEY_CHECKS=1;";
            $connection->query($sql);
            echo "ncataloginventory_stock_status $missing missing $fixed fixed $unchanged unchangedn";
            echo "ncataloginventory_stock_item $missing2 missing $fixed2 fixed $unchanged2 unchangedn";
            }
            $sql = "update cataloginventory_stock_status set stock_status=1 where qty > 0";
            echo "$sqln";
            $connection->query($sql);
            $sql = "update cataloginventory_stock_status_idx set stock_status=1 where qty > 0";
            echo "$sqln";
            $connection->query($sql);


            This is actually called by my script above via exec, because this is also a standalone cron script if I need it to be. I haven't had issues with the stock status since this change and inventory levels are actually correct now. The bootstrapm2 script it calls is really long, but anything that can just setup the dependencies and bootstrap m2 is fine.






            share|improve this answer




























              0














              So far, unfortunately the only true answer I have found is to write my own cataloginventory_stock indexer.



              Note Vendor/Module is for whatever your custom module is, plugin your own names.



              In your Vendor/Module/etc/di.xml file



              <preference for="MagentoCatalogInventoryModelIndexerStockActionFull" type="VendorModuleIndexerCatalogInventoryFull" />


              Vendor/Module/Indexer/CatalogInventoryFull.php



              <?php
              namespace VendorModuleIndexer;

              use MagentoCatalogModelResourceModelIndexerActiveTableSwitcher;
              use MagentoFrameworkAppResourceConnection;
              use MagentoCatalogInventoryModelResourceModelIndexerStockFactory;
              use MagentoCatalogModelProductType as ProductType;
              use MagentoFrameworkIndexerCacheContext;
              use MagentoFrameworkEventManagerInterface as EventManager;
              use MagentoFrameworkEntityManagerMetadataPool;
              use MagentoFrameworkIndexerBatchSizeManagementInterface;
              use MagentoFrameworkIndexerBatchProviderInterface;
              use MagentoFrameworkAppObjectManager;
              use MagentoFrameworkExceptionLocalizedException;
              use MagentoCatalogInventoryModelIndexerStockAbstractAction;
              use MagentoCatalogInventoryModelResourceModelIndexerStockStockInterface;

              class CatalogInventoryFull extends MagentoCatalogInventoryModelIndexerStockActionFull {
              public function execute($ids = null){
              //custom indexer code goes here
              return;
              }
              }


              As for the custom indexer code it will vary for you. Basically however you decide to determine stock and stock status is fine. Here's my script but it depends on there being the amasty multiinventory warehouse module.



              <?php
              require_once("bootstrapm2.php");

              //now use the total field to make sure the cataloginventory_stock_status and cataloginventory_stock_status_idx tables are correct
              $sql = "select product_id,available_qty from amasty_multiinventory_warehouse_item where warehouse_id=1";
              $rows = $connection->fetchAll($sql);
              $count = 0;
              $missing = $fixed = $unchanged = 0;
              $missing2 = $fixed2 = $unchanged2 = 0;
              $sql = "SET FOREIGN_KEY_CHECKS=0;";
              $connection->query($sql);
              foreach($rows as $k=>$v){
              echo "working on $pidn";
              $pid = $v['product_id'];
              $qty = (int)$v['available_qty'];
              $sql = "select * from cataloginventory_stock_status where product_id=$pid";
              $r = $connection->fetchAll($sql);
              if(!empty($r)) $r = $r[0];

              $status = 0;
              if($qty > 0) $status = 1;
              if(empty($r)){
              $missing++;
              $sql = "insert into cataloginventory_stock_status values($pid,0,1,$qty,$status)";
              echo "n$sqln";
              $connection->query($sql);
              }
              else if($r['qty'] !== $qty || ($qty > 0 && $r['stock_status'] == 0) || ($qty == 0 && $r['stock_status']==1)){
              $fixed++;
              $sql = "replace into cataloginventory_stock_status values($pid,0,1,$qty,$status)";
              echo "n$sqln";
              $connection->query($sql);
              }
              else $unchanged++;

              $sql = "select * from cataloginventory_stock_item where product_id=$pid";
              $r2 = $connection->fetchAll($sql);
              if(!empty($r2)) $r2 = $r2[0];

              if(empty($r2)){
              $missing2++;
              $sql = "insert into cataloginventory_stock_item values(null,$pid,1,$qty,0,1,0,0,1,1,1,10000,1,1,null,1,1,1,1,0,1,1,1,0,0,0)";
              echo "n$sqln";
              $connection->query($sql);
              }
              else if($qty !== $r2['qty']){
              $fixed2++;
              $sql = "update cataloginventory_stock_item set qty=$qty where product_id=$pid";
              echo "n$qty != $r2[qty] r2n";
              echo "n$sqln";
              $connection->query($sql);
              }
              else $unchanged2++;

              $count++;
              }

              $sql = "truncate cataloginventory_stock_status_idx";
              $connection->query($sql);
              $sql = "insert into cataloginventory_stock_status_idx select * from cataloginventory_stock_status";
              $connection->query($sql);
              echo "nrebuilt cataloginventory_stock_status_idxn";

              $sql = "SET FOREIGN_KEY_CHECKS=1;";
              $connection->query($sql);
              echo "ncataloginventory_stock_status $missing missing $fixed fixed $unchanged unchangedn";
              echo "ncataloginventory_stock_item $missing2 missing $fixed2 fixed $unchanged2 unchangedn";
              }
              $sql = "update cataloginventory_stock_status set stock_status=1 where qty > 0";
              echo "$sqln";
              $connection->query($sql);
              $sql = "update cataloginventory_stock_status_idx set stock_status=1 where qty > 0";
              echo "$sqln";
              $connection->query($sql);


              This is actually called by my script above via exec, because this is also a standalone cron script if I need it to be. I haven't had issues with the stock status since this change and inventory levels are actually correct now. The bootstrapm2 script it calls is really long, but anything that can just setup the dependencies and bootstrap m2 is fine.






              share|improve this answer


























                0












                0








                0







                So far, unfortunately the only true answer I have found is to write my own cataloginventory_stock indexer.



                Note Vendor/Module is for whatever your custom module is, plugin your own names.



                In your Vendor/Module/etc/di.xml file



                <preference for="MagentoCatalogInventoryModelIndexerStockActionFull" type="VendorModuleIndexerCatalogInventoryFull" />


                Vendor/Module/Indexer/CatalogInventoryFull.php



                <?php
                namespace VendorModuleIndexer;

                use MagentoCatalogModelResourceModelIndexerActiveTableSwitcher;
                use MagentoFrameworkAppResourceConnection;
                use MagentoCatalogInventoryModelResourceModelIndexerStockFactory;
                use MagentoCatalogModelProductType as ProductType;
                use MagentoFrameworkIndexerCacheContext;
                use MagentoFrameworkEventManagerInterface as EventManager;
                use MagentoFrameworkEntityManagerMetadataPool;
                use MagentoFrameworkIndexerBatchSizeManagementInterface;
                use MagentoFrameworkIndexerBatchProviderInterface;
                use MagentoFrameworkAppObjectManager;
                use MagentoFrameworkExceptionLocalizedException;
                use MagentoCatalogInventoryModelIndexerStockAbstractAction;
                use MagentoCatalogInventoryModelResourceModelIndexerStockStockInterface;

                class CatalogInventoryFull extends MagentoCatalogInventoryModelIndexerStockActionFull {
                public function execute($ids = null){
                //custom indexer code goes here
                return;
                }
                }


                As for the custom indexer code it will vary for you. Basically however you decide to determine stock and stock status is fine. Here's my script but it depends on there being the amasty multiinventory warehouse module.



                <?php
                require_once("bootstrapm2.php");

                //now use the total field to make sure the cataloginventory_stock_status and cataloginventory_stock_status_idx tables are correct
                $sql = "select product_id,available_qty from amasty_multiinventory_warehouse_item where warehouse_id=1";
                $rows = $connection->fetchAll($sql);
                $count = 0;
                $missing = $fixed = $unchanged = 0;
                $missing2 = $fixed2 = $unchanged2 = 0;
                $sql = "SET FOREIGN_KEY_CHECKS=0;";
                $connection->query($sql);
                foreach($rows as $k=>$v){
                echo "working on $pidn";
                $pid = $v['product_id'];
                $qty = (int)$v['available_qty'];
                $sql = "select * from cataloginventory_stock_status where product_id=$pid";
                $r = $connection->fetchAll($sql);
                if(!empty($r)) $r = $r[0];

                $status = 0;
                if($qty > 0) $status = 1;
                if(empty($r)){
                $missing++;
                $sql = "insert into cataloginventory_stock_status values($pid,0,1,$qty,$status)";
                echo "n$sqln";
                $connection->query($sql);
                }
                else if($r['qty'] !== $qty || ($qty > 0 && $r['stock_status'] == 0) || ($qty == 0 && $r['stock_status']==1)){
                $fixed++;
                $sql = "replace into cataloginventory_stock_status values($pid,0,1,$qty,$status)";
                echo "n$sqln";
                $connection->query($sql);
                }
                else $unchanged++;

                $sql = "select * from cataloginventory_stock_item where product_id=$pid";
                $r2 = $connection->fetchAll($sql);
                if(!empty($r2)) $r2 = $r2[0];

                if(empty($r2)){
                $missing2++;
                $sql = "insert into cataloginventory_stock_item values(null,$pid,1,$qty,0,1,0,0,1,1,1,10000,1,1,null,1,1,1,1,0,1,1,1,0,0,0)";
                echo "n$sqln";
                $connection->query($sql);
                }
                else if($qty !== $r2['qty']){
                $fixed2++;
                $sql = "update cataloginventory_stock_item set qty=$qty where product_id=$pid";
                echo "n$qty != $r2[qty] r2n";
                echo "n$sqln";
                $connection->query($sql);
                }
                else $unchanged2++;

                $count++;
                }

                $sql = "truncate cataloginventory_stock_status_idx";
                $connection->query($sql);
                $sql = "insert into cataloginventory_stock_status_idx select * from cataloginventory_stock_status";
                $connection->query($sql);
                echo "nrebuilt cataloginventory_stock_status_idxn";

                $sql = "SET FOREIGN_KEY_CHECKS=1;";
                $connection->query($sql);
                echo "ncataloginventory_stock_status $missing missing $fixed fixed $unchanged unchangedn";
                echo "ncataloginventory_stock_item $missing2 missing $fixed2 fixed $unchanged2 unchangedn";
                }
                $sql = "update cataloginventory_stock_status set stock_status=1 where qty > 0";
                echo "$sqln";
                $connection->query($sql);
                $sql = "update cataloginventory_stock_status_idx set stock_status=1 where qty > 0";
                echo "$sqln";
                $connection->query($sql);


                This is actually called by my script above via exec, because this is also a standalone cron script if I need it to be. I haven't had issues with the stock status since this change and inventory levels are actually correct now. The bootstrapm2 script it calls is really long, but anything that can just setup the dependencies and bootstrap m2 is fine.






                share|improve this answer













                So far, unfortunately the only true answer I have found is to write my own cataloginventory_stock indexer.



                Note Vendor/Module is for whatever your custom module is, plugin your own names.



                In your Vendor/Module/etc/di.xml file



                <preference for="MagentoCatalogInventoryModelIndexerStockActionFull" type="VendorModuleIndexerCatalogInventoryFull" />


                Vendor/Module/Indexer/CatalogInventoryFull.php



                <?php
                namespace VendorModuleIndexer;

                use MagentoCatalogModelResourceModelIndexerActiveTableSwitcher;
                use MagentoFrameworkAppResourceConnection;
                use MagentoCatalogInventoryModelResourceModelIndexerStockFactory;
                use MagentoCatalogModelProductType as ProductType;
                use MagentoFrameworkIndexerCacheContext;
                use MagentoFrameworkEventManagerInterface as EventManager;
                use MagentoFrameworkEntityManagerMetadataPool;
                use MagentoFrameworkIndexerBatchSizeManagementInterface;
                use MagentoFrameworkIndexerBatchProviderInterface;
                use MagentoFrameworkAppObjectManager;
                use MagentoFrameworkExceptionLocalizedException;
                use MagentoCatalogInventoryModelIndexerStockAbstractAction;
                use MagentoCatalogInventoryModelResourceModelIndexerStockStockInterface;

                class CatalogInventoryFull extends MagentoCatalogInventoryModelIndexerStockActionFull {
                public function execute($ids = null){
                //custom indexer code goes here
                return;
                }
                }


                As for the custom indexer code it will vary for you. Basically however you decide to determine stock and stock status is fine. Here's my script but it depends on there being the amasty multiinventory warehouse module.



                <?php
                require_once("bootstrapm2.php");

                //now use the total field to make sure the cataloginventory_stock_status and cataloginventory_stock_status_idx tables are correct
                $sql = "select product_id,available_qty from amasty_multiinventory_warehouse_item where warehouse_id=1";
                $rows = $connection->fetchAll($sql);
                $count = 0;
                $missing = $fixed = $unchanged = 0;
                $missing2 = $fixed2 = $unchanged2 = 0;
                $sql = "SET FOREIGN_KEY_CHECKS=0;";
                $connection->query($sql);
                foreach($rows as $k=>$v){
                echo "working on $pidn";
                $pid = $v['product_id'];
                $qty = (int)$v['available_qty'];
                $sql = "select * from cataloginventory_stock_status where product_id=$pid";
                $r = $connection->fetchAll($sql);
                if(!empty($r)) $r = $r[0];

                $status = 0;
                if($qty > 0) $status = 1;
                if(empty($r)){
                $missing++;
                $sql = "insert into cataloginventory_stock_status values($pid,0,1,$qty,$status)";
                echo "n$sqln";
                $connection->query($sql);
                }
                else if($r['qty'] !== $qty || ($qty > 0 && $r['stock_status'] == 0) || ($qty == 0 && $r['stock_status']==1)){
                $fixed++;
                $sql = "replace into cataloginventory_stock_status values($pid,0,1,$qty,$status)";
                echo "n$sqln";
                $connection->query($sql);
                }
                else $unchanged++;

                $sql = "select * from cataloginventory_stock_item where product_id=$pid";
                $r2 = $connection->fetchAll($sql);
                if(!empty($r2)) $r2 = $r2[0];

                if(empty($r2)){
                $missing2++;
                $sql = "insert into cataloginventory_stock_item values(null,$pid,1,$qty,0,1,0,0,1,1,1,10000,1,1,null,1,1,1,1,0,1,1,1,0,0,0)";
                echo "n$sqln";
                $connection->query($sql);
                }
                else if($qty !== $r2['qty']){
                $fixed2++;
                $sql = "update cataloginventory_stock_item set qty=$qty where product_id=$pid";
                echo "n$qty != $r2[qty] r2n";
                echo "n$sqln";
                $connection->query($sql);
                }
                else $unchanged2++;

                $count++;
                }

                $sql = "truncate cataloginventory_stock_status_idx";
                $connection->query($sql);
                $sql = "insert into cataloginventory_stock_status_idx select * from cataloginventory_stock_status";
                $connection->query($sql);
                echo "nrebuilt cataloginventory_stock_status_idxn";

                $sql = "SET FOREIGN_KEY_CHECKS=1;";
                $connection->query($sql);
                echo "ncataloginventory_stock_status $missing missing $fixed fixed $unchanged unchangedn";
                echo "ncataloginventory_stock_item $missing2 missing $fixed2 fixed $unchanged2 unchangedn";
                }
                $sql = "update cataloginventory_stock_status set stock_status=1 where qty > 0";
                echo "$sqln";
                $connection->query($sql);
                $sql = "update cataloginventory_stock_status_idx set stock_status=1 where qty > 0";
                echo "$sqln";
                $connection->query($sql);


                This is actually called by my script above via exec, because this is also a standalone cron script if I need it to be. I haven't had issues with the stock status since this change and inventory levels are actually correct now. The bootstrapm2 script it calls is really long, but anything that can just setup the dependencies and bootstrap m2 is fine.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                WolfeWolfe

                251113




                251113






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f255117%2fcataloginventory-stock-status-randomly-has-missing-entries%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

                    How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

                    夢乃愛華...