Page 1 of 1

Add multiple items to cart with 1 POST

Posted: Tue Jun 04, 2013 7:37 am
by jasoneasterday
Please advise,
We are wanting to add multiple items (different items, NOT same item with quantity) to the cart with one call.
Currently with 1 item,

Code: Select all

    @ActionMapping("add")
    public void add(ActionRequest request, ActionResponse response, Model model,
            @ModelAttribute("addToCartItem") AddToCartItem addToCartItem) throws IOException, PricingException, AddToCartException {
       System.out.println("CartPortletController.add()");
        super.add(PortalUtil.getHttpServletRequest(request), PortalUtil.getHttpServletResponse(response), model, addToCartItem);
    }

is invoked using a

Code: Select all

<form name="addItem" method="POST" liferay:action="action='add', lifecycle='ACTION_PHASE'">
...

New to Spring, but I have been unsuccessful adding multiple items using below

Code: Select all

@ActionMapping("action=addMultipleItems")
    public void addMultipleItems(ActionRequest request, ActionResponse response, Model model, ArrayList<AddToCartItem> addToCartItems)

Not sure how this looks in a form.

What I'm considering is

Code: Select all

@ActionMapping("action=addMultipleItems")
    public void addMultipleItems(ActionRequest request, ActionResponse response, Model model, String[] addToCartItems) {

Then build the AddToCartItem somehow, then call super.add multiple times?

If there is a way to make one call to the BroadleafCartController to add multiple items, please let me know.

Jason

Re: Add multiple items to cart with 1 POST

Posted: Tue Jun 04, 2013 9:06 am
by RapidTransit
Check out: http://bitbucket.org/rapidtransit440/br ... &at=master

I ran into a problem where I wanted to use a Post request you can see where I changed how it adds to the cart and get a return of what was added via Pines/jGrowl

This is also useful if you want to do a post request with out a form element:
http://bitbucket.org/rapidtransit440/br ... ?at=master

http://bitbucket.org/rapidtransit440/br ... ?at=master

Example Usage:

Code: Select all

<script th:inline="text">

/* <csrf:csrf />  <![CDATA[*/
    $('.add-to-cart').on('click',function(event){
        var selId = '#' + event.target.id;
        var attr = $(selId).attr('attr');
        var itemAttr = 'itemAttributes['+attr+']';

        var objLit = {};
        objLit["productId"] = $(selId).data('id');
        objLit["quantity"] = $('#qty'+event.target.id).val();
        objLit["hasProductOptions"] = "true";
        objLit[itemAttr]= $(selId).attr('attrVal');
        objLit["csrfToken"]= "[[${csrfToken}]]";
        $.ajax({
            url: 'cart/add',
            type: 'POST',
            data: objLit,
            success: function(data){
                    $.jGrowl('<strong>Added to Cart</strong><p>'+data.quantity+ ' x ' + data.name + '</p>');
                $('.headerCartItemsCount').text(data.updateCart)
            }
        })
    });
/*]]>*/
</script>

Re: Add multiple items to cart with 1 POST

Posted: Tue Jun 04, 2013 9:56 am
by jasoneasterday
thanks for your reply.
I don't have access to view your bitbucket links :(
Also, one thing I'm trying to do is not use thymeleaf on this because we are using forms within a velocity template (liferay).
Jason

Re: Add multiple items to cart with 1 POST

Posted: Tue Jun 04, 2013 10:38 am
by RapidTransit
Just made it public, also you can adapt the CSRF protection if you happen to run into trouble using post requests