3

I am using Magento version 2.4 and with a need to have a custom button in admin products listing, which would redirect users to a custom admin page. I created view/adminhtml/product_listing.xml with following code.

<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="buttons" xsi:type="array">
            <item name="new_button" xsi:type="array">
                <item name="name" xsi:type="string">odoo_sync_button</item>
                <item name="label" xsi:type="string" translate="true">Sync stock with Odoo Manually</item>
                <item name="class" xsi:type="string">action-secondary</item>
                <item name="url" xsi:type="string">odoo_sync/sync/index</item>
            </item>
        </item>
    </argument>
</listing>

Can someone please let me know the correct syntax for attaching this URL to the button ?

Button is rendered but on click , it redirects to the dashboard with the message 'Invalid security or form key. Please refresh the page.'.

Same custom admin page is accessible via Admin menu. Please see etc/adminhtml/routes.xml & etc/adminhtml/menu.xml

routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="odoosync" frontName="odoosync">
            <module name="Iwdat_StockManager172" />
        </route>
    </router>
</config>

menu.xml

<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
    <menu>
        <add id="mnu_odoo::manager" title="Odoo Sync" module="Iwdat_StockManager172" sortOrder="10" resource="Iwdat_StockManager172::manager"/>
        <add id="mnu_odoo_sync::manager" title="Odoo Sync Go" module="Iwdat_StockManager172" sortOrder="0" parent="mnu_odoo::manager"
                action="odoosync/sync" resource="Iwdat_StockManager172::manager"/>
    </menu>
</config>

Controller in Controller/Adminhtml/Sync folder

<?php
namespace Iwdat\StockManager172\Controller\Adminhtml\Sync;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; use Psr\Log\LoggerInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku; use Magento\Catalog\Model\Product; use Magento\CatalogInventory\Api\StockRegistryInterface; use Magento\Catalog\Model\ProductRepository; use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
use Magento\InventoryApi\Api\SourceItemsSaveInterface; use Magento\Framework\Controller\ResultFactory; class Index extends \Magento\Backend\App\Action { . . . }

Thanks and Best Regards

Indunil

1 Answers1

1

Replace your product_listing.xml with below code:

<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="buttons" xsi:type="array">
            <item name="new_button" xsi:type="array">
                <item name="name" xsi:type="string">odoo_sync_button</item>
                <item name="label" xsi:type="string" translate="true">Sync stock with Odoo Manually</item>
                <item name="class" xsi:type="string">action-secondary</item>
                <item name="url" xsi:type="string">odoosync/sync/index</item>
            </item>
        </item>
    </argument>
</listing>

This will work for you.

Please let me know if you got any issue

Arun Sharma
  • 1,315
  • 1
  • 9
  • 33