function _log(msg){ console.log(msg); } var IqUserShopCart = { btn_add_free: '.btn-add-free-product', input_free: '.switcher-input', btn_shop_add: '.btn-add-product-shop', btn_shop_minus: '.btn-minus-product-shop', btn_remove: '.remove-product-shop', btn_clear: '#clear-products-basket', cart_input: '.cart-input-event-onchange', remove_item: '.remove_item_form_cart', url: null, action: null, cart_holder: '#user_cart_holder', invoice_holder: '#invoice_details_holder', checkout_holder: '#user_checkout_holder', show_products_holder: '#show_products_holder', badge_basket_indicator : '#badge_basket_indicator', free_product_id: null, shipping_option: null, show_products_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)); }); $(_self.btn_shop_minus).on('click', function(event){ event.preventDefault(); _self.minusShopProduct($(this)); }); $('input[name=switchers_shipping]').on('change', function(event){ event.preventDefault(); _self.switchShipping($(this)); }); $('select[name=show_products_filter]').on('change', function(event){ event.preventDefault(); _self.switchShowProducts($(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); }); $('#shipping_state').on('change', function(){ if ($('#shipping_address_switch').is(':checked')) { _self.changeStateShipping($(this).val()); } }); $('#billing_state').on('change', function(){ if (!$('#shipping_address_switch').is(':checked')) { _self.changeStateShipping($(this).val()); } }); }, changeStateShipping: function(_state_id){ var _self = this; _self.performRequest({shipping_country_id: _state_id, action: 'change-state-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); } }, minusShopProduct: function(_ele){ var _self = this; if(_ele.data('product_id')){ _self.performRequest({product_id: _ele.data('product_id'), action: 'minus-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); }, switchShowProducts: function(_ele){ var _self = this; _self.show_products_option = _ele.val(); _self.performRequest({show_products_option: _self.show_products_option, action: 'switch-show_products'}) .done(_self.refreshShowProducts); }, refreshButtonDisabled: function (data){ if(data.response.qty >= 100){ $('#cart_btn_plus_'+data.response.product_id).prop("disabled",true); }else{ $('#cart_btn_plus_'+data.response.product_id).prop("disabled",false); } if(data.response.qty <= 0){ $('#cart_btn_minus_'+data.response.product_id).prop("disabled",true); }else{ $('#cart_btn_minus_'+data.response.product_id).prop("disabled",false); } }, refreshShowProducts: function (data){ var _self = IqUserShopCart; if(data.shop_products_view){ $(_self.show_products_holder).html(data.shop_products_view); } $(_self.btn_shop_add).on('click', function(event){ event.preventDefault(); _self.addShopProduct($(this)); }); }, 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 >= 300){ return 300; } return number; }, refreshItemsAndView: function (data){ var _self = IqUserShopCart; _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' || data.response.action == 'minus-shop-product'){ var qty = data.response.qty > 0 ? "x"+data.response.qty : 0; $('#badge_cart_indicator_'+data.response.product_id).html(' '+qty); //on off buttons _self.refreshButtonDisabled(data); } 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); } if(data.basketqty){ $(_self.badge_basket_indicator).html(data.basketqty); } if(data.response.shipping_price_formated){ $('#shipping_price_holder').html(data.response.shipping_price_formated); } _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!"); }); } };