Regarding SCS 2020.2, what would be the best appro...
# suitecommerce
c
Regarding SCS 2020.2, what would be the best approach (using the extensibility API) to add an item to the cart via a custom child view? Typically I would use the Live Order Model via LiveOrderModel.getInstance().addProducts(products), however I am concerned that this route will no longer be an option with future releases.
s
Cart component; addLine()
👍 1
c
@Steve Goldberg I updated my code to use the cart.addLine(). However, something that I noticed… If the item has already been added to the cart, the alert message from the example code displays twice and double the item quantity is added to the cart. It is as if the cart.addLine() fires twice.
s
OK, without seeing your code I can't really offer advice. Is it possible you're triggering it twice?
Perhaps if the item is already in the cart, you should just update the quantity?
c
@Steve Goldberg Wanted to provide the code for reference. However, I don’t think this extension is even possible to build due to both CartConfirmationView and LiveOrderModel are no longer allowed core modules. While I could build a custom modal view, I am not exactly sure how to get the model data for the item added to the cart without LiveOrderModel.getInstance().getLatestAddition().
Copy code
intAddOnItemsView.prototype.getEvents = function () {
	return {
		'click [data-action="add-to-cart"]': 'addToCart'
	}
}

intAddOnItemsView.prototype.addToCart = function addToCart(e){
	e.preventDefault();
	e.stopPropagation();
	
	var self = this,
		itemInternalId = this.$(e.target).data('id'),
		qty = this.$(e.target).parent().find('#quantity-' + itemInternalId ).val();
		
	this.cart.addLine({
		line: {
			quantity: qty,
			item: {
				internalid: itemInternalId
			}
		}
	}).then(

		function onSuccess() {
			var view = new CartConfirmationView({
				application: self.application,
				model: LiveOrderModel.getInstance().getLatestAddition()
			});
			view.showInModal();
		},
		function onError() {
			
			// console.log('onError', onError);
			
			var layout = self.application.getLayout();
			var output_message = Utils.translate(
				'Sorry, there is a problem with this Item and can not be purchased at this time. Please check back later.'
			);
			layout.showErrorInModal(output_message);
		}
	);
}
s
Yes, this is why we are planning to add getLatestAddition() to the API in the next release. Where are you adding this view? If it's as a child view on a PDP or PLP item cell, you may be able to access the item model with a contextDataRequest
c
PDP… Basically the extension adds a child view to the PDP, displaying suggested accessory items that can quickly be added to the cart.
s
Well you can use contextDataRequest to get the model data of the current PDP
c
Best example I could give for this customization is the PDP is of a phone and my extension adds a child view where 2 items are displayed. Let’s say one item is a screen protector and the other is a case and each has their own add to cart button. When the the shopper adds one of the accessory to the cart, we are trying to display a confirmation message that the accessory only has been added to the cart. Without being able to use LiveOrderModel.getInstance().getLatestAddition() or the confirmation view, things becomes tricky.
s
OK, without investing too much time on this myself the best I can say is that the method you want should be available in the next release
In the meantime, you can use the 'unsafe' method and change it after the new release
You might be able to use the internal ID of the item to query the cart's contents and pull the data from that
Eg,
Copy code
var id_to_find = 'item8046set1555'
container.getComponent('Cart').getLines().then(function (lines){
    lines.forEach(function(line) {
        if (line.internalid == id_to_find){
            console.log(line.item.itemid);
        }
    })
})
=> "Rishi Jacket-S-PE"
👍 1
c
@Steve Goldberg How do I get the “id_to_find” value?
s
Is it not the same as your
itemInternalId
value?
besides it should be returned by the addLine() method, or, at least, in the afterAddLine event