1

I have successfully created custom customer attributes in Magento 2.3.2 (like doctor name and doctor zipcode) and I can see/modify them in my adminhtml customer view. What I would like is to have them inserted into customer orders and order e-mails. Has anyone already done that ? Thank you for your pointers/hints/solutions ! Edit :

I tried using observers : 1 for customer signup, 1 on successful order. So I added 2 modules

One called CustomerRegister : registration.php :

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'CustomerRegister_Emails',
    __DIR__
);

etc/events.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <event name="customer_register_success">
<observer name="customerregister" instance="CustomerRegister\Emails\Observer\AddCustomerData">
    </event>
</config>

etc/module.xml:

?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="CustomerRegister" setup_version="1.0.1">
<sequence>
<module name="Magento_CustomerRegister"/>
</sequence>
    </module>
</config>

Observer/AddCustomerData.php :

<?php

namespace Anshu\Emails\Observer;

use Magento\Framework\Event\ObserverInterface;

class AddCustomerData implements ObserverInterface
{
 public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
  ){
    $this->_customerRepositoryInterface = $customerRepositoryInterface;
  }

  public function execute(\Magento\Framework\Event\Observer $observer)
  {
    $transport = $observer->getEvent()->getTransport();
    $customer = $this->_customerRepositoryInterface->getById($transport->getOrder()->GetCustomerId());

$transport->addData([
'nommedecin' => $customer->getCustomAttribute('nommedecin')->getValue()

]);
}
}

If I insert the {{ var nommedecin }} in my new customer email template, it's empty... If you have any clues, please do not hesitate

Your Answer

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

Browse other questions tagged or ask your own question.