1function YmEc(options){const self=this;let products={}
2;let cartItems={}
3;let detailProductId=null;let currency=null;let hasActiveVariation=false;let sentDetails=[];let dataLayerName=options?options:"dataLayer";window[dataLayerName]=window[dataLayerName]||[];this.addData=function(data){if(!data){return}
4if(data.products){this.registerProducts(data.products)}
5if(data.cartItems){this.registerCartItems(data.cartItems)}
6if(data.currency){this.setCurrency(data.currency)}
7if(data.detailProductId){this.setDetailProductId(data.detailProductId)}
8if(data.hasActiveVariation){hasActiveVariation=true}
9if(data.actions){for(var index in data.actions){var action=data.actions[index];this.send(action.type,action.data)}
10}
11if(data.purchase){this.sendPurchase(data.purchase.actionField,data.purchase.products)}
12}
13;this.registerCartItem=function(hash,data){cartItems[hash]=data}
14;this.registerProduct=function(id,data){if(typeof data==="string"){console.log(data);try{data=JSON.parse(data)}
15catch(e){console.log("При регистрации товара произошла ошибка - неверная JSON строка");return}
16}
17products[id]=data}
18;this.registerProducts=function(products){for(var productId in products){if(!products.hasOwnProperty(productId)){continue}
19self.registerProduct(productId,products[productId])}
20}
21;this.registerCartItems=function(cartItems){for(var hash in cartItems){if(!cartItems.hasOwnProperty(hash)){continue}
22self.registerCartItem(hash,cartItems[hash])}
23}
24;this.isDetail=function(){return!!detailProductId}
25;this.hasActiveVariation=function(){return hasActiveVariation}
26;this.setDetailProductId=function(productId){detailProductId=productId}
27;this.setCurrency=function(currencyCode){currency=currencyCode}
28;this.getCurrency=function(){return currency}
29;this.getDetailProductId=function(){return detailProductId}
30;this.getProduct=function(id){return products[id]||null}
31;this.getProductByHash=function(hash){if(!cartItems.hasOwnProperty(hash)){return null}
32const productId=cartItems[hash].id;return products[productId]||null}
33;this.getProducts=function(){return products}
34;this.getCartItems=function(){return cartItems}
35;this.clearCartItems=function(){cartItems={}
36}
37;this.clearProducts=function(){products={}
38}
39;this.updateCartItems=function(newCartItems){for(let hash in cartItems){const cartItem=cartItems[hash];if(typeof newCartItems[hash]==="undefined"){this.send("remove",hash,{quantity:cartItem.quantity}
40);delete cartItems[hash]}
...
</html>