0

I want to add country name in the address list of customers/me rest api.

I have tried adding my changes in core files and I got the desired result. But when I try to rewrite, it doesn't work.

Can anyone please check and help me with this?

etc/di.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Magento\Customer\Model\Data\Address" type="Company\Module\Model\Data\Customer\Address"/>    
</config>  

Address.php file:

<?php

namespace Company\Module\Model\Data\Customer;

use \Magento\Framework\Api\AttributeValueFactory;

class Address extends \Magento\Customer\Model\Data\Address implements \Company\Module\Api\Data\Customer\AddressInterface
{

    /**
     * Locale model
     *
     * @var \Magento\Framework\Locale\ListsInterface
     */
    protected $_localeLists;

    /**
     * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
     * @param AttributeValueFactory $attributeValueFactory
     * @param array $data
     */
    public function __construct(
            \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
            AttributeValueFactory $attributeValueFactory, 
            \Magento\Customer\Api\AddressMetadataInterface $metadataService,
            \Magento\Framework\Locale\ListsInterface $localeLists,
            $data = []
    )
    {
        $this->_localeLists = $localeLists;
        parent::__construct($extensionFactory, $attributeValueFactory, $metadataService, $data);
    }


    /**
     * Get Country name
     *
     * @return string|null
     */
    public function getCountryName()
    {
        $country_id = $this->getCountryId();
        $country_name = (string) $this->_localeLists->getCountryTranslation($country_id);
        return $country_name;
    }

}

AddressInterface file:

<?php 

namespace Company\Module\Api\Data\Customer;


/**
 * Customer address interface.
 * @api
 */

interface AddressInterface extends \Magento\Customer\Api\Data\AddressInterface
{

    /**
     * Country Name from Country Id
     *
     * @return string|null
     */

    public function getCountryName();       

}

when I use any existing function from parent model file and do changes in it, it works. So I think rewrite is working fine. But it doesn't work for the newly added function.

0

Follow magento Docs as they are defined good way to add new field in address form.

http://devdocs.magento.com/guides/v2.0/howdoi/checkout/checkout_new_field.html
| improve this answer | |

Your Answer

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

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