Need to Add Custom Button and Related action in System->Config->Custom section as field

by Yogesh   Last Updated January 13, 2018 16:09 PM

I am Adding Custom Section in System->Config Tab.

I need to add Button as Field and its related action which i need to perform. but i am not found on google for button all example provide other field option but hard to get button example.

Other Section Working fine but i am not able to add button and related action (filepath)which call on click.

    <fields> 
           <goldprice translate="label"> 
               <label>GoldPrice</label>                   
               <frontend_type>text</frontend_type>                
               <sort_order>0</sort_order>  
               <show_in_default>1</show_in_default> 
               <show_in_website>1</show_in_website> 
               <show_in_store>1</show_in_store> 
               <comment>Gold Price per 1gm</comment> 
          </goldprice> 
          <run translate="label">
             <frontend_type>button</frontend_type>
<frontend_model>letsnurture_customgrid/adminhtml_system_config_customfield</frontend_model>
             <sort_order>30</sort_order>
             <show_in_default>1</show_in_default>
             <show_in_website>1</show_in_website>
             <show_in_store>1</show_in_store>
        </run> 
</fields>

My Config File Like Below

<?xml version="1.0"?>
<config>
  <modules>
    <LetsNurture_CustomGrid>
      <version>0.1.0</version>
    </LetsNurture_CustomGrid>
  </modules>
  <global>
    <blocks>
      <customgrid>
              <class>LetsNurture_CustomGrid_Block</class>
      </customgrid>
    </blocks>
    <models>
         <class>LetsNurture_CustomGrid_Model</class>
    </models>
    <helpers>
      <customgrid>
        <class>LetsNurture_CustomGrid_Helper</class>
      </customgrid>
    </helpers>
    <events>
          <admin_system_config_changed_section_gold> <!-- identifier of the event we want to catch -->
          <observers>
              <customgrid>
                  <type>model</type>
                  <class>LetsNurture_CustomGrid_Model_Observer</class>
                  <method>UpdatePrice</method>
              </customgrid>
          </observers>
       </admin_system_config_changed_section_gold>
  </events>
  </global>
</config>

And Customfiled.php like below

    class LetsNurture_CustomGrid_Block_Adminhtml_System_Config_Customfield extends Mage_Adminhtml_Block_System_Config_Form_Field
{
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $url = $this->getUrl('catalog/product'); //

        $html = $this->getLayout()->createBlock('adminhtml/widget_button')
                    ->setType('button')
                    ->setClass('scalable')
                    ->setLabel('Run Now !')
                    ->setOnClick("setLocation('$url')")
                    ->toHtml();


        return $html;
    }
}

But Its Give me Warning like this "failed to open stream: No such file or directory"

Thanks, Yogesh



Answers 2


<frontend_model> path goes to to Blocks (instead of Models).

Incidentally, even though it’s named a model class but we have to create it as a Magento Block class since this is how the other built in front end model classes are implemented.

As well as, it is used for template output so it’s appropriate to create a block class for this.

Example

<frontend_model>NAMESPACE_MODULENAME/adminhtml_system_config_customfield</frontend_model>

So

Create Customfield.php under .../NAMESPACE/MODULENAME/Block/Adminhtml/System/Config/ with the following content:

class NAMESPACE_MODULENAME_Block_Adminhtml_System_Config_Customfield extends Mage_Adminhtml_Block_System_Config_Form_Field
{


    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {       
        $html = parent::_getElementHtml($element);
       //Write your custom html here
        return $html;
    }
}

Hope this help you

Keyur Shah
Keyur Shah
October 21, 2015 09:40 AM

i have this code on my config.xml

    <crontab>
    <jobs>
        <sariina_payvast_purchase_price_update>
            <schedule>
                <cron_expr>* 1 * * *</cron_expr>
            </schedule>
            <run>
                <model>sariina_payvast/observer::getMerchandiseLastBuyRate</model>
            </run>
        </sariina_payvast_purchase_price_update>
        <sariina_payvast_stock_update>
            <schedule>
                <cron_expr>30 1 * * *</cron_expr>
            </schedule>
            <run>
                <model>sariina_payvast/observer::getMerchandiseRemainForAllStockRooms</model>
            </run>
        </sariina_payvast_stock_update>
    </jobs>
</crontab>

how i can execute this code immediately by click the button ? function declare in Observer.php like this code

   public function getMerchandiseLastBuyRate()
{
    if (Mage::getStoreConfig('payvast/general/enable_purchase_price'))
    {
        Mage::log('Payvast LastBuyRate Update Start...', null, 'payvast.log');

        $client = new SoapClient(Mage::getStoreConfig('payvast/general/url'));
        $products = Mage::getModel('catalog/product')->getCollection();
        $products->addAttributeToSelect('sku');
        $products->addAttributeToSelect('last_purchase_price');
        foreach($products as $product)
        {
            Mage::log('Updating Last Purchase Price for '.$product->getSku(), null, 'payvast.log');
            $params = array(
                'merchandiseCode'   =>  $product->getSku()
            );  
            $response   =   $client->GetMerchandiseLastBuyRate($params);    
    //Mage::log($response, null, 'payvast.log');
            if ($product->getLastPurchasePrice() != $response->GetMerchandiseLastBuyRateResult/10)
            {
      /* Rial to Toman */
                $product->setLastPurchasePrice($response->GetMerchandiseLastBuyRateResult/10)->save();
            }
        }

        Mage::log('Payvast LastBuyRate Update Finished.', null, 'payvast.log');
    }
}

public function getMerchandiseRemainForAllStockRooms()
{
    if (Mage::getStoreConfig('payvast/general/enable_stock'))
    {
        Mage::log('Payvast Stock Update Start...', null, 'payvast.log');

        $client = new SoapClient(Mage::getStoreConfig('payvast/general/url'));
        $products = Mage::getModel('catalog/product')->getCollection();
        $products->addAttributeToSelect('sku');
        foreach($products as $product)
        {
            Mage::log('Updating Stock for '.$product->getSku(), null, 'payvast.log');
            $date = date("Y-m-d");
            $fiscalPeriod = Mage::getStoreConfig('payvast/general/fiscal_period');
                    $username = Mage::getStoreConfig('payvast/general/username');
                    $password = Mage::getStoreConfig('payvast/general/password');
            $params = array(
                'merchandiseCode'   =>  $product->getSku(),
                        'date'                  =>  $date,
                        'fiscalPeriod'      =>  $fiscalPeriod,
                        'username'      =>  $username,
                        'password'      =>  $password
            );  
            $response   =   $client->GetMerchandiseRemainForAllStockRooms($params); 
            //Mage::log($response, null, 'payvast.log');
            $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
            if ($stockItem->getId() > 0 and $stockItem->getManageStock())
            {
                $qty = $response->GetMerchandiseRemainForAllStockRoomsResult;
                if ($stockItem->getQty() != $qty)
                {
                    $stockItem->setQty($qty);
                    $stockItem->setIsInStock((int)($qty > 0));
                    $stockItem->save();
                }
            }
        }

        Mage::log('Payvast Stock Update Finished.', null, 'payvast.log');
    }
}
user3062126
user3062126
January 13, 2018 15:49 PM

Related Questions



System Config page at backend is showing Blank page

Updated March 24, 2017 07:09 AM

System Config Multiselect default all selected

Updated March 16, 2017 06:09 AM

Magento 2: Store Config Missing on Backend

Updated May 14, 2018 21:09 PM

System configuration custom source model error

Updated July 07, 2015 12:43 PM