1

This is my system.xml file

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <tabs>
        <Threede_extensions translate="label" module="designit">
            <label>Threede Extensions</label>
            <sort_order>100</sort_order>
        </Threede_extensions>
    </tabs>  
    <sections>
        <designit>
            <label>Design It Settings</label>
            <tab>Threede_extensions</tab>
            <sort_order>1000</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store> 
           <groups>
               <general translate="label" module="designit">
                   <label>General</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>1</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <enablemodule translate="label">
                            <label>Enable Module</label>                            
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>1</sort_order>
                            <show_in_default>90</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </enablemodule>
                        <keytoken translate="label" module="designit">
                            <label>Token</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>110</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </keytoken>
                        <validate translate="label" module="designit">
                            <comment>Please save configuration value before validating.</comment>
                            <frontend_type>button</frontend_type>
                            <frontend_model>designit/adminhtml_system_config_form_field_validate</frontend_model>
                            <sort_order>120</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </validate>
                    </fields>
                </general>
                <synchronize translate="label" module="designit">
                   <label>Synchronize Products</label>
                    <frontend_type>select</frontend_type>
                    <sort_order>1</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <import translate="label">
                            <label>Products</label>
                            <frontend_type>button</frontend_type>
                            <frontend_model>designit/adminhtml_system_config_form_field_productimport</frontend_model>
                            <sort_order>120</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </import>
                   </fields>
                </synchronize>
           </groups>                            
        </designit>
    </sections>
</config>

This is my validate.php file

<?php

    class Threede_Designit_Block_Adminhtml_System_Config_Form_Field_Validate extends Mage_Adminhtml_Block_System_Config_Form_Field 
    {
        /*
         * Set template
         */
        protected function _construct()
        {
            parent::_construct();
            $this->setTemplate('designit/system/config/form/field/validate.phtml');
        }

        /**
         * Return element html
         *
         * @param  Varien_Data_Form_Element_Abstract $element
         * @return string
         */
        protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
        {
            return $this->_toHtml();
        }

        /**
         * Return ajax url for button
         *
         * @return string
         */
        public function getAjaxCheckUrl()
        {
            return Mage::helper('adminhtml')->getUrl('adminhtml/designit/validate');
        }

        /**
         * Generate button html
         *
         * @return string
         */
        public function getButtonHtml()
        {
            $button = $this->getLayout()->createBlock('adminhtml/widget_button')
                ->setData(array(
                'id'        => 'token_button',
                'label'     => $this->helper('adminhtml')->__('Validate Token'),
                'onclick'   => 'javascript:validate(); return false;'
            ));

            return $button->toHtml();
        }
    }

This is Productimport.php

<?php

class Threede_Designit_Block_Adminhtml_System_Config_Form_Field_Productimport extends Mage_Adminhtml_Block_System_Config_Form_Field 
{
    /*
     * Set template
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setTemplate('designit/system/config/form/field/productimport.phtml');
    }

    /**
     * Return element html
     *
     * @param  Varien_Data_Form_Element_Abstract $element
     * @return string
     */
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        return $this->_toHtml();
    }

    /**
     * Return ajax url for button
     *
     * @return string
     */
    public function getProductImportUrl()
    {
        return Mage::helper('adminhtml')->getUrl('adminhtml/designit/import');
    }

    /**
     * Generate button html
     *
     * @return string
     */
    public function getButtonHtml()
    {
        $button = $this->getLayout()->createBlock('adminhtml/widget_button')
            ->setData(array(
            'id'        => 'productimport_button',
            'label'     => $this->helper('adminhtml')->__('Synchronize Product'),
            'onclick'   => 'javascript:validate(); return false;'
        ));

        return $button->toHtml();
    }
}

This is productimport.phtml file

<script type="text/javascript">
    //<![CDATA[
    function validate() {
        new Ajax.Request('<?php echo $this->getProductImportUrl() ?>', {
            method:     'get',
            onSuccess: function(transport){
                data = JSON.parse(transport.responseText);
                alert(data.status_message)
            }
        });
    }
    //]]>
</script>

<?php echo $this->getButtonHtml() ?>

This is validate.phtml

<script type="text/javascript">
    //<![CDATA[
    function validate() {
        new Ajax.Request('<?php echo $this->getAjaxCheckUrl() ?>', {
            method:     'get',
            onSuccess: function(transport){
                data = JSON.parse(transport.responseText);
                alert(data.status_message)
            }
        });
    }
    //]]>
</script>

<?php echo $this->getButtonHtml() ?>

This is controller

<?php

class Threede_Designit_Adminhtml_DesignitController extends Mage_Adminhtml_Controller_action
{

    protected function _initAction() {
        $this->loadLayout()
            ->_setActiveMenu('designit/items')
            ->_addBreadcrumb(Mage::helper('adminhtml')->__('Items Manager'), Mage::helper('adminhtml')->__('Item Manager'));

        return $this;
    }   

