Wednesday 21 September 2016

How to get a url parameter in Magento controller?

Is there a Magento function to get the value of "id" from this url:

http://domain.com/path/action/id/123

$id = $this->getRequest()->getParam('id', 0);

Friday 16 September 2016

How to get POST and GET data variables after submit a form in Magento

There Are two steps for get post/get data in magento

1 Way : You can get all variables using $this->getRequest()->getParams();
2 Way : You can get one variable using $this->getRequest()->getParam('id');

Thursday 15 September 2016

How to change the sort order for shipping in admin panel in magento



Step 1 : You have to copy this two files to your Theme in the adminhtml

app/design/adminhtml/default/default/template/sales/order/shipment/create/tracking.phtml
app/design/adminhtml/default/default/template/sales/order/invoice/create/tracking.phtml


Step 2 : Open : app/design/adminhtml/default/default/template/sales/order/shipment/create/tracking.phtml
(you will find this code at Line number 93)

Replace with

<select name="tracking[__index__][carrier_code]" id="trackingC__index__" class="select carrier" style="width:110px;" disabled="disabled">
    <?php foreach ($this->getCarriers() as $_code=>$_name): ?>
    <option value="<?php echo $_code ?>"><?php echo $this->escapeHtml($_name) ?></option>
    <?php endforeach; ?>
</select>

Step 3 : Open : app/design/adminhtml/default/default/template/sales/order/invoice/create/tracking.phtml
(you will find this code at Line number 93)

Replace with

<select name="tracking[__index__][carrier_code]" id="trackingC__index__" class="select carrier" style="width:110px;" disabled="disabled">
    <?php foreach ($this->getCarriers() as $_code=>$_name): ?>
    <option <?php if ($_code=="ups"): ?>selected<?php endif; ?> value="<?php echo $_code ?>"><?php echo $this->escapeHtml($_name) ?></option>
    <?php endforeach; ?>
</select>

Wednesday 14 September 2016

How to create custom model and resources file in existing module with database in magento

Step 1 : Update code in config.xml file of the model

<models>
...
...
<kanasagra_jaydip_resource>
...
...
<printsignsize>
   <table>name of your table</table>
</printsignsize>
</kanasagra_jaydip_resource>
</models>

Step 2 : Add new file in Model as Printsignsize.php

<?php
class Kanasagra_Jaydip_Model_Printsignsize extends Mage_Core_Model_Abstract
{

    const ENTITY    = 'kanasagra_jaydip_printsignsize';
    const CACHE_TAG = 'kanasagra_jaydip_printsignsize';
    protected $_eventPrefix = 'kanasagra_jaydip_printsignsize';
    protected $_eventObject = 'printsignsize';
    public function _construct()
    {
        parent::_construct();
        $this->_init('kanasagra_jaydip/printsignsize');
    }
    protected function _beforeSave()
    {
        parent::_beforeSave();
        $now = Mage::getSingleton('core/date')->gmtDate();
        if ($this->isObjectNew()) {
            $this->setCreatedAt($now);
        }
        $this->setUpdatedAt($now);
        return $this;
    }
    public function getPrintsignUrl()
    {
        return Mage::getUrl('kanasagra_jaydip/printsignsize/view', array('id'=>$this->getId()));
    }
    protected function _afterSave()
    {
        return parent::_afterSave();
    }
    public function getDefaultValues()
    {
        $values = array();
        $values['status'] = 1;
        return $values;
    }
 
}

Step 3 : Add new file in model/Resource as Printsignsize.php

<?php
class Kanasagra_Jaydip_Model_Resource_Printsignsize extends Mage_Core_Model_Resource_Db_Abstract
{
    public function _construct()
    {
        $this->_init('kanasagra_jaydip/printsignsize', 'entity_id');
    }
    public function lookupStoreIds($printsignId)
    {
        $adapter = $this->_getReadAdapter();
        $select  = $adapter->select()
            ->from($this->getTable('kanasagra_jaydip/printsignsize_store'), 'store_id')
            ->where('printsignsize_id = ?', (int)$printsignId);
        return $adapter->fetchCol($select);
    }
 
    protected function _afterLoad(Mage_Core_Model_Abstract $object)
    {
        if ($object->getId()) {
            $stores = $this->lookupStoreIds($object->getId());
            $object->setData('store_id', $stores);
        }
        return parent::_afterLoad($object);
    }
    protected function _getLoadSelect($field, $value, $object)
    {
        $select = parent::_getLoadSelect($field, $value, $object);
        if ($object->getStoreId()) {
            $storeIds = array(Mage_Core_Model_App::ADMIN_STORE_ID, (int)$object->getStoreId());
            $select->join(
                array('signprint_printsignsize_store' => $this->getTable('kanasagra_jaydip/printsignsize_store')),
                $this->getMainTable() . '.entity_id = signprint_printsignsize_store.printsignsize_id',
                array()
            )
            ->where('signprint_printsignsize_store.store_id IN (?)', $storeIds)
            ->order('signprint_printsignsize_store.store_id DESC')
            ->limit(1);
        }
        return $select;
    }
    protected function _afterSave(Mage_Core_Model_Abstract $object)
    {
        $oldStores = $this->lookupStoreIds($object->getId());
        $newStores = (array)$object->getStores();
     
        if (empty($newStores)) {
            $newStores = (array)$object->getStoreId();
        }
        $table  = $this->getTable('kanasagra_jaydip/printsignsize_store');
        $insert = array_diff($newStores, $oldStores);
        $delete = array_diff($oldStores, $newStores);
        if ($delete) {
            $where = array(
                'printsignsize_id = ?' => (int) $object->getId(),
                'store_id IN (?)' => $delete
            );
            $this->_getWriteAdapter()->delete($table, $where);
        }
        if ($insert) {
            $data = array();
            foreach ($insert as $storeId) {
                $data[] = array(
                    'printsignsize_id'  => (int) $object->getId(),
                    'store_id' => (int) $storeId
                );
            }
            $this->_getWriteAdapter()->insertMultiple($table, $data);
        }
        return parent::_afterSave($object);
    }
}

