Create Dependency Region selector without ajax in magento1


Some time we need Region selector from country.I have do some R&D and find that everywhere it stated with ajax.

I have built with core code so may someone find it helpfull.

if you are not on onepage checkout we have to add oncheckout js using layout file.

Below is code to add this checkout .


<action method="addItem"><type>skin_js</type><name>js/opcheckout.js</name></action>

now add html where you want to render this regrion selector.


<?php $cpBlock = $this->getLayout()->getBlockSingleton('checkout/onepage_billing');?>
<div class="field">
<label for="billing:region_id" class="required"><em>*</em><?php echo $this->__('State/Province') ?></label>
<div class="input-box">
<select id="billing:region_id" name="billing[region_id]" title="<?php echo Mage::helper('core')->quoteEscape($this->__('State/Province')) ?>" class="validate-select" style="display:none;">
<option value=""><?php echo $this->__('Please select region, state or province') ?></option>
</select>
<script type="text/javascript">
//<![CDATA[
$('billing:region_id').setAttribute('defaultValue', "<?php echo $cpBlock->getAddress()->getRegionId() ?>");
//]]>
</script>
<input type="text" id="billing:region" name="billing[region]" value="<?php echo $cpBlock->getAddress()->getRegion() ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('State/Province')) ?>" class="input-text <?php echo Mage::helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;" />
</div>
</div>
<div class="field name-lastname">
<label for="billing:region_id" class="required"><em style="">*</em><?php echo $this->__('Country') ?></label>
<div class="input-box">
<?php echo $cpBlock->getCountryHtmlSelect('billing') ?>
</div>
</div>
<script type="text/javascript">
//<![CDATA[

//billingForm.setElementsRelation('billing:country_id', 'billing:region', '<?php echo $this->getUrl('directory/json/childRegion') ?>', '<?php echo $this->__('Select State/Province...') ?>');

var billingRegionUpdater = new RegionUpdater('billing:country_id', 'billing:region', 'billing:region_id', <?php echo Mage::helper('directory')->getRegionJson() ?>, undefined, 'billing:postcode');
//]]>
</script>

Enjoy codding……………………………………………………………..!

 

Advertisements

Open popup in admin view using window.js


Your Button code should look something like this

Make sure you have window.js/default.css/magento.css added in page on which you are opening this popup

<dsv_adminhtml_dsv_index>
<reference name="head">
<action method="addItem"><type>js_css</type><name>prototype/windows/themes/default.css</name></action>
<action method="addCss"><name>lib/prototype/windows/themes/magento.css</name></action>
</reference>
</dsv_adminhtml_dsv_index>

If not then add this like below in your custom layout update xml


<button id="id_65ab52c5c84e5069a0c7debf5bb8be76" title="Edit Message" type="button" class="scalable add" onclick="dsvmaileditor.open('https://yourhost/index.php/admin_dsv/adminhtml_dsv/renderemailform/key/c5b25f40b86f9c0c7873836b27f7e430/', 'mail_edit_form')" style=""><span><span><span>Edit Message</span></span></span></button>

dsvmaileditor will be define in js code

On above button onclick open first  parameter should be ajax URL which return HTML and second is id anything i don’t know where it is used but it should be in parameter.

 

Now just add below JS code in your page.


var dsvmaileditor = {
overlayShowEffectOptions : null,
overlayHideEffectOptions : null,
open : function(editorUrl, elementId) {
if (editorUrl && elementId) {
new Ajax.Request(editorUrl, {
parameters: {
element_id: elementId,
},
onSuccess: function(transport) {
try {
this.openDialogWindow(transport.responseText, elementId);
} catch(e) {
alert(e.message);
}
}.bind(this)
});
}
},
openDialogWindow : function(content, elementId) {
this.overlayShowEffectOptions = Windows.overlayShowEffectOptions;
this.overlayHideEffectOptions = Windows.overlayHideEffectOptions;
Windows.overlayShowEffectOptions = {duration:0};
Windows.overlayHideEffectOptions = {duration:0};

Dialog.info(content, {
draggable:true,
resizable:true,
closable:true,
className:"magento",
windowClassName:"popup-window",
title:"Edit Mail Subject and Body",
width:550,
height:400,
zIndex:1000,
recenterAuto:true,
hideEffect:Element.hide,
showEffect:Element.show,

});

content.evalScripts.bind(content).defer();
},

};

My controller action just return phtml file html something like this


public function renderemailformAction() { 
$myBlock = $this->getLayout()->createBlock('core/template');        
$myBlock->setTemplate('dsv/view/emailedit.phtml');        
$myHtml =  $myBlock->toHtml();        
$this->getResponse()->setHeader('Content-Type', 'text/html')->setBody($myHtml);          
return; 
}



You can open these dialog using window.js