    public function indexAction() {
        $this->_initAction()
            ->renderLayout();
    }
    public function validateAction()
    {

        $data['token'] = Mage::getStoreConfig('designit/general/keytoken');
        $model = Mage::getModel('designit/designit');
        $result = $model->callApi($data,'validate-token');
        //$result = $model->callApi($data,'stores/products');
        //Mage::log($result,null,'test.log');exit;
        Mage::app()->getResponse()->setBody($result);
    }  

    public function importAction()
    {

        $entityTypeId = Mage::getModel('eav/entity')
                ->setType('catalog_product')
                ->getTypeId();
        $attributeSetName   = 'Threede Set';
        $attributeSetId     = Mage::getModel('eav/entity_attribute_set')
                            ->getCollection()
                            ->setEntityTypeFilter($entityTypeId)
                            ->addFieldToFilter('attribute_set_name', $attributeSetName)
                            ->getFirstItem()
                            ->getAttributeSetId();

        $data['token'] = Mage::getStoreConfig('designit/general/keytoken');
        $model = Mage::getModel('designit/designit');
        $result = $model->callApi($data,'stores/products');
        $decoderesult = Mage::helper('core')->jsonDecode($result);

        foreach ($decoderesult['products'] as $key=>$pdct) {
            Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
            $product = Mage::getModel('catalog/product');

            $image_url  = $pdct['image']; 
            //Mage::log($image_url,null,'12.log');
            $imageString = file_get_contents($pdct['image']);
            $save = file_put_contents('/var/www/html/mage1/media/threede',$imageString);

            //~ $imageString = file_get_contents($image_url);
            //~ $save = file_put_contents('Image/saveto/image.jpg',$imageString);
            Mage::log($imageString,null,'1235.log');
            Mage::log($save,null,'1246.log');
            $product

            ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
            ->setAttributeSetId($attributeSetId) //ID of a attribute set named 'default'
            ->setTypeId('simple') //product type
            ->setCreatedAt(strtotime('now')) //product creation time

            ->setSku($pdct['product_id']) //SKU
            ->setName('New') //product name
            ->setWeight(4.0000)
            ->setStatus(1) //product status (1 - enabled, 2 - disabled)
            ->setTaxClassId(4) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
            ->setThreedeeProductType($pdct['type'])
            ->setThreedeeProductId($pdct['id'])
            ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility

            ->setPrice($pdct['price']) //price in form 11.22
            ->setCost(22.33) //price in form 11.22

            ->setMetaTitle('test meta title 2')
            ->setMetaKeyword('test meta keyword 2')
            ->setMetaDescription('test meta description 2')

            ->setDescription($pdct['description'])
            ->setShortDescription($pdct['description'])

            //~ ->setMediaGallery(array('images'=>array (), 'values'=>array ())) //media gallery initialization
            //~ ->addImageToMediaGallery(.$image_url, array('image','thumbnail','small_image'), false, false) //assigning image, thumb and small image to media gallery

            ->setStockData(array(
                               //'use_config_manage_stock' => 0, //'Use config settings' checkbox
                               'manage_stock'=>1, //manage stock
                               //'min_sale_qty'=>1, //Minimum Qty Allowed in Shopping Cart
                              // 'max_sale_qty'=>2, //Maximum Qty Allowed in Shopping Cart
                               'is_in_stock' => 1, //Stock Availability
                               'qty' => 999 //qty
                           )
            )

            ->setCategoryIds(array(3, 10)); //assign product to categories
            $product->save();
        }
        Mage::app()->getResponse()->setBody($result);
    }   

}

This is sytem config section http://i.prntscr.com/bb36c5a33c794902beb838db4ce5e308.png

But when I click on both button,same controller action is calling. http://i.prntscr.com/767754f12fef4b2b9d4cdffd7559f087.png

Please help. Both are working individually on commenting one section group code.

1

It seems like Javascript function name conflict issue, update your Productimport.php

public function getButtonHtml()
    {
        $button = $this->getLayout()->createBlock('adminhtml/widget_button')
            ->setData(array(
            'id'        => 'productimport_button',
            'label'     => $this->helper('adminhtml')->__('Synchronize Product'),
            'onclick'   => 'javascript:syncProducts(); return false;'
        ));

        return $button->toHtml();
    }

and update your productimport.phtml

<script type="text/javascript">
    //<![CDATA[
    function syncProducts() {
        new Ajax.Request('<?php echo $this->getProductImportUrl() ?>', {
            method:     'get',
            onSuccess: function(transport){
                data = JSON.parse(transport.responseText);
                alert(data.status_message)
            }
        });
    }
    //]]>
</script>

<?php echo $this->getButtonHtml() ?>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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