Get value for Number of Lines in a Street Address that have been set in magento 2 configuration

helpermagento2magento2.3.5magento2.3.5-p2street-address

I am using Magento 2.3.5-p2

I know that in Magento 2, the Street address configuration is not able to be changed through admin by going to

Stores > Settings > Configuration > Customers > Costumer Configuration > Name and Address Options > Number of Lines in a Street Address

enter image description here

now my task is that I am required to retrieve value of Number of Lines in a Street Address being set above to below file so that I can create some condition

vendor/magento/module-customer/Controller/Address/FormPost.php

what helper or object that I needed to use to be able to get the value of Number of Lines in a Street Address being set

Thank you

Best Answer

You can create your own helper like:

<?php

namespace MyVendor\MyModule\Helper;

use \Magento\Framework\App\Helper\AbstractHelper;
use \Magento\Framework\App\Helper\Context;
use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{

    const CONFIG_ADDRESS_NUMBER_OF_LINES = 'customer/address/street_lines';

    /**
     * @var ScopeConfigInterface
     */
    protected $scopeConfig;

    public function __construct(
        Context $context,
        ScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
        parent::__construct($context);
    }

    public function getAddressNumberOfLines()
    {
        return $this->scopeConfig->getValue(self::CONFIG_ADDRESS_NUMBER_OF_LINES, ScopeInterface::SCOPE_STORE);
    }
}

And inject the helper to your block or controller or by object manager:

$helper = \Magento\Framework\App\ObjectManager::getInstance()->get(\MyVendor\MyModule\Helper\Data::class);
echo $helper->getAddressNumberOfLines();

P.S.S you can also direct the first code to your block or controller if you dont want to used helper Sample Code via Object Manager

$scopeConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\App\Config\ScopeConfigInterface::class);
$streetLines = $scopeConfig->getValue('customer/address/street_lines', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
echo $streetLines;

P.S. using Object Manager is not advisable and you should inject it

*you can find the config path here: vendor/magento/module-customer/etc/adminhtml/system.xml

*config default values was set here: vendor/magento/module-customer/etc/config.xml

you can get others path by opening the system.xml

E.g. customer/address/street_lines

  • customer is the section id
  • address is the group id
  • street_lines is the field id
Related Topic