Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$order->getCustomer() returns NULL for registered customer #25268

Closed
jakwinkler opened this issue Oct 24, 2019 · 25 comments
Closed

$order->getCustomer() returns NULL for registered customer #25268

jakwinkler opened this issue Oct 24, 2019 · 25 comments
Assignees
Labels
Area: Frontend Component: Customer Component: Sales Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Partner: i95Dev partners-contribution Pull Request is created by Magento Partner Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch

Comments

@jakwinkler
Copy link
Contributor

jakwinkler commented Oct 24, 2019

Preconditions (*)

  1. Magento 2.3.2 & 2.4-develop, community edition

Steps to reproduce (*)

  1. Create customer account and make an order
  2. Create a module with an observer to sales_order_save_after event
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_save_after">
        <observer name="my-observer" instance="Something\Darkside\Observer\OrderSaveAfter" />
    </event>
</config>
  1. Try to access Customer object from order object
public function execute(Observer $observer): void
    {
         /** @var Order $order */
         $order = $observer->getEvent()->getOrder();
         $customer = $order->getCustomer();
        ....

Expected result (*)

  1. Method getCustomer should return customer object, as defined in phpdoc block OR phpdoc block should be changed to highlight that NULL could be returned

Actual result (*)

  1. Method getCustomer always returns NULL, but it's not highlighted in phpdoc block that it could return null

Additional info

Method getCustomer will return customer only in case if someone added it to Order object, in other cases it doesn't exists. In case if you need to get customer from the Order object you need to do following get customer ID from the order object

$customerId = $order->getCustomerId();

// $customerId could be NULL in case if order was placed as guest
$customer = $customerId ? $this->customerRepository->getById($customerId) : null;

Where $this->customerRepository is instance of \Magento\Customer\Api\CustomerRepositoryInterface that retrieved via constructor

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Oct 24, 2019
@m2-assistant
Copy link

m2-assistant bot commented Oct 24, 2019

Hi @qsolutions-pl. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.3-develop instance - upcoming 2.3.x release

For more details, please, review the Magento Contributor Assistant documentation.

@qsolutions-pl do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • yes
  • no

@kanthy-magento kanthy-magento self-assigned this Oct 25, 2019
@m2-assistant
Copy link

m2-assistant bot commented Oct 25, 2019

Hi @itsPranith. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@jakwinkler
Copy link
Contributor Author

Any update on this?

@kanthy-magento kanthy-magento added Area: Frontend Component: Sales Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels Oct 29, 2019
@ghost ghost unassigned kanthy-magento Oct 29, 2019
@magento-engcom-team magento-engcom-team added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Oct 29, 2019
@magento-engcom-team
Copy link
Contributor

✅ Confirmed by @itsPranith
Thank you for verifying the issue. Based on the provided information internal tickets MC-22188 were created

Issue Available: @itsPranith, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@sudheer-gajjala sudheer-gajjala self-assigned this Oct 29, 2019
@m2-assistant
Copy link

m2-assistant bot commented Oct 29, 2019

Hi @sudheer-gajjala. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


@mohith227
Copy link
Contributor

@qsolutions-pl in this event we are not getting customer object so instead of $order->getCustomer() use like $order->getCustomerFirstname() for customer first name

@jakwinkler
Copy link
Contributor Author

honestly @mohith227 that is not an answer nor a solution, I need customer object to fetch other customer data like:

  • customer custom attributes
  • customer group

@rajeebkumar-sahoo rajeebkumar-sahoo added Partner: i95Dev partners-contribution Pull Request is created by Magento Partner labels Nov 22, 2019
@rajeebkumar-sahoo rajeebkumar-sahoo self-assigned this Nov 22, 2019
@m2-assistant
Copy link

m2-assistant bot commented Nov 22, 2019

Hi @rajeebkumar-sahoo. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


@ghost ghost unassigned sudheer-gajjala Nov 23, 2019
@rajeebkumar-sahoo rajeebkumar-sahoo mentioned this issue Nov 25, 2019
4 tasks
@ghost ghost unassigned rajeebkumar-sahoo Nov 25, 2019
@rajeebkumar-sahoo rajeebkumar-sahoo mentioned this issue Nov 25, 2019
4 tasks
@ihor-sviziev
Copy link
Contributor

ihor-sviziev commented Nov 26, 2019

Hi @qsolutions-pl @itsPranith @rajeebkumar-sahoo,

Unfortunately adding new method to existing functionality will break backward compatibility, more details: https://devdocs.magento.com/guides/v2.3/contributor-guide/backward-compatible-development/

Why do you think order model should have method "getCustomer"?

As for me it shoudn't as it's not his responsibility. It should just contain getCustomerId method or something like this, and it is:

public function getCustomerId()
{
return $this->getData(OrderInterface::CUSTOMER_ID);
}

If you need to have customer - you can get it from customer repository.

I would mark it as not an issue

@ihor-sviziev ihor-sviziev removed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Nov 26, 2019
@rajeebkumar-sahoo
Copy link

Hi @ihor-sviziev,

The call to "$order->getCustomer()" as mentioned in the issue does not raise any PHP error saying that "method does not exist on object" but it returns NULL. So the method already exists and the functionality provided on "Order" model class as a magic method like
"@method \Magento\Customer\Model\Customer getCustomer()". And the magic method gets resolved in "\Magento\Framework\DataObject::__call" where it doesn't find any key named "customer" and hence returns NULL which violets the method signature defined like "@method \Magento\Customer\Model\Customer getCustomer()".

The solution to this could be either we provide logic on "__call" by overriding it in "\Magento\Sales\Model\Order" or the way I did it by providing a direct function. I choose the second method because overriding "__call" would be only possible by introducing static text comparison like "if ($method === 'getCustomer')", which is not appropriate.

@ihor-sviziev
Copy link
Contributor

ihor-sviziev commented Nov 28, 2019 via email

@rajeebkumar-sahoo
Copy link

HI @ihor-sviziev @qsolutions-pl @itsPranith,

Yes, that sounds reasonable and also we should mark the magic method as deprecated so that with the next minor release we could remove that altogether.

like:
* @method \Magento\Customer\Model\Customer|null getCustomer() { @deprecated instead use getCustomerId() along with customer repository to get customer instance }

@ihor-sviziev
Copy link
Contributor

Replacing to something like will be good enough: * @method \Magento\Customer\Model\Customer|null getCustomer()
So people will see that it could be also null and will check it additionally. Also as it's not real method - we can't deprecate it.

@rajeebkumar-sahoo
Copy link

Actually we can see "phpDocumentor/phpDocumentor#1604" if we could or will go with the above solution.

@ihor-sviziev
Copy link
Contributor

Actually we can see "phpDocumentor/phpDocumentor#1604" if we could or will go with the above solution.

It wasn't fixed and AFAIK major IDEs are not supporting this syntax.

@ghost ghost unassigned rajeebkumar-sahoo Dec 16, 2019
@engcom-Hotel engcom-Hotel self-assigned this Jan 13, 2020
@m2-assistant
Copy link

m2-assistant bot commented Jan 13, 2020

Hi @engcom-Hotel. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-Hotel engcom-Hotel added Component: Customer Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch and removed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development labels Jan 13, 2020
@ghost ghost unassigned engcom-Hotel Jan 13, 2020
@magento-engcom-team
Copy link
Contributor

✅ Confirmed by @engcom-Hotel
Thank you for verifying the issue. Based on the provided information internal tickets MC-25044 were created

Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@magento-engcom-team magento-engcom-team added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Jan 13, 2020
@sertlab sertlab self-assigned this Jan 16, 2020
@m2-assistant
Copy link

m2-assistant bot commented Jan 16, 2020

Hi @sertlab. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 2. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


@ihor-sviziev
Copy link
Contributor

@engcom-Hotel i think we expected result should be changed, that magic method should highlight that it could return null. This is not direct responsibility of order object to retrieve customer. In client code customer could be retrieved by customerId.
Btw both methods getCustomer and getCustomerId might return null in case when order were placed as guest, and that would be absolutely valid behavior

@engcom-Hotel
Copy link
Contributor

@ihor-sviziev thanks for contributing! I have updated the description.

@magento-deployment-service
Copy link

Thanks for opening this issue!

3 similar comments
@magento-deployment-service
Copy link

Thanks for opening this issue!

@magento-deployment-service
Copy link

Thanks for opening this issue!

@magento-deployment-service
Copy link

Thanks for opening this issue!

@slavvka
Copy link
Member

slavvka commented Feb 9, 2020

Hi @qsolutions-pl. Thank you for your report.
The issue has been fixed in #26423 by @sertlab in 2.4-develop branch
Related commit(s):

The fix will be available with the upcoming 2.4.0 release.

@slavvka slavvka added the Fixed in 2.4.x The issue has been fixed in 2.4-develop branch label Feb 9, 2020
@slavvka slavvka closed this as completed Feb 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Frontend Component: Customer Component: Sales Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Partner: i95Dev partners-contribution Pull Request is created by Magento Partner Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch
Projects
None yet
Development

No branches or pull requests

10 participants