1

Hey I hope this question isn't to broad but how is the order attached to a customer that is created after guest checkout?

Background to my question:
I want to attach a custom field to my customer. This custom field is acquired in checkout and stored in the order. I tried to receive the order in a observer watching the customer_register_success event:

namespace Vendor\CheckoutCustomerInfo\Observer;


use Magento\Customer\Model\Customer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;

class AddInfoFromOrderToCustomerObserver implements ObserverInterface
{

    private $collectionFactoryInterface;

    public function __construct(CollectionFactoryInterface $collectionFactoryInterface)
    {
        $this->collectionFactoryInterface = $collectionFactoryInterface;
    }

    /**
     * @param Observer $observer
     *
     * @return void
     */
    public function execute(Observer $observer)
    {
        /**
         * @var Customer $customer
         */
        $customer = $observer->getCustomer();

        $orders = $this->collectionFactoryInterface->create($customer->getId())->addFieldToSelect(
            '*'
        )->setOrder(
            'created_at',
            'desc'
        );
    }
}

But in $orders I do not have the order.

Am I doing something wrong with querying the orders of the customer or is it attached somehow different? Is this event to early to get the order?

Thanks in advance!

  • Can you debug further into the button which allows to create a user on the success page? Or is your observer just fired before Magento assigns the order to the customer? – Alex Nov 2 '18 at 9:07
1

After a bit more debugging I figured out the orders are lazy loaded. I need to call $orders->getItems() or $orders->getFirstItem()in order to have them actually loaded.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.