Dialog Module
– confirm
– alert
– info
– setInfoMessage
– closeInfo

 

I have used Dialog.info

you can use as per your requirement.

For more info you can visit this site

Window JS Reference

 

Translate text write on layout xml file.


There is sometime we need this translation to translate it globally. I have spend lot of time to find this solution so I thought may someone can save sometime to get rid from this.

Hope someone can take help from this.

and enjoy his time to developed new thing 🙂

Here is how translation work for layout file.


<checkout_cart_index>
<reference name="breadcrumbs">
<action method="addCrumb" translate="crumbInfo.label crumbInfo.title">
<crumbName>Beregn pris</crumbName>
<crumbInfo>
<label>Beregn pris</label>
<title>Beregn pris</title>
<link>/beregn-pris</link>
</crumbInfo>
</action>
<action method="addCrumb" translate="crumbInfo.label crumbInfo.title">
<crumbName>Cart</crumbName>
<crumbInfo>
<label>Indkøbskurv</label>
<title>Indkøbskurv</title>
</crumbInfo>
</action>
</reference>
</checkout_cart_index>

in the above example you have notice that crumbInfo.label crumbInfo.title this is because it label and title is child of crumbInfo so you have to define it using dot(.)

for parent element tab you just need to write translate=”label title” only here you can write multiple attribute with (Space) separated. It will translate each element.

Now you just need to add translate.csv in you locale folder with you locale code folder.

 

 

Add custom option to quote item programmatically magento


Magento provides a capability for adding options that aren’t product attributes or product custom options. They are set on the product and quote items with the option code additional_options.

There are two steps you need to take, each can be handled via an event observer. If you want the additional options to carry through reordering, you will need also observe a third event.

Add options to quote item

The first step is to add the event observer to set the additional options on the loaded product before it is added to the cart. One option is to use the catalog_product_load_after event.

<catalog_product_load_after>
    <observers>
        <extra_options>
            <type>model</type>
            <class>extra_options/observer</class>
            <method>catalogProductLoadAfter</method>
        </extra_options>
    </observers>
</catalog_product_load_after>

In the event observer you can add additional checks the requested page is indeed an add to cart action. The main point of this observer method is to add the selection of your special options to the additional_options option on the product model.
public function catalogProductLoadAfter(Varien_Event_Observer $observer)
{
    // set the additional options on the product
    $action = Mage::app()->getFrontController()->getAction();
    if ($action->getFullActionName() == 'checkout_cart_add')
    {
        // assuming you are posting your custom form values in an array called extra_options...
        if ($options = $action->getRequest()->getParam('extra_options'))
        {
            $product = $observer->getProduct();

            // add to the additional options array
            $additionalOptions = array();
            if ($additionalOption = $product->getCustomOption('additional_options'))
            {
                $additionalOptions = (array) unserialize($additionalOption->getValue());
            }
            foreach ($options as $key => $value)
            {
                $additionalOptions[] = array(
                    'label' => $key,
                    'value' => $value,
                );
            }
            // add the additional options array with the option code additional_options
            $observer->getProduct()
                ->addCustomOption('additional_options', serialize($additionalOptions));
        }
    }
}

The additional options will be moved from the product to the quote item automatically. With this observer in place, your options will appear in the cart and the checkout review.

Add options to order item

In order to have them persist, one additional observer is needed (only since Magento 1.5).

 

<sales_convert_quote_item_to_order_item>
    <observers>
        <extra_options>
            <type>model</type>
            <class>extra_options/observer</class>
            <method>salesConvertQuoteItemToOrderItem</method>
        </extra_options>
    </observers>
</sales_convert_quote_item_to_order_item>
Here we move the option from the quote item to the order item.

public function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer)
{
    $quoteItem = $observer->getItem();
    if ($additionalOptions = $quoteItem->getOptionByCode('additional_options')) {
        $orderItem = $observer->getOrderItem();
        $options = $orderItem->getProductOptions();
        $options['additional_options'] = unserialize($additionalOptions->getValue());
        $orderItem->setProductOptions($options);
    }
}

From this point on the additional options will be visible in the customer order history in the frontend and the order emails, as well as in the admin interface order view, invoices, shipments, creditmemos and PDFs.

Add support for reorders

In order to carry the oprions over to the new order during a reorder, you need to take care to copy them over. Here is one possibility using the checkout_cart_product_add_after event.

<checkout_cart_product_add_after>
    <observers>
        <extra_options>
            <type>singleton</type>
            <class>extra_options/observer</class>
            <method>checkoutCartProductAddAfter</method>
        </extra_options>
    </observers>
</checkout_cart_product_add_after>

The parsing of the extra options and building the additional options array should be moved into a separate function to avoid code duplication, but for this example I’ll leave the required logic for each method in place for clarity.

