Add to cart functionality update for PLP page

PAPL or PPLD requirement

By the SCA, the quantity will added to existing quantity when we try to add the quantity from PLP page. But there is a functionality implemented for add to cart button in PLP page. The button name will be Update if the item is already cart.


Requirement : The update button is for update of quantity not an addition of quantity. For example, if a customer adds x3 of an item to the cart and then updates the quantity to x2. The cart should have a total of x2 not x5.
So in order to achieve the requirement we have override the functionality of add to cart button

addToCart: function addToCart(e) {

							e.preventDefault();

							var self = this
								, cart_promise;

							if (!this.model.areAttributesValid(['options', 'quantity'], self.getAddToCartValidators())) {
								return;
							}

							if (!this.model.isNew() && this.model.get('source') === 'cart') {
								cart_promise = this.cart.updateProduct(this.model);
								cart_promise.done(function () {
									self.options.application.getLayout().closeModal();
								});
							}
							else {

								console.log('addToCart', self.cart.get('lines').models[0])
								var line = self._findLine(self.cart.get('lines').models[0].get('internalid'));

								console.log('addToCart', line.set('quantity', 2))


								cart_promise = self.cart
									.updateLine(line)
									.done(function () {

									})
									.fail(function (error) {
										var line = LiveOrderLineModel.createFromProduct(self.model);

									})
								cart_promise = self.cart.addLine(line);
								CartConfirmationHelpers.showCartConfirmation(cart_promise, line, self.options.application);

							}

							cart_promise.fail(function (jqXhr) {
								var error_details = null;
								try {
									var response = JSON.parse(jqXhr.responseText);
									error_details = response.errorDetails;
								}
								finally {
									if (error_details && error_details.status === 'LINE_ROLLBACK') {
										self.model.set('internalid', error_details.newLineId);
									}
								}

							});

							this.disableElementsOnPromise(cart_promise, e.target);
							return false;
						},
						_findLine: function _findLine(internal_id) {
							const line = this.cart.get('lines').models.find(function (cart_line) {
								console.log('cart_line', cart_line)
								console.log('internal_id', internal_id)
								return cart_line.get('internalid') === internal_id;
							});

							//!line && this._reportError('INVALID ID', 'Line ' + internal_id + ' not found');
							console.log('cart_lineline', line)
							return line;
						},

Leave a comment

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