Advertisement
Greenice

Javascript: Working with Yii2

Jan 31st, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var codeSent = false;
  2. var checkoutForm = (function ($) {
  3.     var form = $('#checkout-form'),
  4.         pub = {
  5.             init: function() {
  6.                 initNextStep();
  7.                 initBilling();
  8.                 initPayment();
  9.                 initDiscountCode();
  10.                 initSummary();
  11.                 initCheckout();
  12.             }
  13.         };
  14.     function initNextStep() {      
  15.         $(document).on('click', '.next-step', function(e) {
  16.             e.preventDefault();
  17.             var el = $(this),
  18.                 step = $(el).data('step'),
  19.                 validateOption = $(el).data('validate');
  20.             $('#loadmoreajaxloader').show();
  21.             setTimeout(function() {
  22.                 $.ajax({
  23.                     type : 'POST',
  24.                     url : '/order/checkout/setstep',
  25.                     async : false,
  26.                     cache : false,
  27.                     data : {step: step},
  28.                     success : function(response) {
  29.                         if (validateOption) {
  30.                             if (validate(validateOption)) {
  31.                                 var nextEl = $('.step-' + step).removeClass('disable'),
  32.                                     scroll = nextEl.data('scroll');
  33.                                 scrollContentTo(scroll ? $(scroll) : nextEl);
  34.                             }
  35.                         } else {
  36.                             var nextEl = $('.step-' + step).removeClass('disable'),
  37.                                 scroll = nextEl.data('scroll');
  38.                             scrollContentTo(scroll ? $(scroll) : nextEl);
  39.                         }
  40.                         sendCheckoutStatus(step);
  41.                         if (step == 2 && $('#Order_guest:checked').length > 0) {
  42.                             sentCheckoutOption(step, 'guest');
  43.                         }
  44.                         $('#loadmoreajaxloader').hide();
  45.                     }
  46.                 });
  47.             }, 10);
  48.         });
  49.     }
  50.     function initBilling() {
  51.         $(document).on('change', '#Order_shippingmethodoption_id', function() {
  52.             var el = $(this);
  53.             $('#loadmoreajaxloader').show();
  54.             setTimeout(function() {
  55.                 $.ajax({
  56.                     type : 'POST',
  57.                     url : '/order/checkout/GetShippingOptionPrice',
  58.                     async : false,
  59.                     cache : false,
  60.                     data : {
  61.                         option_id : el.val()
  62.                     },
  63.                     dataType: 'json',
  64.                     success : function(result) {
  65.                         $('.order-total-price').text(result.orderTotalLabel);
  66.                         $('.float-cart-btn strong').text(result.orderTotalLabel);
  67.                         $('.order-shipping-price').text(result.shippingPriceLabel);
  68.                         if (result.orderTotalWitoutSavigsLabel) {
  69.                             $('.order-total-price-without-savings').text(result.orderTotalWitoutSavigsLabel);
  70.                         } else {
  71.                             $('.order-total-price-without-savings').text('');
  72.                         }
  73.                         $('#loadmoreajaxloader').hide();
  74.                         reloadSummary();
  75.                     }
  76.                 });
  77.             }, 10);
  78.         }).on('change', '#OrderContact_3_country', function() {
  79.             var el = $(this);
  80.             $('#loadmoreajaxloader').show();
  81.             setTimeout(function() {
  82.                 $.ajax({
  83.                     type : 'POST',
  84.                     url : '/nomenclator/states',
  85.                     async : false,
  86.                     cache : false,
  87.                     data  : {
  88.                         country_name: el.val(),
  89.                         field_name: 'state',
  90.                         model_name: 'OrderContact[3]'
  91.                     },
  92.                     success : function(response) {
  93.                         $('#OrderContact_3_state').parent().html(response);
  94.                         $('#loadmoreajaxloader').hide();
  95.                     }
  96.                 });
  97.             }, 10);
  98.         });
  99.     }
  100.     function initPayment() {
  101.         $(document).on('click', 'input[name="Order[payment_option]"]', function() {
  102.             var el = $(this),
  103.                 payment = el.val();
  104.  
  105.             $('.container-payment').hide();
  106.  
  107.             if($("#container-" + payment).length > 0) {
  108.                 $("#container-" + payment).show();
  109.             }
  110.         }).on('click', '.btn-paypal', function(e) {
  111.             e.preventDefault();
  112.             var el = $(this);
  113.             $('.btn-paypal').removeClass('active');
  114.             el.addClass('active');
  115.             $('#PaypalPaymentMethod_type').val(el.data('type'));
  116.             $('#PaypalPaymentMethod_type_em_').text('');
  117.             getPaypal();
  118.             scrollContentTo($('#checkoutButtonSubmit'));
  119.         });
  120.     }
  121.     function initDiscountCode() {
  122.         $(document).on('change', '#promoCode', function(e) {
  123.             if (!codeSent) {
  124.                 var el = $(this);
  125.                 promoCode(el);
  126.             }
  127.         }).on('keydown', '#promoCode', function(e) {
  128.             if (e.which == 13 || e.keyCode == 13) {
  129.                 e.preventDefault();
  130.                 var el = $(this);
  131.                 promoCode(el);
  132.                 codeSent = true;
  133.                 setTimeout(function() {
  134.                     codeSent = false;
  135.                 }, 100);
  136.             }
  137.         });
  138.     }
  139.     function initSummary() {
  140.         $(document).on('click', '.drivers .driver-experience', function(e) {
  141.             e.preventDefault();
  142.             var el = $(this);
  143.             if (!el.hasClass('active')) {
  144.                 $('.experience-data').hide().removeClass('active');
  145.                 $('.driver-experience').removeClass('active');
  146.                 $('.driver-experience i').removeClass('fa-caret-down').addClass('fa-caret-right');
  147.                 el.addClass('active').find('i').removeClass('fa-caret-right').addClass('fa-caret-down');
  148.                 $('.experience-data-' + el.data('id')).slideDown().addClass('active');
  149.             }
  150.         });
  151.     }
  152.     function initCheckout() {
  153.         var error = $('.help-block.error:visible');
  154.         if (error.length) {
  155.             scrollContentTo(error);
  156.         }
  157.         $(document).on('click', '#checkoutButtonSubmit', function(e) {
  158.             if ($('input[name="Order[payment_option]"]:checked').val() == 'paypal') {
  159.                 e.preventDefault();
  160.                 if (validate('Payment')) {
  161.                     if (orderTerms === true || $('#Order_terms:checked').length > 0) {
  162.                         checkout.paypal.initAuthFlow();
  163.                     } else {
  164.                         $('#Order_terms_em_').text('You should agree to Terms of Sale').show();
  165.                     }
  166.                 }
  167.             }
  168.         }).on('submit', '#checkout-form', function(e) {
  169.             if (validate('Payment')) {
  170.                 $('#loadmoreajaxloader').show();
  171.             } else {
  172.                 e.preventDefault();
  173.             }
  174.         });
  175.     }
  176.     function promoCode(el) {
  177.         $('#loadmoreajaxloader').show();
  178.         setTimeout(function() {
  179.             $.ajax({
  180.                 type : 'POST',
  181.                 url : '/order/checkout/applyDiscount',
  182.                 async : false,
  183.                 cache : false,
  184.                 data  : {
  185.                     discount_code: el.val()
  186.                 },
  187.                 dataType: 'json',
  188.                 success : function(result) {
  189.                     $('#summary-data').replaceWith(result.html);
  190.                     getPaypal();
  191.                     if (result.message) {
  192.                         alert(result.message);
  193.                     }
  194.                 }
  195.             });
  196.         }, 10);
  197.     }
  198.     function getPaypal() {
  199.         $('#loadmoreajaxloader').show();
  200.         setTimeout(function() {
  201.             $.ajax({
  202.                 type : 'GET',
  203.                 url : '/order/checkout/ppcheckout',
  204.                 async : false,
  205.                 cache : false,
  206.                 dataType: 'json',
  207.                 success : function(result) {
  208.                     if ($('#paypal-checkout-script').length > 0) {
  209.                         $('#paypal-checkout-script').replaceWith(result.html);
  210.                     } else {
  211.                         $('body').append(result.html);
  212.                     }
  213.                     $('#loadmoreajaxloader').hide();
  214.                 }
  215.             });
  216.         }, 10);
  217.     }
  218.     function scrollContentTo(el) {
  219.         $('html, body').animate({
  220.             scrollTop: el.offset().top - (isMobile ? 60 : 150),
  221.         }, 500);
  222.     }
  223.     function validate(validateAction) {
  224.         var valid = false;
  225.         $.ajax({
  226.             type : 'POST',
  227.             url : '/order/checkout/validate' + validateAction,
  228.             async : false,
  229.             cache : false,
  230.             data : form.serialize(),
  231.             dataType: 'json',
  232.             success : function(result) {
  233.                 $('.help-block').hide();
  234.                 if(getResponseSize(result) > 0) {
  235.                     showAttributesErrors(result);
  236.                     valid = false;
  237.                 } else {
  238.                     valid = true;
  239.                 }
  240.                 $('#loadmoreajaxloader').hide();
  241.             }
  242.         });
  243.         return valid;
  244.     }
  245.     function getResponseSize(response){
  246.         var size = 0, key;
  247.         for (key in response) {
  248.             if (response.hasOwnProperty(key)) size++;
  249.         }
  250.         return size;
  251.     }
  252.     function showAttributesErrors(errors) {
  253.         for (var key in errors) {
  254.             $('#' + key + '_em_').text(errors[key][0]).addClass('error').show();
  255.         }
  256.     }
  257.     /* Tracking Enhance Ecommerce */
  258.     function sendCheckoutStatus(step, option) {
  259.         if (typeof ga == 'function') {
  260.             var cartStructures = JSON.parse(cartStructure);
  261.             products = cartStructures.products;
  262.             $.each(products, function (index, product) {
  263.                 ga('ec:addProduct', {
  264.                     'id': product.id,
  265.                     'name': product.name,
  266.                     'category': product.category,
  267.                     'brand': product.brand,
  268.                     'price': product.price,
  269.                     'quantity': product.quantity,
  270.                 });
  271.             });
  272.  
  273.             promo = cartStructures.promo;
  274.             if (promo) {
  275.                 ga('ec:addPromo', {
  276.                     'id': promo.id,
  277.                     'name': promo.name,
  278.                     'creative': promo.creative,
  279.                     'position': promo.position,
  280.                 });
  281.             }
  282.  
  283.             ga('ec:setAction', 'checkout', {step: step});
  284.             ga('send', {hitType: 'event', eventCategory: 'Ecommerce', eventAction: 'Checkout', eventLabel: undefined, eventValue: undefined});
  285.         }
  286.     }
  287.     function sentCheckoutOption(step, option) {
  288.         if (typeof ga == 'function') {
  289.             ga('ec:setAction', 'checkout_option', {
  290.                 'step': step,
  291.                 'option': option
  292.             });
  293.             ga('send', 'event', 'Checkout', 'Option');
  294.         }
  295.     }
  296.     function reloadSummary() {
  297.         if (!$('.float-cart-btn').hasClass('active')) {
  298.             $('.summary-panel').remove();
  299.             if (typeof summary != 'undefined') {
  300.                 summary.getBoxContent();
  301.             }
  302.         }
  303.     }
  304.     return pub;
  305. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement