-
Notifications
You must be signed in to change notification settings - Fork 153
Create customer account functionality #254
Create customer account functionality #254
Conversation
…tomer and trying to retrieve data
@naydav, I haven't written tests yet. Will be covered a bit later. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Need to add a possibility to set
customer EAV attributes
(include validation rules)
Pay attention EAV attributes have own scope
$customerDataObject->setWebsiteId($store->getWebsiteId()); | ||
$customerDataObject->setStoreId($store->getId()); | ||
|
||
$password = array_key_exists('password', $args['input']) ? $args['input']['password'] : null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When do we not need a password?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try { | ||
$customer = $this->createUserAccount($args); | ||
$this->setUpUserContext($context, $customer); | ||
if (array_key_exists('is_subscribed', $args['input'])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pls, reuse \Magento\CustomerGraphQl\Model\Customer\ChangeSubscriptionStatus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
array $value = null, | ||
array $args = null | ||
) { | ||
if (!isset($args['input']) || !is_array($args['input']) || empty($args['input'])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to have more smarty validation (based on is_required
values of Customer EAV attributes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got it from the current \Magento\CustomerGraphQl\Model\Resolver\UpdateCustomer implementation. Is it ok for updating but not ok for creation?
Just question to think. We can have attribute requirement changed between account creation and account updating. So it also needs to be validated in customer update.
My implementation checks which fields are required
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Magento\CustomerGraphQl\Model\Resolver\UpdateCustomer
does not contain any validation related to settings in admin panel (there is only one rule need to provide password if you change email
)
} | ||
$data = $this->customerDataProvider->getCustomerById((int)$customer->getId()); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException(__($e->getMessage())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to highlight to client all of possible errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
return ['customer' => $data]; | ||
} | ||
|
||
private function createUserAccount($args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pls, add dockblcoks and type hinting to all of methods (including return values)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@naydav eav attributes are implemented |
…ed, gender input type changed
…ndling only validation exceptions
use Magento\Newsletter\Model\SubscriberFactory; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
class CreateCustomer implements ResolverInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to split this class (an example is in other resolvers of this module)
use Magento\Newsletter\Model\SubscriberFactory; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
class CreateCustomer implements ResolverInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to create dockblock
for all of classes
* @var CustomerDataProvider | ||
*/ | ||
private $customerDataProvider; | ||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed rmpty line
* @throws LocalizedException | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function resolve( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use inheritdoc
array $value = null, | ||
array $args = null | ||
) { | ||
if (!isset($args['input']) || !is_array($args['input']) || empty($args['input'])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Magento\CustomerGraphQl\Model\Resolver\UpdateCustomer
does not contain any validation related to settings in admin panel (there is only one rule need to provide password if you change email
)
@Stepa4man @naydav Thanks for the demo today! After going back to check on the use cases, it does not look like a guest shopper can create an account without a password on storefront. The Customer configuration options do now allow this either. The only way to create an account without a password is in Admin, which is a use case we do not support. So the password field should be required when creating an account through GraphQL. |
@misha-kotov Let me just give you some use case for example: |
After discussing this further, we decided to make the password field not required to allow for alternative account creation flows. So out of the box behavior of the endpoint would be:
|
@Stepa4man thank you for contributing. Please accept Community Contributors team invitation here to gain extended permissions for this repository. |
Hi @Stepa4man, thank you for your contribution! |
Description
Added the ability to create customer account through the GraphQL
Fixed Issues
Manual testing scenarios (*)
Run in GraphiQL:
mutation { createCustomer ( input: { firstname: "Test" lastname: "Dev" email: "[email protected]" is_subscribed:true } ) { customer{ id, firstname, lastname, email, is_subscribed } } }
Contribution checklist (*)