0

When I create a new order for new customer and submit the order it shows the exception

The value of attribute "Account No" must be set

enter image description here

account_no is the required attribute for customer table that means It needs to be filled so I added the custom field in the customer account Information section on order create page so that the user can give the account_no enter image description here

Now I'm stuck on how to pass the new customer account number so that it creates the customer with account_no and save the order . I tried using the events

sales_order_place_before sales_order_save_before adminhtml_sales_order_create_process_item_before

<event name="adminhtml_customer_prepare_save">
    <observer name="account_num_customer_save_before" instance="Vendor\Module\Observer\CustomerSaveBefore" />
</event>




<?php

    namespace Vendor\Module\Observer;

    use Magento\Framework\Event\Observer;

    class CustomerSaveBefore implements \Magento\Framework\Event\ObserverInterface
    {

        protected $_helper;


        public function __construct(
            \Vendor\Module\Helper\Data $helper,
            \Magento\Framework\App\Request\Http $request,
            \Magento\Customer\Model\CustomerFactory $customerFactory,
            \Magento\Store\Model\StoreManagerInterface $storeManager
        ) {

            $this->_helper= $helper;
            $this->request = $request;
            $this->storeManager = $storeManager;
            $this->customerFactory = $customerFactory;
        }

        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            /**
             * @var $customer \Magento\Customer\Model\Customer
             */

            $quote = $observer->getQuote();
            $customer = $observer->getCustomer();
            echo "<pre>";
        }

    }
0

I added di.xml with

<preference for="Magento\Sales\Model\AdminOrder\Create" type="Vendor\Module\Rewrite\Magento\Sales\Model\AdminOrder\Create"/>

Create.php

<?php
 namespace Vendor\Module\Rewrite\Magento\Sales\Model\AdminOrder;
 class Create extends \Magento\Sales\Model\AdminOrder\Create
{
     /**
     * Prepare customer data for order creation.
     *
     * Create customer if not created using data from customer form.
     * Create customer billing/shipping address if necessary using data from customer address forms.
     * Set customer data to quote.
     *
     * @return \Magento\Sales\Model\AdminOrder\Create
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     */
    public function _prepareCustomer()
    {
     //   $this->_logger->critical('accunt no: '.$this->getData('account/account_no'));
     //   $this->_logger->critical('here now gg....');
        if ($this->getQuote()->getCustomerIsGuest()) {
            return $this;
        }
        /** @var $store \Magento\Store\Model\Store */
        $store = $this->getSession()->getStore();
        $customer = $this->getQuote()->getCustomer();
         if ($customer->getId() && !$this->_customerIsInStore($store)) {
            /** Create a new customer record if it is not available in the specified store */
            /** Unset customer ID to ensure that new customer will be created */
            $customer->setId(null)
                ->setStoreId($store->getId())
                ->setWebsiteId($store->getWebsiteId())
                ->setCreatedAt(null);
            $customer = $this->_validateCustomerData($customer);
        } else if (!$customer->getId()) {
            /** Create new customer */
            $customerBillingAddressDataObject = $this->getBillingAddress()->exportCustomerAddress();
            $customer->setSuffix($customerBillingAddressDataObject->getSuffix())
                ->setFirstname($customerBillingAddressDataObject->getFirstname())
                ->setLastname($customerBillingAddressDataObject->getLastname())
                ->setMiddlename($customerBillingAddressDataObject->getMiddlename())
                ->setPrefix($customerBillingAddressDataObject->getPrefix())
                ->setStoreId($store->getId())
                ->setWebsiteId($store->getWebsiteId())
                ->setEmail($this->_getNewCustomerEmail())
                ->setCustomAttribute('account_no', $this->_getNewCustomerAccountNo());
            $customer = $this->_validateCustomerData($customer);
        }
        $this->getQuote()->setCustomer($customer);
         if ($this->getBillingAddress()->getSaveInAddressBook()) {
            $this->_prepareCustomerAddress($this->getQuote()->getCustomer(), $this->getBillingAddress());
            $address = $this->getBillingAddress()->setCustomerId($this->getQuote()->getCustomer()->getId());
            $this->setBillingAddress($address);
        }
        if (!$this->getQuote()->isVirtual() && $this->getShippingAddress()->getSaveInAddressBook()) {
            $this->_prepareCustomerAddress($this->getQuote()->getCustomer(), $this->getShippingAddress());
            $address = $this->getShippingAddress()->setCustomerId($this->getQuote()->getCustomer()->getId());
            $this->setShippingAddress($address);
        }
        $this->getBillingAddress()->setCustomerId($customer->getId());
        $this->getQuote()->updateCustomerData($this->getQuote()->getCustomer());
         $customer = $this->getQuote()->getCustomer();
        $origAddresses = $customer->getAddresses(); // save original addresses
        $customer->setAddresses([]);
        $customerData = $this->customerMapper->toFlatArray($customer);
        $customer->setAddresses($origAddresses); // restore original addresses
        foreach ($this->_createCustomerForm($customer)->getUserAttributes() as $attribute) {
            if (isset($customerData[$attribute->getAttributeCode()])) {
                $quoteCode = sprintf('customer_%s', $attribute->getAttributeCode());
                $this->getQuote()->setData($quoteCode, $customerData[$attribute->getAttributeCode()]);
            }
        }
         return $this;
    }
     /**
     * Retrieve new customer account number.
     *
     * @return string
     */
    protected function _getNewCustomerAccountNo()
    {
        return $this->getData('account/account_no');
    }
 }

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.