0

I need a button in stores > configuration that retrieves product data for the store view selected in the "scope" of the configuration. I created the button but I cannot retrieve the store (or the website) chosen via the "scope" dropdown.

I tried retrieving the current store id via StoreManager, which of course doesn't work because the current store is the admin store and not the system/config scoped store; I also tried using the http request but it doesn't work either:

/* \Magento\Framework\App\Request\Http $request */
$scopeId = (int) $this->request->getParam('store', 0); 

---

/* request in controller */
$this->_request->getParam('store');
$this->getRequest()->getParam('store');

I also tried using "store_id" in lieu of "store" as the request parameter but none of the above attempts were successful.

So, is there a way to retrieve the scoped store id of the system/config?

This is my button:


- Vendor/Module/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
  <section ...>
   <group ...>
    <field id="btn" translate="label" type="button" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
     <label>Button</label>
     <frontend_model>Vendor\Module\Block\System\Config\Button</frontend_model>
    </field>
    ...
   </group>
  </section>
 </system>
</config>

- Vendor/Module/Block/System/Config/Button.php
<?php

class Button extends Field
{
    protected $_template = 'Vendor_Module::system/config/button.phtml';

    public function __construct(Context $context, array $data = [])
    {
        parent::__construct($context, $data);
    }

    public function render(AbstractElement $element)
    {
        $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
        return parent::render($element);
    }

    protected function _getElementHtml(AbstractElement $element)
    {
        return $this->_toHtml();
    }

    public function getAjaxUrl()
    {
        return $this->getUrl('route/system_config/action');
    }

    public function getButtonHtml()
    {
        $button = $this->getLayout()->createBlock('Magento\Backend\Block\Widget\Button')->setData(['id' => 'btn_id', 'label' => __('Button'),]);
        return $button->toHtml();
    }

}

- Vendor/Module/Controller/Adminhtml/System/Config/Action.php

...

/* \Magento\Framework\App\Request\Http $request */
$storeId = (int) $this->request->getParam('store', 0);  // this returns 0


$result = $this->resultJsonFactory->create();
return $result->setData(['success' => true]);

Does anyone know how to get the scoped store id (or website) for this button?

Thanks

2 Answers 2

0

The solution turned out to be very simple indeed! I don't know if it is the right way but it worked for me. I simply retrieved the store parameter from the block instead of the controller, then I passed the store value as a parameter for my controller:

- Vendor/Module/Block/System/Config/Button.php
...
public function getAjaxUrl()
{
    return $this->getUrl('route/system_config/action', ['store' => $this->getRequest()->getParam('store', 0)]);
}
...

Now in my controller I can call the store parameter:

- Vendor/Module/Controller/Adminhtml/System/Config/Action.php
...
public function execute()
{
  $storeId = $this->getRequest()->getParam('store');
}
0

Try below code.

protected $state;
protected $storeManager;
public function __construct(   
    Context $context,
    \Magento\Framework\App\State $state,
    \Magento\Store\Model\StoreManagerInterface $storeManager
) {
    parent::__construct($context);
    $this->state = $state;
    $this->storeManager = $storeManager;
}

create your function and use below code.

public function getCurrentWebsiteId() {
    if ($this->state->getAreaCode() == \Magento\Framework\App\Area::AREA_ADMINHTML) {       
        $request = $this->_request;
        $storeId = (int) $request->getParam('store', 0);
    } else {
        $storeId = true; 
    }
    $store = $this->storeManager->getStore($storeId);
    $websiteId = $store->getWebsiteId();
    return $websiteId; }

check with object manager.

$storeManager = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface');
$storeId = (int) $this->getRequest()->getParam('store', 0);
$store = $storeManager->getStore($storeId);
$storeManager->setCurrentStore($store->getCode());
1
  • Maybe I'm understanding this wrong, but isn't this code doing the opposite of what I need? This code gets the store id from the url (which in my case is already 0 because I'm on backend) and in case nothing is found it returns 0, which is the admin id, not the scoped id of my config section.
    – Lou Nik
    May 16, 2022 at 11:13

Your Answer

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

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