2

I need to fetch the stores addresses using REST API.

We already have storeConfigs but this doesn't return the addresses.

GET  /V1/store/storeConfigs

enter image description here

How can we get the address mentioned in the Store Information tab. Also the Address is Website scoped but I need Store View based.

Thanks in advance.

1 Answer 1

1

For your requirement then you need to build a Rest API yourself.

API points like:

webapi.xml Code

 <?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route method="POST" url="/V1/devamitbera-systemconfig/systemconfig/">
        <service class="Devamitbera\Systemconfig\Api\SystemconfigManagementInterface" method="getSystemconfig"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
    <!-- getting all store detail -->
    <route method="GET" url="/V1/devamitbera-systemconfig/systemconfiglist">
        <service class="Devamitbera\Systemconfig\Api\SystemconfigListManagementInterface" method="getSystemconfig"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Interface Class for getting Single store view.

Devamitbera\Systemconfig\Api\SystemconfigManagementInterface

And code

    <?php

    namespace Devamitbera\Systemconfig\Api;

    interface SystemconfigManagementInterface
    {

        /**
         * GET for getSystemconfig api
         * @param string $storeCode
         * @param string $path
         * @return string[]
         */
        public function getSystemconfig($storeCode =null );
    }

Interface Class for getting all store view:

Devamitbera\Systemconfig\Api\SystemconfigListManagementInterface

And code

<?php
namespace Devamitbera\Systemconfig\Api;

interface SystemconfigListManagementInterface
{

    /**
     * GET for getSystemconfig api
     * @return string[]
     */
    public function getSystemconfig();
}

Model Class for the interface class SystemconfigManagement:

Devamitbera\Systemconfig\Model\SystemconfigManagement

Code:

<?php

namespace Devamitbera\Systemconfig\Model;

class SystemconfigManagement implements \Devamitbera\Systemconfig\Api\SystemconfigManagementInterface
{


/**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
private $storeManager;

/**
 * @var \Magento\Framework\App\Config\ScopeConfigInterface
 */
private $scopeConfig;

protected $configPaths = [
        'general/store_information/name',
        'general/store_information/phone',
        'general/store_information/hours',
        'general/store_information/country_id',
        'general/store_information/region_id',
        'general/store_information/postcode',
        'general/store_information/city',
        'general/store_information/street_line1',   
        'general/store_information/street_line2',
        'general/store_information/merchant_vat_number'                 
];

public function __construct(
 \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
 \Magento\Store\Model\StoreManagerInterface $storeManager 
 )
 {
     $this->scopeConfig = $scopeConfig;
     $this->storeManager = $storeManager;
}
/**
 * {@inheritdoc}
 */
public function getSystemconfig($storeCode = null)
{

   if($storeCode === null){
       $storeCode = $this->storeManager->getStore()->getCode();
   }
   $storeConfigFinaData = [];

   foreach ($this->configPaths as $configPath) {
       $storeConfigFinaData[][$configPath] = $this->scopeConfig
               ->getValue(
                       $configPath,
                       \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
                       $storeCode
                       );
   }

   return $storeConfigFinaData;
}

}

Model Class for the interface class SystemconfigListManagementInterface:

Devamitbera\Systemconfig\Model\SystemconfigListManagement

Code:

<?php

namespace Devamitbera\Systemconfig\Model;

class SystemconfigListManagement implements \Devamitbera\Systemconfig\Api\SystemconfigListManagementInterface 
{

    /**
     * @var \Magento\Store\Model\ResourceModel\Store\CollectionFactory
     */
    private $storeCollectionFactory;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    private $storeManager;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    private $scopeConfig;
    protected $configPaths = [
        'general/store_information/name',
        'general/store_information/phone',
        'general/store_information/hours',
        'general/store_information/country_id',
        'general/store_information/region_id',
        'general/store_information/postcode',
        'general/store_information/city',
        'general/store_information/street_line1',
        'general/store_information/street_line2',
        'general/store_information/merchant_vat_number'
    ];

    public function __construct(
            \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeCollectionFactory,
            \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
            \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $this->storeCollectionFactory = $storeCollectionFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function getSystemconfig() {


        $storeConfigs = [];
        $storeCollection = $this->storeCollectionFactory->create();
        foreach ($storeCollection->load() as $item) {
            $storeConfigs[] = $this->StoreInfomation($item);
        }

        return $storeConfigs;
    }

    private function StoreInfomation($store) {
        $storeConfigFinaData = [];
        $storeConfigFinaData['store_id'] = $store->getId();
        $storeConfigFinaData['store_code'] = $store->getCode(); 
        $storeConfigFinaData['website_id'] = $store->getWebsiteId(); 
        foreach ($this->configPaths as $configPath) {
            $storeConfigFinaData[$configPath] = $this->scopeConfig
                    ->getValue(
                    $configPath,
                    \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
                    $store->getCode()
            );

        }

        return $storeConfigFinaData;
    }

}

di.xml

TEST SAMPLE:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://127.0.0.1/magento23dev/rest/V1/devamitbera-systemconfig/systemconfig/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"storeCode\": \"default\"}",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer gv5dgldyscnzpa8sveg1xfct9frnwy0q",
    "Content-Type: application/json",
    "Postman-Token: ff714339-e830-4347-8266-e458c58102d9",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

This method support one parameter:

1.$storeCode : Store code

All store Information API Test Sample:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://127.0.0.1/magento23dev/rest/V1/devamitbera-systemconfig/systemconfiglist",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer gv5dgldyscnzpa8sveg1xfct9frnwy0q",
    "Content-Type: application/json",
    "Postman-Token: d9b495e5-3bc7-4e96-a723-3931b96b70ba",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
6
  • Thanks Amit. What is $path here : $storeConfigFinaData[][$path] Did you miss anything in the Model function ? How can I fetch the all stores because this seems to fetch only by storeCode. Commented May 29, 2019 at 12:27
  • OOPS ,typo mistake :) I have edited this answer
    – Amit Bera
    Commented May 29, 2019 at 12:30
  • For fetching all store data at the single request you need to do some code
    – Amit Bera
    Commented May 29, 2019 at 12:32
  • @amit. => from where we get this token : gv5dgldyscnzpa8sveg1xfct9frnwy0q Commented Aug 30, 2019 at 4:49
  • I used integration token devdocs.magento.com/guides/v2.3/get-started/authentication/…
    – Amit Bera
    Commented Aug 30, 2019 at 5:35

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.