define( [ 'jquery', 'Magento_Ui/js/modal/confirm', 'Magento_Customer/js/customer-data', 'jquery-ui-modules/widget', 'Magento_Catalog/js/product/view/product-ids-resolver', 'mage/url', 'Magento_Ui/js/modal/alert', 'ko', ], function ($, confirm, customerData, widget, idsResolver, urlBuilder, alertModal, ko, _ ) { var mixin = { getFormattedPrice: function (price) { var value = Intl.NumberFormat.call("de-DE", {style: "currency", currency: "CLP"}).format(price); const regex = /,/i; var newvalue = value.replace(regex, '.'); return '$'+newvalue; }, minQty: function (item_id, parent){ let input = $(`#cart-item-${item_id}-qty`); if(input.val()>1){ input.val(parseInt(input.val())-1); } if (input.val() <= 0) { input.val(1); } parent._ajax(window.checkout.updateItemQtyUrl, { 'item_id': item_id, 'item_qty': input.val() }); }, maxQty: function (item_id, parent){ let input = $(`#cart-item-${item_id}-qty`); input.val(parseInt(input.val())+1); parent._ajax(window.checkout.updateItemQtyUrl, { 'item_id': item_id, 'item_qty': input.val() }); }, maxQtyT: function (item_id, parent){ let input = $(`#cart-item-${item_id}-qty`); if( input.val() <= 0){ input.val(1); } parent._ajax(window.checkout.updateItemQtyUrl, { 'item_id': item_id, 'item_qty': input.val() }); }, goToCheckout: function() { location.href = window.checkout.checkoutUrl; }, goToCartPAge: function() { location.href = window.checkout.shoppingCartUrl; }, _remove: function(item_id, parent){ confirm({ content: $.mage.__('Are you sure you would like to remove this item from the shopping cart?'), actions: { /** @inheritdoc */ confirm: function () { parent._ajax(window.checkout.removeItemUrl, { 'item_id': item_id }); customerData.invalidate(['cart']); }, /** @inheritdoc */ always: function (e) { e.stopImmediatePropagation(); } } }); }, getProductIds: function(){ let data = this.getCartItems(); let ids = ''; data.forEach(item =>{ if(ids === '') ids += item.product_id; else ids += ',' + item.product_id; }) return ids; }, reloadRR: function (){ try { if(retailrocket !== undefined) { retailrocket.markup.render(); } } catch (e){ console.error(e); return false; } return true; }, _ajax: function (url,data) { $.extend(data, { 'form_key': $.mage.cookies.get('form_key') }); $.ajax({ url: url, data: data, type: 'POST', dataType: 'json', }).done(function (response) { let msg; if(response.hasOwnProperty('error_message')){ msg = response['error_message']; if (msg) { alertModal({ title:'TU CARRO', content: msg }); } } }); }, addToCart: function (form) { productId = form.id; var data = {product: productId}; var sections = ['cart']; $.ajax({ url : urlBuilder.build('minicart/cart/add'), dataType : 'json', type : 'POST', data: data, /** @inheritdoc */ beforeSend: function () { // $('[data-block=\'minicart\']').trigger('processStart'); console.log('Minicart JS addToCart beforeSend'); }, success : function(res) { console.log('Minicart JS addToCart success'); // $('[data-block=\'minicart\']').trigger('processStop'); customerData.invalidate(sections); customerData.reload(sections, true); }, error : function(res) { console.log('Minicart JS addToCart error'); $(document).trigger('ajax:addToCart:error', { 'productIds': productId, 'form': form, 'response': res }); } }); }, getDiscountRate: function (regular,final) { var dato =1; if(regular == final){ dato =0; } return dato; }, getDiscount: function (regular,final) { var Percentage = '-'+(((regular-final)/regular)*100).toFixed(0)+'%'; return Percentage; }, getCartItems: function () { var items = this.getCartParamUnsanitizedHtml('items') || []; items = items.slice(parseInt(-this.maxItemsToDisplay, 10)); //console.log('showcart CartItems items = '+JSON.stringify(items)); var ItemS = []; $.each(items, function (index, value) { if(value['message']){ ItemS.push({ product_name : value['product_name'], item_id : value['item_id'] }); } }) $.mage.cookies.set('CartItems',JSON.stringify(ItemS)); return items; } } $(".showcart").on("click",function(e){ $('.loading-mask').css('visibility', 'hidden'); }); return function (target) { return target.extend(mixin) } } );