Last active
January 27, 2017 14:15
-
-
Save iKrevedko/fe456de6f26f57b5e8bf to your computer and use it in GitHub Desktop.
external delivery sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
MyExternal = (function() { | |
function MyExternal(deliveryId, fieldId) { | |
this.deliveryId = deliveryId; | |
this.fieldId = fieldId; | |
this.bind(); | |
} | |
MyExternal.prototype.input = function() { | |
return this.element || (this.element = $(this.inputSelector)); | |
}; | |
MyExternal.prototype.bind = function() { | |
return $(document).on('inited:insales:checkout:deliveries', (function(_this) { | |
return function(e, data) { | |
var myDelivery; | |
myDelivery = data.deliveries[_this.deliveryId]; | |
if (!myDelivery || !myDelivery.available) { | |
return; | |
} | |
_this.order = data.order; | |
_this.inputSelector = myDelivery.html_id; | |
try { | |
return _this.input().trigger('update:insales:delivery', $.extend(_this.calc(), { | |
is_external: true | |
})); | |
} catch (_error) { | |
_this.input().trigger('disable:insales:delivery'); | |
return _this.input().trigger('error:insales:delivery', 'Доставка не доступна'); | |
} | |
}; | |
})(this)); | |
}; | |
MyExternal.prototype.calc = function() { | |
if (this.order.shipping_address.city === 'Москва') { | |
return { | |
price: 100, | |
fields_values: [ | |
{ | |
field_id: this.fieldId, | |
value: '123' | |
} | |
] | |
}; | |
} | |
if (this.order.shipping_address.city === 'Санкт-Петербург') { | |
throw 'not available'; | |
} | |
return { | |
price: 150, | |
fields_values: [ | |
{ | |
field_id: this.fieldId, | |
value: '321' | |
} | |
] | |
}; | |
}; | |
return MyExternal | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment