|
9 | 9 |
|
10 | 10 | use Magento\Customer\Api\Data\CustomerInterface;
|
11 | 11 | use Magento\Framework\Exception\LocalizedException;
|
12 |
| -use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
13 | 12 | use Magento\Framework\GraphQl\Config\Element\Field;
|
14 | 13 | use Magento\Framework\GraphQl\Query\ResolverInterface;
|
| 14 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
15 | 15 | use Magento\Newsletter\Model\SubscriberFactory;
|
16 | 16 |
|
17 | 17 | /**
|
@@ -49,9 +49,30 @@ public function resolve(
|
49 | 49 | /** @var CustomerInterface $customer */
|
50 | 50 | $customer = $value['model'];
|
51 | 51 | $customerId = (int)$customer->getId();
|
52 |
| - $websiteId = (int)$context->getExtensionAttributes()->getStore()->getWebsiteId(); |
53 |
| - $status = $this->subscriberFactory->create()->loadByCustomer($customerId, $websiteId)->isSubscribed(); |
54 | 52 |
|
55 |
| - return (bool)$status; |
| 53 | + $extensionAttributes = $context->getExtensionAttributes(); |
| 54 | + if (!$extensionAttributes) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + $store = $extensionAttributes->getStore(); |
| 59 | + if (!$store) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + return $this->getSubscriptionStatus($customerId, (int)$store->getWebsiteId()); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param int $customerId |
| 68 | + * @param int $websiteId |
| 69 | + * @return bool |
| 70 | + */ |
| 71 | + public function getSubscriptionStatus(int $customerId, int $websiteId): bool |
| 72 | + { |
| 73 | + $subscriberFactory = $this->subscriberFactory->create(); |
| 74 | + $subscriptionData = $subscriberFactory->loadByCustomer($customerId, $websiteId); |
| 75 | + |
| 76 | + return $subscriptionData->isSubscribed() ?? false; |
56 | 77 | }
|
57 | 78 | }
|
0 commit comments