Shopify, Shopify Apps, Magento, WordPress, Codeigniter, Joomla, Big Commerce | PHP
Saturday, 22 August 2015
Get controller, module, action and router name
You can easily get controller name, action name,
router name and module name in any template file or class file.
IN TEMPLATE FILES$this->getRequest() can be used in template (phtml) files.
/** * get Controller name */ $this->getRequest()->getControllerName(); /** * get Action name, i.e. the function inside the controller */ $this->getRequest()->getActionName(); /** * get Router name */ $this->getRequest()->getRouteName(); /** * get module name */ $this->getRequest()->getModuleName();
/** * get Current URL */
$currentUrl = Mage::helper('core/url')->getCurrentUrl(); $url = Mage::getSingleton('core/url')->parseUrl($currentUrl); $path = $url->getPath();
IN CLASS FILES
/*** get Controller name*/Mage::app()->getRequest()->getControllerName();/*** get Action name, i.e. the function inside the controller*/Mage::app()->getRequest()->getActionName();/*** get Router name*/Mage::app()->getRequest()->getRouteName();/*** get module name*/Mage::app()->getRequest()->getModuleName();
Thursday, 20 August 2015
Simple Ajax add to cart in magento
list.phtml page in template
<form class="product_addtocart_form" action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<?php if(!$_product->isGrouped()): ?>
<label for="qty" class="qtylabel" style=""><?php echo $this->__('Qty') ?>:</label>
<input type="text" class="form-control qtybox" name="qty">
<?php endif; ?>
<button type="submit" class="link_cart"><span><i class="fa fa-shopping-cart" style="margin-right: 5px;"></i><?php echo $this->__('Add to Cart') ?></span></button>
</form>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('.product_addtocart_form').submit(function(event){
event.preventDefault();
var $this = jQuery(this);
jQuery($this).parents('li').append('<div class="ajax_loader"></div>');
url = jQuery(this).attr('action');
console.log(jQuery(this).serialize());
url += '&isAjax=1';
var data = jQuery(this).serialize();
data += '&isAjax=1';
jQuery.ajax({
url : url,
dataType : 'json',
type : 'post',
data : data,
success : function(data) {
getcartdata($this);
}
});
setTimeout(function(){
//getcartdata($this);
},1000);
});
});
function getcartdata($this){
jQuery.ajax( {
url : "<?php echo Mage::getBaseUrl().'getcitys/index/getcartdetail'; ?>",
dataType : 'html',
type : 'get',
success : function(data) {
jQuery('.ajax_loader').hide();
jQuery('.block-cart-header').html(data);
}
});
}
</script>
Make in controllers/IndexController.php
public function getcartdetailAction() {
$block = $this->getLayout()->createBlock('checkout/cart_sidebar');
$block->setTemplate('checkout/cart/sidebar_header_ajax.phtml');
echo $block->toHtml();exit;
}
view.phtml page in template
Replace above code To below code
productAddToCartForm.submit = function(button, url) {
if (this.validator.validate()) {
var form = this.form;
var oldUrl = form.action;
if (url) {
form.action = url;
}
var e = null;
if(!url){
url = jQuery('#product_addtocart_form').attr('action');
}
var data = jQuery('#product_addtocart_form').serialize();
data += '&isAjax=1';
try {
jQuery('.product-shop').prepend('<div class="ajax_loader"> </div>');
jQuery.ajax({
url: url,
dataType: 'json',
type : 'post',
data: data,
success: function(data){
getcartdata(this);
}
});
setTimeout(function(){
getcartdata(this.form);
},1000);
} catch (e) {
}
this.form.action = oldUrl;
if (e) {
throw e;
}
}
}.bind(productAddToCartForm);
Write Bottom in view page
<script type="text/javascript">
function getcartdata($this){
jQuery.ajax( {
url : "<?php echo Mage::getBaseUrl().'getcitys/index/getcartdetail'; ?>",
dataType : 'html',
type : 'get',
success : function(data) {
jQuery('.ajax_loader').hide();
jQuery('.block-cart-header').html(data);
}
});
}
</script>
<form class="product_addtocart_form" action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<?php if(!$_product->isGrouped()): ?>
<label for="qty" class="qtylabel" style=""><?php echo $this->__('Qty') ?>:</label>
<input type="text" class="form-control qtybox" name="qty">
<?php endif; ?>
<button type="submit" class="link_cart"><span><i class="fa fa-shopping-cart" style="margin-right: 5px;"></i><?php echo $this->__('Add to Cart') ?></span></button>
</form>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('.product_addtocart_form').submit(function(event){
event.preventDefault();
var $this = jQuery(this);
jQuery($this).parents('li').append('<div class="ajax_loader"></div>');
url = jQuery(this).attr('action');
console.log(jQuery(this).serialize());
url += '&isAjax=1';
var data = jQuery(this).serialize();
data += '&isAjax=1';
jQuery.ajax({
url : url,
dataType : 'json',
type : 'post',
data : data,
success : function(data) {
getcartdata($this);
}
});
setTimeout(function(){
//getcartdata($this);
},1000);
});
});
function getcartdata($this){
jQuery.ajax( {
url : "<?php echo Mage::getBaseUrl().'getcitys/index/getcartdetail'; ?>",
dataType : 'html',
type : 'get',
success : function(data) {
jQuery('.ajax_loader').hide();
jQuery('.block-cart-header').html(data);
}
});
}
</script>
Make in controllers/IndexController.php
public function getcartdetailAction() {
$block = $this->getLayout()->createBlock('checkout/cart_sidebar');
$block->setTemplate('checkout/cart/sidebar_header_ajax.phtml');
echo $block->toHtml();exit;
}
view.phtml page in template
productAddToCartForm.submit = function(button, url) {
if (this.validator.validate()) {
var form = this.form;
var oldUrl = form.action;
if (url) {
form.action = url;
}
var e = null;
try {
this.form.submit();
} catch (e) {
}
this.form.action = oldUrl;
if (e) {
throw e;
}
if (button && button != 'undefined') {
button.disabled = true;
}
}
}.bind(productAddToCartForm);
if (this.validator.validate()) {
var form = this.form;
var oldUrl = form.action;
if (url) {
form.action = url;
}
var e = null;
try {
this.form.submit();
} catch (e) {
}
this.form.action = oldUrl;
if (e) {
throw e;
}
if (button && button != 'undefined') {
button.disabled = true;
}
}
}.bind(productAddToCartForm);
Replace above code To below code
productAddToCartForm.submit = function(button, url) {
if (this.validator.validate()) {
var form = this.form;
var oldUrl = form.action;
if (url) {
form.action = url;
}
var e = null;
if(!url){
url = jQuery('#product_addtocart_form').attr('action');
}
var data = jQuery('#product_addtocart_form').serialize();
data += '&isAjax=1';
try {
jQuery('.product-shop').prepend('<div class="ajax_loader"> </div>');
jQuery.ajax({
url: url,
dataType: 'json',
type : 'post',
data: data,
success: function(data){
getcartdata(this);
}
});
setTimeout(function(){
getcartdata(this.form);
},1000);
} catch (e) {
}
this.form.action = oldUrl;
if (e) {
throw e;
}
}
}.bind(productAddToCartForm);
Write Bottom in view page
<script type="text/javascript">
function getcartdata($this){
jQuery.ajax( {
url : "<?php echo Mage::getBaseUrl().'getcitys/index/getcartdetail'; ?>",
dataType : 'html',
type : 'get',
success : function(data) {
jQuery('.ajax_loader').hide();
jQuery('.block-cart-header').html(data);
}
});
}
</script>
Wednesday, 19 August 2015
How to import database using ubuntu terminal or windwos CMD
For linux ubunto
pv /var/www/html/decorati_main.sql.gz | gunzip | mysql -u root -p root
apt-get install pv
pv /var/www/html/decorati_main.sql.gz | gunzip | mysql -u root -p decorativeplumb
For Windows
mysql -uroot -ppassword mydb > myfile.sql.gz
Tuesday, 18 August 2015
How to get only cart sidebar Layout block in magento
In your controller you could try something like this:
$block = $this->getLayout()->createBlock('checkout/cart_sidebar'); $block->setTemplate('checkout/cart/sidebar.phtml');
Depending on your configuration (Config -> Checkout -> Shopping Cart Sidebar),
you can render the template with
$block->toHtml();
If you use a custom template, you could ignore the config value so it renders anytime.
JQuery serialize() function with $.ajax() post for bigger HTML forms
JQuery provides a very useful function jQuery.serialize() which encodes a set of form elements as a string for submission, and is very useful while we are dealing with huge HTML forms. Basically it will combine your values of form element and then you can post it on any server side scripting page.
For example, suppose you have a very long form with many fields and you want to post it with the help of $.ajax() function, you need to add just few lines of code for jquery from with ajax, no need to add id attribute to any form elements, just have to give id to form itself as follows:
The above form will have all the fields like text boxes, text areas, select boxes, radios etc. having only name attribute, just as our usual html form. Now the JQuery code will be as follows:
For example, suppose you have a very long form with many fields and you want to post it with the help of $.ajax() function, you need to add just few lines of code for jquery from with ajax, no need to add id attribute to any form elements, just have to give id to form itself as follows:
<form name="frm_details" id="frm_details" method="post">
<input type="text" name="name" value="jems">
<input type="submit" name="submit" value="submit">
</form>
The above form will have all the fields like text boxes, text areas, select boxes, radios etc. having only name attribute, just as our usual html form. Now the JQuery code will be as follows:
<script>
$(function() {
$("#product_details").on("submit", function(event) {
event.preventDefault();
$.ajax({
url: "somefile.php",
type: "post",
data: $(this).serialize(),
success: function(d) {
alert(d);
}
});
});
});
</script>
Saturday, 8 August 2015
Including jQuery condition if in page have jQuery or not
// Including jQuery conditionnally. if (typeof jQuery === 'undefined') { document.write("\u003cscript src=\"\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1\/jquery.min.js\" type=\"text\/javascript\"\u003e\u003c\/script\u003e"); document.write('<script type="text/javascript">jQuery.noConflict();<\/script>'); }
Subscribe to:
Posts (Atom)