I'm trying to get the country name from billing address. I have the address object and I see that the attribute country_id
is always No but I'm sure the country is existed as it is displayed properly in the My account page. Below I printed all the attributes of the address object in the debug log.
[2016-08-27 06:43:40] main.DEBUG: === Billing Address Attribute Start === {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: entity_id = 9 {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: entity_type_id = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: increment_id = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: parent_id = 9 {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: attribute_set_id = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: created_at = 2016-08-27 05:46:38 {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: updated_at = 2016-08-27 06:28:54 {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: prefix = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: firstname = Alex {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: middlename = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: lastname = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: suffix = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: company = ABC Company {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: street = 11 Hoi Shing Road
One Midtown {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: city = Tsuen Wan {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: country_id = No {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: region = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: region_id = 0 {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: postcode = 852 {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: telephone = 22233344 {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: fax = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: vat_id = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: vat_is_valid = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: vat_request_id = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: vat_request_date = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: vat_request_success = {"is_exception":false} []
[2016-08-27 06:43:40] main.DEBUG: === Billing Address Attribute End === {"is_exception":false} []
Here is how I get the address object
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
class CustomerAddressSaveAfterObserver implements ObserverInterface {
protected $logger;
public function __construct(\Psr\Log\LoggerInterface $loggerInterface //
) {
$this->logger = $loggerInterface;
}
public function execute(Observer $observer) {
$this->logger->debug("CustomerAddressSaveAfterObserver execute");
$customerAddress = $observer->getCustomerAddress();
$customer = $customerAddress->getCustomer();
$billingAddress = $customer->getDefaultBillingAddress();
$shippingAddress = $customer->getDefaultShippingAddress();
}
}
events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="customer_address_save_after">
<observer name="customer_address_save_after_observer" instance="Vendor\Module\Observer\CustomerAddressSaveAfterObserver" />
</event>
</config>
region
andregion_id
? You can add more code lines about how you get the quote address object?events.xml
also.