@@ -32,6 +32,8 @@ class Msrp
32
32
33
33
/**
34
34
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection
35
+ * @deprecated
36
+ * @see $collectionFactory
35
37
*/
36
38
protected $ productCollection ;
37
39
@@ -40,6 +42,11 @@ class Msrp
40
42
*/
41
43
protected $ configWriter ;
42
44
45
+ /**
46
+ * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
47
+ */
48
+ private $ collectionFactory ;
49
+
43
50
/**
44
51
* @param SampleDataContext $sampleDataContext
45
52
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
@@ -54,6 +61,28 @@ public function __construct(
54
61
$ this ->csvReader = $ sampleDataContext ->getCsvReader ();
55
62
$ this ->productCollection = $ productCollectionFactory ->create ()->addAttributeToSelect ('sku ' );
56
63
$ this ->configWriter = $ configWriter ;
64
+ $ this ->collectionFactory = $ productCollectionFactory ;
65
+ }
66
+
67
+ /**
68
+ * Load products with given SKUs.
69
+ *
70
+ * @param string[] $skus
71
+ * @return \Magento\Catalog\Model\Product[]
72
+ */
73
+ private function loadProducts (array $ skus ): array
74
+ {
75
+ /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
76
+ $ collection = $ this ->collectionFactory ->create ();
77
+ $ collection ->addAttributeToSelect ('sku ' );
78
+ $ collection ->addAttributeToFilter ('sku ' , ['in ' => $ skus ]);
79
+ $ products = [];
80
+ /** @var \Magento\Catalog\Model\Product $product */
81
+ foreach ($ collection as $ product ) {
82
+ $ products [$ product ->getSku ()] = $ product ;
83
+ }
84
+
85
+ return $ products ;
57
86
}
58
87
59
88
/**
@@ -68,29 +97,40 @@ public function install(array $fixtures)
68
97
continue ;
69
98
}
70
99
100
+ $ productsData = [];
71
101
$ rows = $ this ->csvReader ->getData ($ fileName );
72
102
$ header = array_shift ($ rows );
73
-
74
103
foreach ($ rows as $ row ) {
75
- $ data = [];
76
- foreach ($ row as $ key => $ value ) {
77
- $ data [$ header [$ key ]] = $ value ;
104
+ $ rowData = [];
105
+ $ sku = null ;
106
+ foreach ($ row as $ i => $ data ) {
107
+ if ($ header [$ i ] === 'sku ' ) {
108
+ $ sku = $ data ;
109
+ } else {
110
+ $ rowData [$ header [$ i ]] = $ data ;
111
+ }
78
112
}
79
- $ row = $ data ;
80
- $ productId = $ this ->getProductIdBySku ($ row ['sku ' ]);
81
- if (!$ productId ) {
82
- continue ;
113
+ if ($ sku ) {
114
+ $ productsData [$ sku ] = $ rowData ;
83
115
}
84
- /** @var \Magento\Catalog\Model\Product $product */
85
- $ product = $ this ->productCollection ->getItemById ($ productId );
86
- $ product ->setMsrpDisplayActualPriceType (Type::TYPE_ON_GESTURE );
87
- if (!empty ($ row ['msrp ' ])) {
88
- $ price = $ row ['msrp ' ];
89
- } else {
90
- $ price = $ product ->getPrice ()*1.1 ;
116
+ }
117
+ if ($ productsData ) {
118
+ $ products = $ this ->loadProducts (array_keys ($ productsData ));
119
+
120
+ foreach ($ productsData as $ sku => $ data ) {
121
+ if (!array_key_exists ($ sku , $ products )) {
122
+ throw new \RuntimeException ('Require product with SKU# ' . $ sku .' not found! ' );
123
+ }
124
+ $ product = $ products [$ sku ];
125
+ $ product ->setMsrpDisplayActualPriceType (Type::TYPE_ON_GESTURE );
126
+ if (!empty ($ data ['msrp ' ])) {
127
+ $ price = $ data ['msrp ' ];
128
+ } else {
129
+ $ price = $ product ->getPrice ()*1.1 ;
130
+ }
131
+ $ product ->setMsrp ($ price );
132
+ $ product ->save ();
91
133
}
92
- $ product ->setMsrp ($ price );
93
- $ product ->save ();
94
134
}
95
135
}
96
136
}
@@ -100,6 +140,8 @@ public function install(array $fixtures)
100
140
*
101
141
* @param string $sku
102
142
* @return int|null
143
+ * @deprecated
144
+ * @see loadProducts()
103
145
*/
104
146
protected function getProductIdBySku ($ sku )
105
147
{
0 commit comments