Step 4 : Add new file in model/Resource/Printsignsize as Collection.php


<?php
class Kanasagra_Jaydip_Model_Resource_Printsignsize_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract
{
    protected $_joinedFields = array();
    protected function _construct()
    {
        parent::_construct();
        $this->_init('kanasagra_jaydip/printsignsize');
        $this->_map['fields']['store'] = 'store_table.store_id';
    }
    public function addStoreFilter($store, $withAdmin = true)
    {
        if (!isset($this->_joinedFields['store'])) {
            if ($store instanceof Mage_Core_Model_Store) {
                $store = array($store->getId());
            }
            if (!is_array($store)) {
                $store = array($store);
            }
            if ($withAdmin) {
                $store[] = Mage_Core_Model_App::ADMIN_STORE_ID;
            }
            $this->addFilter('store', array('in' => $store), 'public');
            $this->_joinedFields['store'] = true;
        }
        return $this;
    }
    protected function _renderFiltersBefore()
    {
        if ($this->getFilter('store')) {
            $this->getSelect()->join(
                array('store_table' => $this->getTable('kanasagra_jaydip/printsignsize_store')),
                'main_table.entity_id = store_table.printsignsize_id',
                array()
            )
            ->group('main_table.entity_id');
            /*
             * Allow analytic functions usage because of one field grouping
             */
            $this->_useAnalyticFunction = true;
        }
        return parent::_renderFiltersBefore();
    }
    protected function _toOptionArray($valueField='entity_id', $labelField='materials', $additional=array())
    {
        return parent::_toOptionArray($valueField, $labelField, $additional);
    }
    protected function _toOptionHash($valueField='entity_id', $labelField='materials')
    {
        return parent::_toOptionHash($valueField, $labelField);
    }
    public function getSelectCountSql()
    {
        $countSelect = parent::getSelectCountSql();
        $countSelect->reset(Zend_Db_Select::GROUP);
        return $countSelect;
    }
}
Step 5 : Add following code into your Controller File

$printsignsize = Mage::getModel('kanasagra_jaydip/printsignsize');
                 
$printsignsize
             ->setPrintsignid($printsign->getId())
             ->setSizename($size['sizename'])
             ->setSizeword($size['sizeword'])
             ->setSizeprice($size['sizeprice'])
             ->setStores($answer['votes']);
$printsignsize->save();

Step 6 : Create table in your database.

Step 7 : Don't forget clear the cache. Finish n Enjoy!!!

Tuesday 13 September 2016

Direct Download Magento Module / Extension Zip File

Please click on following URL

Download Magento Extension

How to Check URL is Secure or Not in amazon EC2 Server in PHP / Codeigniter SSL

You can use following code for  Check URL is Secure or Not in amazon EC2 Server


if ($_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
      $_SERVER['HTTPS'] = 'on';
} else {
      $_SERVER['HTTPS'] = 'off';
}

Show thumbnail image / file in custom module admin form Magento




Just create one file and extended it with magento core abstract class. See example

Just add code into Your Form.php

class Kanasagra_Jaydip_Block_Adminhtml_{{Entity}}_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {
    protected function _prepareForm() {
       
        $form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
            'method' => 'post',
            'enctype' => 'multipart/form-data'
                )
        );
        $fieldset = $form->addFieldset('{{Entity}}_form', array('legend' => Mage::helper('{{Entity}}')->__('Product information')));
        $fieldset->addType('file', Mage::getConfig()                                                                                              ->getBlockClassName('jaydip/Adminhtml_{{Entity}}_Helper_File'));        $fieldset->addField('svgimage', 'file', array(            'label' => Mage::helper('jaydip')->__('Upload SVG'),            'required' => false,            'name' => 'svgimage',        ));     ...
     ...
     ...
    }
}

Create New file in Kanasagra/Jaydip/Block/Adminhtml/{{Entity}}/Helper/File.php

<?php
class Kanasagra_Jaydip_Block_Adminhtml_{{Entity}}_Helper_File extends Varien_Data_Form_Element_Abstract{
   
    public function __construct($data){
        parent::__construct($data);
        $this->setType('file');
    }
    public function getElementHtml(){
        $html = '';
        $this->addClass('input-file');
        $html.= parent::getElementHtml();
        if ($this->getValue()) {
            $url = $this->_getUrl();
            if( !preg_match("/^http\:\/\/|https\:\/\//", $url) ) {
                $url = Mage::getBaseUrl('media').'photo_image/images/' . $url; //replace this with the path to the file if you upload it somewhere else
            }
            $html .= '<br /><img width="100px" src="'.$url.'">';
        }
        return $html;
    }
    protected function _getUrl(){
        return $this->getValue();
    }
}