1
+ <?php
2
+
3
+ /*
4
+ *
5
+ * This script help synchronize Quantity with Salable Quantity
6
+ * Run command: php ProductSynchronizeMSI.php
7
+ *
8
+ */
9
+
10
+ require dirname (__FILE__ ) . '/app/bootstrap.php ' ;
11
+ $ bootstrap = \Magento \Framework \App \Bootstrap::create (BP , $ _SERVER );
12
+
13
+ class Outslide extends \Magento \Framework \App \Http
14
+ implements \Magento \Framework \AppInterface {
15
+ public function launch ()
16
+ {
17
+ $ state = $ this ->_objectManager ->get ('Magento\Framework\App\State ' );
18
+ $ state ->setAreaCode ('adminhtml ' );
19
+ $ myClass = $ this ->_objectManager ->create ('ProductSalableQuantity ' );
20
+ $ myClass ->updateSalableQuantity ();
21
+ return $ this ->_response ;
22
+ }
23
+
24
+ }
25
+
26
+
27
+ class ProductSalableQuantity
28
+ {
29
+
30
+
31
+ /**
32
+ * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
33
+ */
34
+ protected $ productCollectionFactory ;
35
+
36
+ /**
37
+ * @var \Magento\InventoryCatalogApi\Model\SourceItemsProcessorInterface
38
+ */
39
+ protected $ sourceItemsProcessor ;
40
+
41
+ public function __construct (
42
+ \Magento \Catalog \Model \ResourceModel \Product \CollectionFactory $ productCollectionFactory ,
43
+ \Magento \InventoryCatalogApi \Model \SourceItemsProcessorInterface $ sourceItemsProcessor
44
+ ) {
45
+ $ this ->productCollectionFactory = $ productCollectionFactory ;
46
+ $ this ->sourceItemsProcessor = $ sourceItemsProcessor ;
47
+ }
48
+
49
+ public function updateSalableQuantity () {
50
+
51
+ $ sourceData = [
52
+ ['source_code ' =>'default ' , 'status ' =>1 , 'quantity ' =>50 ],
53
+ ];
54
+ $ productsSKU = ['SRC081MASU ' ];
55
+
56
+ $ collection = $ this ->productCollectionFactory ->create ()
57
+ ->joinField ('qty ' ,
58
+ 'cataloginventory_stock_item ' ,
59
+ 'qty ' ,
60
+ 'product_id=entity_id ' ,
61
+ '{{table}}.stock_id=1 ' ,
62
+ 'left '
63
+ )->joinTable ('cataloginventory_stock_item ' , 'product_id = entity_id ' , ['stock_status ' => 'is_in_stock ' ])
64
+ ->addAttributeToSelect ('* ' )
65
+ ->addAttributeToSelect ('sku ' );
66
+ if ($ productsSKU !== '* ' && is_array ($ productsSKU )){
67
+ $ collection ->addAttributeToFilter ('sku ' , ['in ' => $ productsSKU ]);
68
+ }
69
+ try {
70
+ $ total = 0 ;
71
+ foreach ($ collection as $ product ) {
72
+ /* clone array */
73
+ $ data = array_merge ($ sourceData , []);
74
+ foreach ($ data as $ key => $ source ){
75
+ $ qty = (int ) $ product ->getQty ();
76
+ $ dataUpdate = [
77
+ 'quantity ' => $ qty ,
78
+ 'status ' => $ qty ? 1 : 0 ,
79
+ ];
80
+ $ data [$ key ] = array_merge ($ source , $ dataUpdate );
81
+ }
82
+ $ sku = $ product ->getSku ();
83
+ $ this ->sourceItemsProcessor ->execute (
84
+ $ sku ,
85
+ $ data
86
+ );
87
+ $ total ++;
88
+ }
89
+
90
+ echo __ ("A total %1 product(s) synchronize salable quantity successfully. " , $ total ) . PHP_EOL ;
91
+
92
+ } catch (Exception $ e ) {
93
+ echo $ e ->getMessage ();
94
+ }
95
+
96
+ }
97
+
98
+ }
99
+
100
+ /** @var \Magento\Framework\App\Http $app */
101
+ $ app = $ bootstrap ->createApplication ('Outslide ' );
102
+ $ bootstrap ->run ($ app );
0 commit comments