164 lines
No EOL
5.4 KiB
JavaScript
164 lines
No EOL
5.4 KiB
JavaScript
|
|
function _log(msg){
|
|
console.log(msg);
|
|
}
|
|
|
|
var IqPromotionShopCart = {
|
|
btn_add_free: '.btn-add-free-product',
|
|
input_free: '.switcher-input',
|
|
btn_shop_add: '.btn-add-product-shop',
|
|
//btn_add: '.add-product-shop',
|
|
btn_remove: '.remove-product-shop',
|
|
//input_event: '.input-event-promotion-onchange',
|
|
btn_clear: '#clear-products-basket',
|
|
cart_input: '.cart-input-event-onchange',
|
|
remove_item: '.remove_item_form_cart',
|
|
|
|
url: null,
|
|
action: null,
|
|
cart_holder: '#promotion_cart_holder',
|
|
invoice_holder: '#invoice_details_holder',
|
|
checkout_holder: '#promotion_checkout_holder',
|
|
|
|
free_product_id: null,
|
|
shipping_option: null,
|
|
|
|
|
|
init: function () {
|
|
var _self = this;
|
|
_self.url = $('input[name=load_url]').val();
|
|
_self.initElements();
|
|
return _self;
|
|
},
|
|
initElements: function (){
|
|
var _self = this;
|
|
//_log('init');
|
|
$(_self.btn_add_free).on('click', function(event) {
|
|
event.preventDefault();
|
|
$(this).find(_self.input_free).prop('checked', true);
|
|
_self.switchFreeProduct($(this).find(_self.input_free));
|
|
});
|
|
$(_self.btn_shop_add).on('click', function(event){
|
|
event.preventDefault();
|
|
_self.addShopProduct($(this));
|
|
});
|
|
|
|
$('input[name=switchers_shipping]').on('change', function(event){
|
|
event.preventDefault();
|
|
_self.switchShipping($(this));
|
|
});
|
|
_self.showInit();
|
|
|
|
|
|
/*$_self.update_poduct_price();*/
|
|
|
|
},
|
|
showInit: function (){
|
|
var _self = this;
|
|
$(_self.btn_clear).on('click', function (event){
|
|
event.preventDefault();
|
|
_self.performRequest({action: 'clear-cart'})
|
|
.done(_self.refreshItemsAndView)
|
|
});
|
|
$(_self.cart_input).on('change', function(event){
|
|
event.preventDefault();
|
|
_self.updateInputCart($(this));
|
|
});
|
|
$(_self.remove_item).on('click', function(event){
|
|
event.preventDefault();
|
|
_self.performRequest({product_id: $(this).data('product-id'), qty: 0, action: 'remove-shop-product'})
|
|
.done(_self.refreshItemsAndView);
|
|
});
|
|
},
|
|
switchShipping: function(_ele){
|
|
var _self = this;
|
|
_self.shipping_option = _ele.val();
|
|
_self.performRequest({shipping_option: _self.shipping_option, action: 'switch-shipping'})
|
|
.done(_self.refreshItemsAndView);
|
|
},
|
|
switchFreeProduct: function(_ele){
|
|
var _self = this;
|
|
if(_ele.prop('checked')){
|
|
if(_self.free_product_id != _ele.val()){
|
|
_self.free_product_id = _ele.val();
|
|
_self.performRequest({free_product_id: _self.free_product_id, product_id: _ele.data('product_id'), action: 'switch-free-product'})
|
|
.done(_self.refreshItemsAndView);
|
|
}
|
|
}
|
|
},
|
|
addShopProduct: function(_ele){
|
|
var _self = this;
|
|
if(_ele.data('product_id')){
|
|
_self.performRequest({product_id: _ele.data('product_id'), action: 'add-shop-product'})
|
|
.done(_self.refreshItemsAndView);
|
|
}
|
|
},
|
|
|
|
updateInputCart: function (_ele){
|
|
var _self = this;
|
|
var qty = parseInt(_ele.val());
|
|
qty = _self.checkNumber(qty);
|
|
_ele.val(qty);
|
|
_self.performRequest({product_id: _ele.data('product_id'), qty: qty, action: 'update-shop-product'})
|
|
.done(_self.refreshItemsAndView);
|
|
},
|
|
checkNumber : function(number){
|
|
if(number < 0 || isNaN(number)){
|
|
return 0;
|
|
}
|
|
if(number >= 100){
|
|
return 100;
|
|
}
|
|
return number;
|
|
},
|
|
refreshItemsAndView: function (data){
|
|
var _self = IqPromotionShopCart;
|
|
//_log(data);
|
|
if(data.response.action == 'clear-cart'){
|
|
location.reload();
|
|
}
|
|
if(data.response.action == 'add-shop-product' || data.response.action == 'update-shop-product' || data.response.action == 'remove-shop-product'){
|
|
var qty = data.response.qty > 0 ? "x"+data.response.qty : 0;
|
|
$('#badge_cart_indicator_'+data.response.product_id).html(qty);
|
|
}
|
|
if(data.cart){
|
|
$(_self.cart_holder).html(data.cart);
|
|
}
|
|
if(data.invoice){
|
|
$(_self.invoice_holder).html(data.invoice);
|
|
}
|
|
if(data.checkout){
|
|
$(_self.checkout_holder).html(data.checkout);
|
|
}
|
|
_self.showInit();
|
|
},
|
|
performRequest : function(data) {
|
|
var _self = this;
|
|
var url = _self.url;
|
|
data.perform = true;
|
|
_log(data);
|
|
// _log(url);
|
|
return $.ajax({
|
|
url: url,
|
|
data: data,
|
|
type: "POST",
|
|
dataType: "json",
|
|
cache: false,
|
|
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
|
|
encode: true,
|
|
headers: {
|
|
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
|
|
}
|
|
})
|
|
.done(function (data) {
|
|
_log('performRequest');
|
|
_log(data);
|
|
}).fail(function (jqXHR, textStatus, errorThrown) {
|
|
console.log(jqXHR);
|
|
console.log(jqXHR.responseText);
|
|
console.log(textStatus);
|
|
console.log(errorThrown);
|
|
console.log("Sorry, there was a problem!");
|
|
});
|
|
}
|
|
}; |