I'm trying to add an address column to my orders index but I'm having trouble correctly getting the actual address.
From my code below, when I dump on $orderShippingAddress
I get the ID of a valid address. But when I dump on $address
I get null
.
Can anyone spot where I'm going wrong? For reference, I was working from here: https://craftcms.com/docs/commerce/3.x/addresses.html#get-current-customer-address-by-id
Event::on(
Order::class,
Order::EVENT_REGISTER_TABLE_ATTRIBUTES,
function (RegisterElementTableAttributesEvent $event) {
$event->tableAttributes['address'] = ['label' => 'Address'];
}
);
Event::on(
Order::class,
Order::EVENT_SET_TABLE_ATTRIBUTE_HTML,
function (SetElementTableAttributeHtmlEvent $event) {
$order = $event->sender;
$orderShippingAddress = $order->shippingAddressId;
$address = craft\commerce\Plugin::getInstance()->getCustomers()->getCustomer()->getAddressById($order->shippingAddressId);
dump($shippingAddress);
if ($event->attribute === 'address') {
$event->html = $address;
}
}
);
addresses
service?$address = craft\commerce\Plugin::getInstance()->getAddresses()->getAddressById($order->shippingAddressId);