magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.5k stars 9.3k forks source link

[Bug] Customer model - getPrimaryAddresses without primary billing address #1096

Closed ihor-sviziev closed 9 years ago

ihor-sviziev commented 9 years ago

Once you have customer model with primary shipping, but wo primary billing and try to execute getPrimaryAddresses method - you got fatal error. It's happening because there no checking that you have primary billing. https://github.com/magento/magento2/blob/develop/app/code/Magento/Customer/Model/Customer.php#L722

This issue also exists in Magento CE 1.9.1.

How to fix it:

    /**
     * Retrieve all customer default addresses
     *
     * @return Address[]
     */
    public function getPrimaryAddresses()
    {
        $addresses = [];

        $primaryBilling = $this->getPrimaryBillingAddress();
        if ($primaryBilling) {
            $addresses[] = $primaryBilling;
            $primaryBilling->setIsPrimaryBilling(true);
        }

        $primaryShipping = $this->getPrimaryShippingAddress();
        if ($primaryShipping) {
            if ($primaryBilling && $primaryBilling->getId() == $primaryShipping->getId()) {
                $primaryBilling->setIsPrimaryShipping(true);
            } else {
                $primaryShipping->setIsPrimaryShipping(true);
                $addresses[] = $primaryShipping;
            }
        }

        return $addresses;
    }
vpelipenko commented 9 years ago

@ihor-sviziev, thank you posting this issue, but could you provide a way how we can get this fatal? Do you use native Magento workflows? Or is it a result of some customizations?

ihor-sviziev commented 9 years ago

@vpelipenko I got this issue in my own module, it contains cloning of customer model, which firing this method.

vpelipenko commented 9 years ago

@ihor-sviziev, how can we reproduce this issue? What should we do for that?

ihor-sviziev commented 9 years ago

@vpelipenko use code like this:

//Customer ID which has selected shipping address and NOT selected billing address
$customerId = '1'; 
$customer = $this->_objectManager->create('Magento\Customer\Model\Customer');
$customer->load($customerId);

//This part will cause fatal error
$primaryAddresses = $customer->getPrimaryAddresses();

//This part also will cause fatal error
$newCustomer = clone $customer;
vpelipenko commented 9 years ago

Ok. Thanks. We will try to check it.

vpelipenko commented 9 years ago

Issue is closed, because PR #1097 is created instead.