1

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>
4
  • I see that you didn't set region and region_id? You can add more code lines about how you get the quote address object? Commented Aug 27, 2016 at 14:59
  • Edited the question to show how I get the address object.
    – TFS
    Commented Aug 27, 2016 at 16:23
  • Need to see your events.xml also. Commented Aug 27, 2016 at 16:32
  • added events.xml
    – TFS
    Commented Aug 27, 2016 at 17:20

2 Answers 2

3

Here $customerAddress = $observer->getCustomerAddress(); is an object of 'Magento\Customer\Model\Address' class and this class extend from Magento\Customer\Model\Address\AbstractAddress

So go to Magento\Customer\Model\Address\AbstractAddress has some get function that help you a lot. I add a line that help you to pick country.

$this->logger->info($customerAddress->getCountry());
<?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();
        // get country code
        $this->logger->info($customerAddress->getCountry());
        // get country name
        $this->logger->info($customerAddress->getCountryModel()->getName());
    }

}
0

For billing address then this answer worked for me
https://magento.stackexchange.com/a/159497/70258
Hope it help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.