How can I add the Company Name to the Billing Address information in a new order email? I would like to see the customer name, company name (which is a required field), and the rest of the address. But I'm not sure how to go about doing this?
Magento – How to add the Company Name to the Billing Address information in a new order email
billing-addressemail-templatesorders
Related Solutions
you need to create a plugin in your custom module ( the one that you used to create the custom attribute ), and have the code something like the following one:
namespace Package\Name\Plugin\Checkout;
class LayoutProcessor
{
public function afterProcess(
\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
array $jsLayout
) {
// Loop all payment methods (because billing address is appended to the payments)
$configuration = $jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'];
foreach ($configuration as $paymentGroup => $groupConfig) {
if (isset($groupConfig['component']) AND $groupConfig['component'] === 'Magento_Checkout/js/view/billing-address') {
$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
['payment']['children']['payments-list']['children'][$paymentGroup]['children']['form-fields']['children']['custom_attribute_code'] = [
'component' => 'Magento_Ui/js/form/element/abstract',
'config' => [
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/input',
'id' => 'custom_attribute_id',
],
'dataScope' => $groupConfig['dataScopePrefix'] . '.custom_attribute_id',
'label' => __('Custom attribute label'),
'provider' => 'checkoutProvider',
'visible' => true,
'validation' => [
'required-entry' => true,
'min_text_length' => 0,
],
'sortOrder' => 300,
'id' => 'custom_attribute_id'
];
}
}
return $jsLayout;
}
}
Hope this helps
There are few issue may be in your code,
getFirstName() should be getFirstname() as at db table field name is firstname.getFirstName indicated at the filed name is first_name and that is wrong
and getLastName() should be getLastname()
as at db table field name is lastname().getLastName indicated at the filed name is last_name and that is wrong
Also you check the order 700590221 exists at system*.
For first case, you can check availability by $order->getId()
give numeric value if order exits at system else give value false
Code
<?php
$order = Mage::getModel('sales/order')->loadByIncrementId('700590221');
if($order && $order->getId()) {
$billingAddress = $order->getBillingAddress();
echo 'First Name: ' . $billingAddress->getFirstname().PHP_EOL;
echo 'Last Name: ' . $billingAddress->getLastname();
}
?>
Best Answer
You will need to add the attribute as a field to the tables
sales_flat_quote_address
andsales_flat_order_address
. Then update you config.xml to copy the attributes when the address is converted from quote to order.Now once this is done the item should be on the address object that you have in the email so simply calling
$address->getYourAttribute()
should work here.