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

Sumit
  • 4,875
  • 2
  • 19
  • 35
Nafsss
  • 760
  • 13
  • 43

2 Answers2

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();
}

?>

Joao71
  • 1,197
  • 6
  • 16
  • How to get info in $order?? I mean it will show variable not define error – Nafsss Jul 07 '20 at 13:10
  • yeah my bad, give me 5 minutes ;) – Joao71 Jul 07 '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 07 '20 at 13:15
  • Yes this worked. Can you tell me is it possible to fetch the custom customer attribute here ?? – Nafsss Jul 07 '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 07 '20 at 13:20
  • Updated, let me know if you any more help ;) – Joao71 Jul 07 '20 at 13:29
  • @Joao71 Direct use of ObjectManager \Magento\Framework\App\ObjectManager::getInstance(); is not recommended by Magento. – Bhaumik Upadhyay Jul 07 '20 at 13:30
  • @Bhaumik1987 didn't know that, do you know why? Can you point me to the documentation about this issue so I can check it out please? – Joao71 Jul 07 '20 at 13:32
  • Please check it here https://devdocs.magento.com/guides/v2.4/extension-dev-guide/object-manager.html#overview And also check here https://magento.stackexchange.com/questions/75010/why-i-should-not-use-object-manager-directly-in-magento2/75204#75204 – Bhaumik Upadhyay Jul 07 '20 at 13:34
  • "Direct use of the create function prevents type validation and type hinting that a factory class provides."

    I don't see this as an issue in this particular case but I get it, thanks for the tip.

    – Joao71 Jul 07 '20 at 13:37
  • @Nafisa just updated the answer with the two possible approaches in order to get the customer object. it's my true belief that in this case the is no harm in using ObjectManager but I'll leave that decision to you. Le me know if you need any further assistance ;) – Joao71 Jul 07 '20 at 13:56
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!

Bhaumik Upadhyay
  • 893
  • 1
  • 9
  • 18