0

Can any one tell me how can I get the customer id whose order is being created by admin in admin panel, in file /vendor/magento/module-sales/view/adminhtml/templates/order/create/form/account.phtml

1
$block->getCustomerId()

This should do it, let me know if you need any further assistance !

UPDATE:

In order to get the customer and retrive atributes you can do the following:

SOLUTION USING OBJECT MANAGER

$customerId = $block->getCustomerId();
if(isset($customerId))
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();    
    $customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);
    //here get the attributtes you need from $customer, for example
    $firstname = $customer->getData('firstname');
}

SOLUTION USING CUSTOMER REPOSITORY:

In your di.xml (Vendor\Module\etc\di.xml)

<preference for="Magento\Sales\Block\Adminhtml\Order\Create\Form\Account" type="Vendor\Module\Block\Adminhtml\Order\Create\Account"/> 

Create the following file in (Vendor\Module\Block\Adminhtml\Order\Create)

<?php
namespace Vendor\Module\Block\Adminhtml\Order\Create;

class Account extends \Magento\Sales\Block\Adminhtml\Order\Create\Form\Account
{
    public function getCustomer( $customerId)
    {
        try {
            return $this->customerRepository->getById($customerId);
        } catch (\Exception $e) {
            /** If customer does not exist do nothing. */
        }
    }
}
?>

In your account.phtml:

$customerId = $block->getCustomerId();
if(isset($customerId))
{
    $customer = $block->getCustomer( $customerId);
    $firstname = $customer->getFirstName();
}

?>
11
  • How to get info in $order?? I mean it will show variable not define error
    – Nafsss
    Jul 7 '20 at 13:10
  • yeah my bad, give me 5 minutes ;)
    – Joao71
    Jul 7 '20 at 13:11
  • Just updated the answer, it's $block->getCustomerId(). let me know it it's solved and don't forget to upvote/mark as answered :)
    – Joao71
    Jul 7 '20 at 13:15
  • Yes this worked. Can you tell me is it possible to fetch the custom customer attribute here ??
    – Nafsss
    Jul 7 '20 at 13:18
  • yes, you jst need to make an instance and call the object manager, give me 5 minutes and I'll update my answer with that ;)
    – Joao71
    Jul 7 '20 at 13:20
0

You can get Customer ID in /vendor/magento/module-sales/view/adminhtml/templates/order/create/form/account.phtml using the below code:

<p>Customer Id:  <?= $this->getCustomerId() ?> </p>
<p>Customer Email: <?= $this->getQuote()->getCustomerEmail() ?></p>

Check the block $block \Magento\Sales\Block\Adminhtml\Order\Create\Form\Account as it gets in the same way.

Hope this helps!

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.