Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function ($) {
- 'use strict';
- var deliveryTab = $('#delivery'),
- formTab = $('#info'),
- payTab = $('#payment'),
- b2cTab = $('.js-b2c'),
- $zipcode = $('#id_zipcode'),
- cartTab = $('#cart');
- var $deliveryPrice = $('.delivery_price'),
- $productsPrice = $('#productsPrice'),
- $lastDeliveryPrice = deliveryTab.find('form input[name="last_delivery_price"]');
- $(document).bind('deliveryRecountCart', function () {
- withCartTotal(function (total) {
- $productsPrice.text(total);
- deliveryTab.find('#id_country').trigger('change');
- });
- });
- $zipcode.on('keyup', function () {
- load_b2cpl($(this).val());
- });
- function clear_delivery_b2c() {
- $('#id_code').html('');
- $('#b2c_loader .radio').remove();
- $('#b2c_loader .name').remove();
- }
- function load_b2cpl(val) {
- if($('#id_dtype').val() == 'b2cpl') {
- if(val.length > 6) {
- val = val.substr(0, 6);
- $(this).val(val);
- }
- if(val.length == 6) {
- check_delivery(val);
- }
- else {
- clear_delivery_b2c();
- }
- }
- else {
- clear_delivery_b2c();
- }
- }
- function check_delivery(zip) {
- if(zip != '' && zip.length == 6 && parseInt(zip)) {
- $('#b2c_loader').remove();
- if($('.b2c_hello_msg').length > 0) {
- $('.b2c_hello_msg').remove();
- }
- $('#id_code').prepend('<div id="b2c_loader" class=""><div class="radio"><img src="/static/core/img/icons/loader.gif" /></div></div>');
- clear_delivery_b2c();
- withCartWeight(function (weight) {
- getDeliveries(zip, weight);
- });
- }
- function getDeliveries(zip, weight) {
- //get deliveries
- $.ajax({
- url: Urls['cart:b2c_api'](),
- data: {
- func: 'tarif',
- type: '+post',
- zip: zip,
- weight: weight
- },
- success: function (data) {
- var price = 0;
- var b2c_options = '';
- if(data.flag_delivery) {
- add_delivery(data);
- $('[name="code"] option:first').trigger('change');
- $('[name="code"] option:first').attr('selected', 'selected');
- $('[name="code"] option:first').click();
- }
- else {
- //remove old records
- clear_delivery_b2c();
- //error message
- $('#b2c_loader').append('<div class="radio"></div><div class="name" style="color:red">B2CPL доставка не доступна по указаному почтовому индексу</div>');
- }
- }
- });
- }
- }
- function add_delivery(data) {
- var deliveries = '';
- var deliveriesGroup1 = [];
- var deliveriesGroup2 = [];
- var deliveriesGroup3 = [];
- var deliveriesList = [];
- data = replace_delimetr(data);
- var deliveryWays = data.delivery_ways;
- deliveriesGroup1 = deliveryWays.filter(function (item) {
- if(item.Код == 'курьер') {
- return item;
- }
- });
- deliveriesGroup2 = deliveryWays.filter(function (item) {
- if(item.Код.startsWith('пр')) {
- return item;
- }
- });
- deliveriesGroup3 = deliveryWays.filter(function (item) {
- if(item.Код.startsWith('пвз')) {
- return item;
- }
- });
- deliveriesList = deliveriesGroup1.concat(deliveriesGroup2, deliveriesGroup3);
- var deliveryName = '',
- deliveryCode = '',
- deliveryAddress = '',
- deliveryCost = '',
- selectedFirst = false,
- selectText = ' selected="selected" ';
- deliveriesList.forEach(function (deliveryWay) {
- deliveryName = deliveryWay.Наименование;
- deliveryCode = deliveryWay.Код;
- deliveryAddress = deliveryWay.Адрес;
- deliveryCost = Math.round(deliveryWay.Стоимость);
- if(deliveryName == 'пвз') {
- deliveryName = deliveryCode;
- }
- if(deliveryAddress != undefined && deliveryAddress != '') {
- deliveryAddress = ' Адрес: ' + deliveryAddress;
- }
- if(selectedFirst) {
- selectText = '';
- }
- deliveries += '<option' + selectText + ' value = "' + deliveryCode + '" data-price="' + deliveryCost + '">' + deliveryCost + ' руб. ' + deliveryName + deliveryAddress + '</option>';
- selectedFirst = true;
- });
- clear_delivery_b2c();
- $('[name="code"]').append(deliveries);
- }
- function updateTotalPrice(value) {
- var $totalPriceButtons = $('.cabinet_tabs').find('.totalPrice');
- $totalPriceButtons.each(function () {
- $(this).text(value);
- });
- }
- function withCartWeight(callback) {
- $.ajax({
- url: Urls['cart:get_weight'](),
- data: {
- dtype: $('#id_dtype').val()
- },
- success: function (data) {
- callback(data['weight']);
- }
- });
- }
- function withCartTotal(callback) {
- $.ajax({
- url: Urls['cart:get_total'](),
- success: function (data) {
- callback(data['total']);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment