@@ -32,6 +32,8 @@ class Msrp
3232
3333 /**
3434 * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
35+ * @deprecated
36+ * @see $collectionFactory
3537 */
3638 protected $ productCollection ;
3739
@@ -40,6 +42,11 @@ class Msrp
4042 */
4143 protected $ configWriter ;
4244
45+ /**
46+ * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
47+ */
48+ private $ collectionFactory ;
49+
4350 /**
4451 * @param SampleDataContext $sampleDataContext
4552 * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
@@ -54,6 +61,28 @@ public function __construct(
5461 $ this ->csvReader = $ sampleDataContext ->getCsvReader ();
5562 $ this ->productCollection = $ productCollectionFactory ->create ()->addAttributeToSelect ('sku ' );
5663 $ 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 ;
5786 }
5887
5988 /**
@@ -68,29 +97,40 @@ public function install(array $fixtures)
6897 continue ;
6998 }
7099
100+ $ productsData = [];
71101 $ rows = $ this ->csvReader ->getData ($ fileName );
72102 $ header = array_shift ($ rows );
73-
74103 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+ }
78112 }
79- $ row = $ data ;
80- $ productId = $ this ->getProductIdBySku ($ row ['sku ' ]);
81- if (!$ productId ) {
82- continue ;
113+ if ($ sku ) {
114+ $ productsData [$ sku ] = $ rowData ;
83115 }
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 ();
91133 }
92- $ product ->setMsrp ($ price );
93- $ product ->save ();
94134 }
95135 }
96136 }
@@ -100,6 +140,8 @@ public function install(array $fixtures)
100140 *
101141 * @param string $sku
102142 * @return int|null
143+ * @deprecated
144+ * @see loadProducts()
103145 */
104146 protected function getProductIdBySku ($ sku )
105147 {
0 commit comments