$(document).ready(function(){
    if ($('body').hasClass('success-order-loaded')) {
        hash = $('.order-summary').data('order-hash');

        $.ajax({
            'url': '/cart/order/getInfoByHash',
            'type': 'GET',
            'data': { 'hash' : hash },
            'dataType': 'json',
            'success': function(jsondata) {
                products = [];

                // Отправка данных в электронную коммерцию
                $.each(jsondata.order.products, function(index, item){
                    products.push({
                        "id" : item.id,
                        "name" : item.name,
                        "category" : item.category,
                        "price" : item.price,
                        "quantity" : item.quantity
                    });
                });

                window.dataLayer.push({
                    "ecommerce": {
                        "currencyCode": "RUB",
                        "purchase" : {
                            "actionField": {
                                "id" : jsondata.order.id,
                                //"coupon" : jsondata.order.promocode,
                                'revenue' : jsondata.order.totalPrice,
                            },
                            "products" : products
                        }
                    }
                });
            }
        });
    }
});