Let’s initiate a discussion!!
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?php namespace Magemonkeys\CustomInventory\Controller\Inventory; use Magento\Framework\App\Action\Context; use Magemonkeys\CustomInventory\Helper\Data; class GetProducts extends \Magento\Framework\App\Action\Action { 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?php namespace Magemonkeys\CustomInventory\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\CatalogInventory\Model\Stock\StockItemRepository; // Probably deprecating this with the following use Magento\CatalogInventory\Api\StockRegistryInterface; class Data extends AbstractHelper { private $productFactory; private $productCollectionFactory; private $stockItemRepository; private $inflightStatuses; protected $stockRegistry; public function __construct( \Magento\Catalog\Model\ProductFactory $productFactory, StockItemRepository $stockItemRepository, // Probably deprecating this with the following StockRegistryInterface $stockRegistry, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $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; } } |
[crayon-6421750f831cf438693043/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.