How to use addLine and addLines methods from frontend extensibility API to add item to the cart

In the frontend extensibility API in the cart view there are many methods among which two are addLine and addLines.
In the extension for creating child view in the entry point file,
define(
	'JJ.AddRecentItemsToCart.AddRecentItemsToCart'
,   [

	'JJ.AddRecentItemsToCart.AddRecentItemsToCart.View',
	'underscore', 'ItemRelations.RelatedItem.View'
],
function (

	AddRecentItemsToCartView,
	_,
	ItemRelationsRelatedItemView
) {
	'use strict';
// var productLength;
	return  {
		mountToApp: function mountToApp (container)
		{
			var url = new URL(window.location.href);
			console.log('url3', url);
			var isCart = url.hash;
			console.log('isCart', isCart);
			if (isCart === '#cart') {
				_.extend(ItemRelationsRelatedItemView.prototype, {
					childViews: _.extend(ItemRelationsRelatedItemView.prototype.childViews, {
						'RecentViewToAddToCart.View': function(){
							console.log('child view created');
							console.log('entry this', this);
							var url = new URL(window.location.href);
							console.log('url23', url);
							return new AddRecentItemsToCartView({
								model: this.model,
								container : container,
								application: this.options.application
							})
						}
					})
				})
			}
		}
	};
});

For giving the functionality in the view file,
events: {
			'click #recently_viewed_add_to_cart_button_id': 'addRecentToCart',
		},
		addRecentToCart: function(e) {

			let hasRun = false;
			e.preventDefault();
            console.log("this", this)
            var cart = this.container.getComponent('Cart');
			console.log('cart', cart);
			let normalInterid = this.model.attributes.internalid;
			console.log('normalInterid',normalInterid);
			var url = new URL(window.location.href);
			console.log('url', url);
			var isCart = url.hash;
			console.log('param', isCart);

			if (isCart === '#cart') {
					if((this.model.attributes._matrixChilds.length) !== 0){
						let matrixInternalid = this.model.attributes.matrixchilditems_detail[1].internalid;
						console.log('matrixInternalid', matrixInternalid);
						let customizedQuantity = this.model.attributes.matrixchilditems_detail[1].onlinecustomerprice_detail.priceschedule[0].maximumquantity;
						console.log('customizedQuantity', customizedQuantity);

						if (matrixInternalid && customizedQuantity) {
							if(hasRun === false) {
								cart.addLine({
									line: {
										quantity: customizedQuantity,
										item: {
											internalid: matrixInternalid
										}
									}
								}).then(function(){
									alert(Utils.translate('Item added to cart successfully'))
									console.log('Item added to cart successfully');
									hasRun = true;
								}).fail(function(){
									alert(Utils.translate('failed'))
									hasRun = true;
								})
							}
							else{
								return false
							}
						} else if (!matrixInternalid && customizedQuantity) {
							if(hasRun === false) {
								cart.addLine({
									line: {
										quantity: customizedQuantity,
										item: {
											internalid: normalInterid
										}
									}
								}).then(function(){
									alert(Utils.translate('Item added to cart successfully'))
									console.log('Item added to cart successfully');
									hasRun = true;
								}).fail(function(){
									alert(Utils.translate('failed'))
									hasRun = true;
								})
							}
							else{
								return false
							}
						}
					}

					if((this.model.attributes._matrixChilds.length) === 0){
						if (hasRun === false) {
							console.log('inside normal if before addline');
							cart.addLine({
								line: {
									quantity: 1,
									item: {
										internalid: normalInterid
									}
								}
							}).then(function(){
								alert(Utils.translate('Item added to cart successfully'))
								console.log('Item added to cart successfully');
								hasRun = true;
								console.log('success hasRun', hasRun);
							}).fail(function(){
								alert(Utils.translate('failed'))
								hasRun = true;
							})
						}
						else{
							alert(Utils.translate('Inside Else'))
						}
					}
			}	
        }

Leave a comment

Your email address will not be published. Required fields are marked *