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