@@ -381,10 +381,13 @@ protected function createCustomerCart($customerId, $storeId)
381381
382382 /**
383383 * @inheritdoc
384+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
384385 */
385386 public function placeOrder ($ cartId , PaymentInterface $ paymentMethod = null )
386387 {
387388 $ quote = $ this ->quoteRepository ->getActive ($ cartId );
389+ $ customer = $ quote ->getCustomer ();
390+
388391 if ($ paymentMethod ) {
389392 $ paymentMethod ->setChecks (
390393 [
@@ -403,18 +406,22 @@ public function placeOrder($cartId, PaymentInterface $paymentMethod = null)
403406 $ quote ->collectTotals ();
404407 }
405408
406- if ($ quote ->getCheckoutMethod () === self ::METHOD_GUEST || !$ quote -> getCustomer () ) {
409+ if ($ quote ->getCheckoutMethod () === self ::METHOD_GUEST || !$ customer ) {
407410 $ quote ->setCustomerId (null );
408- $ quote ->setCustomerEmail ($ quote ->getBillingAddress ()->getEmail ());
409- if ($ quote ->getCustomerFirstname () === null && $ quote ->getCustomerLastname () === null ) {
410- $ quote ->setCustomerFirstname ($ quote ->getBillingAddress ()->getFirstname ());
411- $ quote ->setCustomerLastname ($ quote ->getBillingAddress ()->getLastname ());
412- if ($ quote ->getBillingAddress ()->getMiddlename () === null ) {
413- $ quote ->setCustomerMiddlename ($ quote ->getBillingAddress ()->getMiddlename ());
411+ $ billingAddress = $ quote ->getBillingAddress ();
412+ $ quote ->setCustomerEmail ($ billingAddress ? $ billingAddress ->getEmail () : null );
413+ if ($ quote ->getCustomerFirstname () === null
414+ && $ quote ->getCustomerLastname () === null
415+ && $ billingAddress
416+ ) {
417+ $ quote ->setCustomerFirstname ($ billingAddress ->getFirstname ());
418+ $ quote ->setCustomerLastname ($ billingAddress ->getLastname ());
419+ if ($ billingAddress ->getMiddlename () === null ) {
420+ $ quote ->setCustomerMiddlename ($ billingAddress ->getMiddlename ());
414421 }
415422 }
416423 $ quote ->setCustomerIsGuest (true );
417- $ groupId = $ quote -> getCustomer ()-> getGroupId () ? : GroupInterface::NOT_LOGGED_IN_ID ;
424+ $ groupId = $ customer ? $ customer -> getGroupId () : GroupInterface::NOT_LOGGED_IN_ID ;
418425 $ quote ->setCustomerGroupId ($ groupId );
419426 }
420427
0 commit comments