7
7
8
8
namespace Magento \Quote \Model \Cart ;
9
9
10
- use Magento \Catalog \Model \ ResourceModel \ Product \ CollectionFactory ;
10
+ use Magento \Catalog \Api \ ProductRepositoryInterface ;
11
11
use Magento \Framework \Exception \NoSuchEntityException ;
12
12
use Magento \Quote \Api \CartRepositoryInterface ;
13
13
use Magento \Quote \Api \Data \CartInterface ;
16
16
use Magento \Quote \Model \MaskedQuoteIdToQuoteIdInterface ;
17
17
use Magento \Quote \Model \Quote ;
18
18
use Magento \Framework \Message \MessageInterface ;
19
- use Magento \Store \Model \StoreManagerInterface ;
20
19
21
20
/**
22
21
* Unified approach to add products to the Shopping Cart.
@@ -48,14 +47,9 @@ class AddProductsToCart
48
47
];
49
48
50
49
/**
51
- * @var CollectionFactory
50
+ * @var ProductRepositoryInterface
52
51
*/
53
- private $ productCollectionFactory ;
54
-
55
- /**
56
- * @var StoreManagerInterface
57
- */
58
- private $ storeManager ;
52
+ private $ productRepository ;
59
53
60
54
/**
61
55
* @var array
@@ -78,21 +72,18 @@ class AddProductsToCart
78
72
private $ requestBuilder ;
79
73
80
74
/**
81
- * @param CollectionFactory $productCollectionFactory
82
- * @param StoreManagerInterface $storeManager
75
+ * @param ProductRepositoryInterface $productRepository
83
76
* @param CartRepositoryInterface $cartRepository
84
77
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
85
78
* @param BuyRequestBuilder $requestBuilder
86
79
*/
87
80
public function __construct (
88
- CollectionFactory $ productCollectionFactory ,
89
- StoreManagerInterface $ storeManager ,
81
+ ProductRepositoryInterface $ productRepository ,
90
82
CartRepositoryInterface $ cartRepository ,
91
83
MaskedQuoteIdToQuoteIdInterface $ maskedQuoteIdToQuoteId ,
92
84
BuyRequestBuilder $ requestBuilder
93
85
) {
94
- $ this ->productCollectionFactory = $ productCollectionFactory ;
95
- $ this ->storeManager = $ storeManager ;
86
+ $ this ->productRepository = $ productRepository ;
96
87
$ this ->cartRepository = $ cartRepository ;
97
88
$ this ->maskedQuoteIdToQuoteId = $ maskedQuoteIdToQuoteId ;
98
89
$ this ->requestBuilder = $ requestBuilder ;
@@ -149,13 +140,18 @@ private function addItemToCart(CartInterface $cart, Data\CartItem $cartItem, int
149
140
return ;
150
141
}
151
142
152
- $ store = $ this ->storeManager ->getStore ($ cart ->getStoreId ());
153
- $ productCollection = $ this ->productCollectionFactory ->create ()
154
- ->addAttributeToFilter ('sku ' , $ sku )
155
- ->addWebsiteFilter ([$ store ->getWebsiteId ()])
156
- ->load ();
157
- $ product = $ productCollection ->getFirstItem ();
158
- if (!$ product ->getId ()) {
143
+ try {
144
+ $ product = $ this ->productRepository ->get ($ sku , false , null , true );
145
+ } catch (NoSuchEntityException $ e ) {
146
+ $ this ->addError (
147
+ __ ('Could not find a product with SKU "%sku" ' , ['sku ' => $ sku ])->render (),
148
+ $ cartItemPosition
149
+ );
150
+
151
+ return ;
152
+ }
153
+
154
+ if (!in_array ($ cart ->getStoreId (), $ product ->getStoreIds ())) {
159
155
$ this ->addError (
160
156
__ ('Could not find a product with SKU "%sku" ' , ['sku ' => $ sku ])->render (),
161
157
$ cartItemPosition
0 commit comments