Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cart directives for e-commerce
'use strict';
angular.
module('core.cart').
directive('addtoCart', ['Cart','$timeout', function(Cart, $timeout){
return {
restrict: 'E',
transclude: true,
scope: {
id:'@',
quantity:'@',
loading:'@'
},
link: function(scope, element, attrs) {
var cartProducts = Cart.products;
if(scope.quantity > 1){
scope.minQuantity = parseInt(scope.quantity);
scope.q = parseInt(scope.quantity);
}else{
scope.minQuantity = 1;
scope.q = 1;
}
scope.loading = false;
scope.showTotal = false;
scope.addToCart = function () {
scope.loading = true;
scope.showTotal = true;
attrs.quantity = scope.q;
Cart.add(attrs).then(function (response) {
if(typeof response !== 'undefined'){
scope.loading = false;
scope.q = scope.minQuantity;
$timeout(function(){
scope.showTotal = false;
}, 5000);
Cart.getContent();
scope.qTotall = response.qty;
}
},
function (response) {}
);
};
if(angular.isDefined(cartProducts)){
angular.forEach(cartProducts, function(value, key){
if(value.id == scope.id){
scope.q = value.qty;
scope.qTotall = value.qty;
}
});
}
},
templateUrl: 'views/app/cart/cart-add.template.html',
};
}]).
directive('removeCart', ['Cart', function(Cart){
return {
restrict: 'EA',
transclude: true,
scope: {
rowId:'=rowId'
},
link: function(scope, element, attrs) {
scope.remove = function () {
Cart.remove(attrs.rowid).then(function (response) {
Cart.getContent();
},
function (response) {}
);
}
},
templateUrl: 'views/app/cart/cart-remove.template.html',
};
}]).
directive('cartHeader', ['Cart', function(Cart){
return {
restrict: 'E',
transclude: true,
controller: ['$scope', 'Cart', function($scope, Cart) {
this.cart = Cart;
}],
link: function(scope, element, attrs, controllers) {},
templateUrl: 'views/app/cart/cart-header.template.html',
controllerAs:'CartHeader'
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.