public function checkoutCartProductAddAfter(Varien_Event_Observer $observer)
{
    $action = Mage::app()->getFrontController()->getAction();
    if ($action->getFullActionName() == 'sales_order_reorder')
    {
        $item = $observer->getQuoteItem();
        $buyInfo = $item->getBuyRequest();
        if ($options = $buyInfo->getExtraOptions())
        {
            $additionalOptions = array();
            if ($additionalOption = $item->getOptionByCode('additional_options'))
            {
                $additionalOptions = (array) unserialize($additionalOption->getValue());
            }
            foreach ($options as $key => $value)
            {
                $additionalOptions[] = array(
                    'label' => $key,
                    'value' => $value,
                );
            }
            $item->addOption(array(
                'code' => 'additional_options',
                'value' => serialize($additionalOptions)
            ));
        }
    }
}

Translation:

There is no mechanism in place to translate these option labels or values. Here are a few ideas that might be useful in that regard.

In a quote_item_load_after event observer, get the additional options array and set $option['print_value'] = $helper->__($option['value']);. If print_value is set, Magento will use that for rendering the display.
The same can be done with order items.

There is no such thing as a print_label, but you could set a custom index (label_source maybe) and set the label on the fly using that as the source, e.g. $option['label'] = $helper->__($option['label_source']);.

Beyond that you would probably have to resort to modifying the templates (grep for getItemOptions()), or overriding the block classes (grep additional_options).


 

Source :: http://stackoverflow.com/questions/9412074/magento-quote-order-product-item-attribute-based-on-user-input/9496266#9496266

Make magento grid row unclickable


some time we need to make row unclickable for specific reason.I had situation in which i have select box and checkbox in grid. so when i click on selectbox it fired row click event and disable my dropdown.Its irritating for any user.so i have find out solution for this.

edit your grid file __construct function like this.

public function __construct()
    {
        parent::__construct();
        $this->setId('customerGrid');
        $this->setUseAjax(true); // Using ajax grid is important
        $this->setDefaultSort('id');
        $this->setDefaultFilter(array('customer_ids'=>1)); // By default we have added a filter for the rows, that in_products value to be 1
        $this->setSaveParametersInSession(false);  //Dont save paramters in session or else it creates problems
        $this->setAdditionalJavaScript("
        // added click on selectbox support
        serializerController.prototype.rowClick = serializerController.prototype.rowClick.wrap(function(o, grid, event) {
            var tagName = Event.element(event).tagName
                isSelect = (tagName == 'SELECT' || tagName == 'OPTION');
            if (!isSelect) {
                o(grid, event);
            }
        });
        ");
    }

How To Set Dynamic Variable in CMS Content


public function setVariableforCMSBlock($BlockId,$VariableArray=""){

	if(!$blockId){
		return;
	}
	$block = Mage::getModel('cms/block')
		->setStoreId(Mage::app()->getStore()->getId())
		->load($blockId);
	$filter = Mage::getModel('cms/template_filter');
	$filter->setVariables($array);
	return $filter->filter($block->getContent());
	
}

use of function 
$test['test1'] = 'test1';
$test['test2'] = 'test2';

Mage::helper('example')->setVariableforCMSBlock('testCmsBlockId',$test);

you can set content in cms block like

Hello this is {{var test1}} and this is another {{var test2}}

OUTPUT :: Hello this is test1 and this is another test2

Add custom variable to cms block in magento


Cms content :: Hello {{variable1}},

Thanks we will contact you.

Thanks
[‘variable2’]

Make This function in any of your helper file.

public function getFilterContent($html = '')
	{
	
		if($html != ''){
			$array = array();
			$array['variable1'] = 'customername';
			$array['variable2'] = 'Team'; /* you can set any no of variable like this */
	 
			// loading the filter which will get the array we created and parse the block content
			$filter = Mage::getModel('cms/template_filter');
			/* @var $filter Mage_Cms_Model_Template_Filter */
			$filter->setVariables($array);
	 
			// return the filtered block content.
			return $filter->filter($html);
		}else{
			return;
		}
	}
}

Import large mysql database using mysql command prompt


Click your wamp server icon then look for MYSQL > MSQL Console then run it.
console

If you dont have password, just hit enter and type :

mysql> use database_name;
mysql> source location_of_your_file;
If you have password, you will promt to enter a password. Enter you password first then type:

mysql> use database_name;
mysql> source location_of_your_file;
location_of_your_file should look like C:/mydb.sql

for desktop my location is like :: C:/Users/Zaheer/Desktop/db.sql/db.sql
so the command is mysql>source C:/Users/Zaheer/Desktop/db.sql/db.sql;

This kind of importing sql dump is very helpful for BIG SQL FILE.

I copied my file mydb.sq to directory C: .It should be capital C: in order to run

and that’s it.