Skip to content

Commit 97a0995

Browse files
authored
Merge pull request #11 from justbetter/feature/check-customers
2 parents 57efc27 + 45cfb33 commit 97a0995

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"require": {
2020
"ext-json": "*",
2121
"magento/module-catalog": "*",
22+
"magento/module-customer": "*",
2223
"magento/module-eav": "*"
2324
},
2425
"autoload": {

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ parameters:
66
- src
77
level: 1
88
ignoreErrors:
9+
- '#Magento\\Customer\\Model\\ResourceModel\\Customer\\CollectionFactory#'
910
- '#JustBetter\\CustomerPricing\\Model\\ResourceModel\\CustomerPricing\\CollectionFactory#'
1011
- '#JustBetter\\CustomerPricing\\Model\\ResourceModel\\CustomerPricingFactory#'
1112
- '#JustBetter\\CustomerPricing\\Model\\CustomerPricingFactory#'

src/Model/CustomerPricingManagement.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,38 @@
44

55
use JustBetter\CustomerPricing\Api\CustomerPricingManagementInterface;
66
use JustBetter\CustomerPricing\Model\CustomerPricingFactory as ModelFactory;
7-
use JustBetter\CustomerPricing\Model\ResourceModel\CustomerPricingFactory as ResourceModelFactory;
87
use JustBetter\CustomerPricing\Model\ResourceModel\CustomerPricing\Collection;
8+
use JustBetter\CustomerPricing\Model\ResourceModel\CustomerPricingFactory as ResourceModelFactory;
99
use Magento\Catalog\Api\ProductRepositoryInterface;
1010
use Magento\Catalog\Model\Product;
11+
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
1112

1213
class 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

Comments
 (0)