Last active
February 22, 2018 13:11
-
-
Save rafaelpatro/788e4051abe83f6119eb2a4b1d5800a6 to your computer and use it in GitHub Desktop.
Add ajax compatibility to Magento action buttoms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add XHR compatibility to Magento action buttoms | |
* | |
* Author: | |
* Rafael Patro <[email protected]> | |
* | |
* Requirements: | |
* DOMParser HTML fixes | |
* jQuery Growl library | |
* | |
* Intallation: | |
* Install requirements | |
* Add a CMS Static Block applying the entire script below. | |
* Add a Widget to product list pages. | |
* | |
* License: | |
* GNU General Public License <http://www.gnu.org/licenses/>. | |
*/ | |
;(function($) { | |
XHRCart = function() { | |
this.doc = null; | |
this.response = null; | |
this.selectors = ['.account-cart-wrapper']; | |
this.messageTypes = ['error','warning','notice','success']; | |
this.progressLoader = '<img src="/skin/frontend/rwd/default/images/opc-ajax-loader.gif" alt="progress" class="xhr-loader" />'; | |
this.callback = function(){}; | |
}; | |
XHRCart.prototype = { | |
success : function(message) { | |
$.growl.notice({'message':message}); | |
}, | |
notice : function(message) { | |
$.growl.notice({'message':message}); | |
}, | |
error : function(message) { | |
$.growl.error({'message':message}); | |
}, | |
warning : function(message) { | |
$.growl.warning({'message':message}); | |
}, | |
clear : function() { | |
$('.xhr-loader').remove(); | |
}, | |
progress : function() { | |
console.log('XHRCart.progress'); | |
var _this = this; | |
this.selectors.each(function(index){ | |
$(index).html(_this.progressLoader); | |
}); | |
}, | |
loadBlocks : function() { | |
console.log('XHRCart.loadBlocks'); | |
var _this = this; | |
this.selectors.each(function(selector){ | |
$(selector).html($(_this.doc).find(selector).html()); | |
}); | |
}, | |
loadMessages : function() { | |
console.log('XHRCart.loadMessages'); | |
if (typeof $.growl == 'function') { | |
var _this = this; | |
var message; | |
var messages; | |
if (messages = $(_this.doc).find('.messages')[0]) { | |
this.messageTypes.each(function(type){ | |
if (message = $(_this.doc).find('.messages .' + type + '-msg')[0]) { | |
var notify = new Function('elem', 'msg', 'return elem.' + type + '(msg);'); | |
notify(_this, message.textContent); | |
} | |
}); | |
if (typeof message == 'undefined') { | |
this.notice(messages.textContent); | |
} | |
} else if (typeof coShippingMethodForm != 'undefined') { | |
this.notice('Concluído!'); | |
} else { | |
this.warning('Para calcular o frete, acesse o carrinho no topo do site.'); | |
} | |
} | |
}, | |
loadParser : function() { | |
console.log('XHRCart.loadParser'); | |
var parser = new DOMParser(); | |
this.doc = parser.parseFromString(this.response, "text/html"); | |
}, | |
loadCallback : function() { | |
console.log('XHRCart.loadCallback'); | |
if (typeof this.callback == 'function') { | |
this.callback(this); | |
} | |
}, | |
load : function(response) { | |
console.log('XHRCart.load'); | |
this.response = response; | |
this.loadParser(); | |
this.loadBlocks(); | |
this.loadMessages(); | |
this.clear(); | |
this.loadCallback(); | |
return true; | |
}, | |
send : function(elem) { | |
console.log('XHRCart.send'); | |
var _this = this; | |
var param = null; | |
var url = elem; | |
if (typeof url != 'string') { | |
url = $(elem).attr('href'); | |
if (typeof url != 'string') { | |
var formObj = $(elem).closest('form'); | |
url = formObj.attr('action'); | |
param = formObj.serialize(); | |
} | |
} | |
$.ajax({ | |
url: url.replace(/http:/, location.protocol), | |
method: 'POST', | |
data: param, | |
beforeSend: function() { | |
_this.progress(); | |
}, | |
success: function(response) { | |
_this.load(response); | |
} | |
}); | |
} | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Carrinho de Compras
Exemplo de implementação ajax nos botões do carrinho, no tema RWD.
Opcional: Para exibir mensagens de notificação (sucesso, erro, etc), basta incluir antes a biblioteca jquery.growl.
Criar um bloco estático, com o conteúdo a seguir. E criar um widget, posicionando o bloco após o conteúdo (abaixo da página).