44
55use JustBetter \CustomerPricing \Api \CustomerPricingManagementInterface ;
66use JustBetter \CustomerPricing \Model \CustomerPricingFactory as ModelFactory ;
7- use JustBetter \CustomerPricing \Model \ResourceModel \CustomerPricingFactory as ResourceModelFactory ;
87use JustBetter \CustomerPricing \Model \ResourceModel \CustomerPricing \Collection ;
8+ use JustBetter \CustomerPricing \Model \ResourceModel \CustomerPricingFactory as ResourceModelFactory ;
99use Magento \Catalog \Api \ProductRepositoryInterface ;
1010use Magento \Catalog \Model \Product ;
11+ use Magento \Customer \Model \ResourceModel \Customer \CollectionFactory as CustomerCollectionFactory ;
1112
1213class CustomerPricingManagement implements CustomerPricingManagementInterface
1314{
1415 public function __construct (
1516 protected ProductRepositoryInterface $ productRepository ,
1617 protected ModelFactory $ customerFactory ,
1718 protected ResourceModelFactory $ customerPricingFactory ,
18- protected Collection $ customerPricingCollection
19- ) {
20- }
19+ protected Collection $ customerPricingCollection,
20+ protected CustomerCollectionFactory $ customerCollectionFactory
21+ ) { }
2122
2223 /**
23- * @inheritDoc
24+ * { @inheritDoc}
2425 */
2526 public function saveCustomerPrices (string $ sku , mixed $ customerPrices ): bool
2627 {
28+ $ customerIds = array_unique (array_column ($ customerPrices , 'customer_id ' ));
29+
30+ $ customerCollection = $ this ->customerCollectionFactory ->create ()
31+ ->addFieldToFilter ('entity_id ' , ['in ' => $ customerIds ])
32+ ->addFieldToSelect ('entity_id ' );
33+
34+ $ validCustomerIds = array_map (
35+ static fn ($ customer ) => (int ) $ customer ->getId (),
36+ $ customerCollection ->getItems ()
37+ );
38+
2739 /** @var Product $product */
2840 $ product = $ this ->productRepository ->get ($ sku );
2941
@@ -43,6 +55,10 @@ public function saveCustomerPrices(string $sku, mixed $customerPrices): bool
4355 $ quantity = $ customerPriceData ['quantity ' ];
4456 $ price = $ customerPriceData ['price ' ];
4557
58+ if (! in_array ($ customerId , $ validCustomerIds )) {
59+ continue ;
60+ }
61+
4662 $ updated = false ;
4763
4864 /** @var CustomerPricing $currentCustomerPrice */
0 commit comments