We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    Let’s say if you need to get all the products through the collection with out-of-stock products and send it in JSON format then what will you do?

    Step 1: Create a front controller GetProducts.php file

    <?php
    
    namespace MagemonkeysCustomInventoryControllerInventory;
    
    use MagentoFrameworkAppActionContext;
    use MagemonkeysCustomInventoryHelperData;
    
    
    class GetProducts extends MagentoFrameworkAppActionAction
    {
        public function __construct(
            Context $context,
            Data $helper
        ){
            $this->helper = $helper;
            parent::__construct($context);
        }
    
        public function execute()
        {
            $data = $this->helper->getProductData();
    
            header('Content-Type: application/json');
    
            echo json_encode(array(
                'data'=>$data,
                'meta'=> array(
                    'count' => count($data)
                )
            ));
    
            return;
    
        }
    }

     

    Step 2: Create a Data.php in the helper folder of your module and paste the below code into it.

    <?php
    
    namespace MagemonkeysCustomInventoryHelper;
    
    use MagentoFrameworkAppHelperAbstractHelper;
    use MagentoFrameworkAppFilesystemDirectoryList;
    use MagentoCatalogInventoryModelStockStockItemRepository; // Probably deprecating this with the following
    use MagentoCatalogInventoryApiStockRegistryInterface;
    
    
    class Data extends AbstractHelper
    {
        private $productFactory;
        private $productCollectionFactory;
        private $stockItemRepository;
        private $inflightStatuses;
        protected $stockRegistry;
    
    
        public function __construct(
            MagentoCatalogModelProductFactory $productFactory,
            StockItemRepository $stockItemRepository, // Probably deprecating this with the following
            StockRegistryInterface $stockRegistry,
            MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory
        )
        {
            $this->inflightStatuses = array('processing', 'fulfillment_picking', 'incomplete', 'holded', 'on_review');
            $this->productFactory = $productFactory;
            $this->productCollectionFactory = $productCollectionFactory;
            $this->stockItemRepository = $stockItemRepository; // Probably deprecating this with the following
            $this->stockRegistry = $stockRegistry;
        }
    
        public function getProductData()
        {
            $data = array();
    
            $collection = $this->productCollectionFactory->create();
            $collection->addAttributeToSelect('*');
            $collection->setPageSize(100000);
            $collection->setFlag('has_stock_status_filter', false);
    
    
            /*echo $collection->count();
            exit;*/
    
            $collection->load();
    
            /* Start to add out of stock products in collection */
            $collection->clear();
            
            $updatedWhere = array();
            $where = $collection->getSelect()->getPart('where');
            foreach ($where as $key => $condition)
            {
                if(strpos($condition, 'stock_status_index.stock_status = 1') !== false){
                    $updatedWhere[] = 'AND (stock_status_index.stock_status IN (1,0))';
                } else {
                    $updatedWhere[] = $condition;
                }   
            }
            $collection->getSelect()->setPart('where', $updatedWhere);
            /* End of to add out of stock products in collection */
    
            $collection->load();
    
            foreach ($collection as $product) {
    
                $sku = trim($product->getSku());
    
                //$stockItem = $this->stockItemRepository->get($product->getId());
                $stockItem = $this->stockRegistry->getStockItemBySku($sku);
    
    
                $data[$sku] = array(
                    'sku' => $sku,
                    'name' => $product->getName(),
                    'qty' => (int) $stockItem->getQty(),
                    'in_stock' => $stockItem->getIsInStock()
                );
            }
            return $data;
        }
    }

     

     

    Fill the below form if you need any Magento relate help/advise/consulting.

    With Only Agency that provides a 24/7 emergency support.

      Get a Free Quote