Skip to content

Commit d48ddc2

Browse files
committed
AC-2797:GraphQL Customer Newsletter Subscriptions are inconsistent in multi website setups
1 parent 263fbc9 commit d48ddc2

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

app/code/Magento/CustomerGraphQl/Model/Resolver/IsSubscribed.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
use Magento\Customer\Api\Data\CustomerInterface;
1111
use Magento\Framework\Exception\LocalizedException;
12-
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1312
use Magento\Framework\GraphQl\Config\Element\Field;
1413
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1515
use Magento\Newsletter\Model\SubscriberFactory;
1616

1717
/**
@@ -49,9 +49,30 @@ public function resolve(
4949
/** @var CustomerInterface $customer */
5050
$customer = $value['model'];
5151
$customerId = (int)$customer->getId();
52-
$websiteId = (int)$context->getExtensionAttributes()->getStore()->getWebsiteId();
53-
$status = $this->subscriberFactory->create()->loadByCustomer($customerId, $websiteId)->isSubscribed();
5452

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;
5677
}
5778
}

0 commit comments

Comments
 (0)