8
8
namespace Magento \QuoteGraphQl \Model \Cart ;
9
9
10
10
use Exception ;
11
- use Magento \Catalog \Api \ProductRepositoryInterface ;
11
+ use Magento \Catalog \Api \Data \ProductInterface ;
12
+ use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory as ProductCollectionFactory ;
12
13
use Magento \Framework \Exception \NoSuchEntityException ;
13
14
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
14
15
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
21
22
class AddSimpleProductToCart
22
23
{
23
24
/**
24
- * @var ProductRepositoryInterface
25
+ * @var ProductCollectionFactory
25
26
*/
26
- private $ productRepository ;
27
+ private $ productCollectionFactory ;
27
28
28
29
/**
29
30
* @var BuyRequestBuilder
30
31
*/
31
32
private $ buyRequestBuilder ;
32
33
33
34
/**
34
- * @param ProductRepositoryInterface $productRepository
35
+ * @param ProductCollectionFactory $productCollectionFactory
35
36
* @param BuyRequestBuilder $buyRequestBuilder
36
37
*/
37
38
public function __construct (
38
- ProductRepositoryInterface $ productRepository ,
39
- BuyRequestBuilder $ buyRequestBuilder
39
+ ProductCollectionFactory $ productCollectionFactory ,
40
+ BuyRequestBuilder $ buyRequestBuilder
40
41
) {
41
- $ this ->productRepository = $ productRepository ;
42
+ $ this ->productCollectionFactory = $ productCollectionFactory ;
42
43
$ this ->buyRequestBuilder = $ buyRequestBuilder ;
43
44
}
44
45
@@ -55,12 +56,13 @@ public function execute(Quote $cart, array $cartItemData): void
55
56
$ cartItemData ['model ' ] = $ cart ;
56
57
$ sku = $ this ->extractSku ($ cartItemData );
57
58
58
- try {
59
- $ product = $ this ->productRepository ->get ($ sku , false , null , true );
60
- } catch (NoSuchEntityException $ e ) {
61
- throw new GraphQlNoSuchEntityException (__ ('Could not find a product with SKU "%sku" ' , ['sku ' => $ sku ]));
62
- }
63
- if (!in_array ($ cart ->getStoreId (), $ product ->getStoreIds ())) {
59
+ $ productCollection = $ this ->productCollectionFactory ->create ()
60
+ ->addAttributeToFilter (ProductInterface::SKU , $ sku )
61
+ ->addWebsiteFilter ([$ cart ->getStore ()->getWebsiteId ()])
62
+ ->load ();
63
+ /** @var ProductInterface $product */
64
+ $ product = $ productCollection ->getFirstItem ();
65
+ if (!$ product ->getId ()) {
64
66
throw new GraphQlNoSuchEntityException (__ ('Could not find a product with SKU "%sku" ' , ['sku ' => $ sku ]));
65
67
}
66
68
0 commit comments