Page 1 of 1

Not able to get the skuId and offer status in addtoCartItem

Posted: Fri Mar 07, 2014 9:32 am
by henry
Hi for getting the sku Id and Offer status in addToCartItem i am calling jquery method from my product.html page but its getting null. I have tried to do ajax call for sending the itemAttibute to my controller but still its not able to get the skuId and offerstatus in addToCartItem object cause of that adding item to cart is not happening .Please see my ajax call where i am mistaking.My requirement is to while placing the order now first user has to login then after that should move to shipping detail page with adding the product in cart.

this is my ajax call on cartOperation.js

Code: Select all

 $('body').on('click', 'input.redirectTologin', function() {
      
    var $button = $(this),
          $container = $button.closest('.product_container'),
          $form = $button.closest('form'),
          $options = $container.find('span.option-value'),
          $errorSpan = $container.find('span.error');
          $productOptionsSpan = $container.find('span.productOptionsSpan');
      if ($container.length == 0) {
          var myId = $button.parent().attr('id').substring('productOptions'.length);
          $container = $('.productActions' + myId).closest('.product_container');
          $form = $container.find('form');
          $options = $button.parent().find('span.option-value');
          $errorSpan = $button.parent().find('span.error');
          $productOptionsSpan = $container.find('span.productOptionsSpan');
      }
      
       var itemRequest = BLC.serializeObject($form),
          modalClick = $button.parents('.fancybox-inner').length > 0,
          wishlistAdd = $button.hasClass('addToWishlist');
         
          if (itemRequest.hasProductOptions == "true" && !modalClick) {
          $.fancybox.open($.extend({ href : '#productOptions' + itemRequest.productId}, fancyProductOptionsOptions));
      } else {
          $options.each(function(index, element) {
             var optionType = $(element).data('optiontype');
             var value;
             
             if ("TEXT" == optionType) {
                value = $(element).next().find('input').val();
             } else {
             var   value = $(element).text();
             }//need to add other types(date,long, etc) as needed
             $("#sizeVarience").val(value);
             itemRequest['itemAttributes[' + $(element).attr('id') + ']'] = value;
          });
         BLC.ajax({url: $form.attr('action'),
                    type: "POST",
                    dataType: "json",
                    data: itemRequest
                }, function(data, extraData) {}
                    alert("yeah its returning something after success");
            );
         }
      
    });


This is my product.html
snippet

Code: Select all

   <blc:form method="POST" th:action="@{/cart/anonymousOrder}" id="orderNow">
                     <div th:class="*{'productActions' + id}">
                        <div class="in_cart"
                           th:classappend="${!cart.containsSku(#object.defaultSku)}? ' hidden'">
                           <a class="fancycart fancybox.ajax big-button gray-button"
                              th:href="@{/cart}">In Cart!</a>
                        </div>
                        <div class="add_to_cart prod-details-addto-cart"
                           th:classappend="${cart.containsSku(#object.defaultSku)}? ' hidden'">
                           <input type="hidden" name="productId" th:value="*{id}" /> <input
                              type="hidden" name="quantity" value="1" />
                              <input type="hidden" id="sizeVarience" name="sizeVarience" />
                              <input type="submit" class="redirectTologin order-now-butt" th:value="#{order.now}"/>
                        </div>
                        <p class="error" th:if="${param.errorMessage}"
                           th:text="${param.errorMessage[0]}"></p>
                     </div>
                  </blc:form>


This is the modify method of quickOrder()

Code: Select all

@RequestMapping(value = "/anonymousOrder", method = RequestMethod.POST )
   public String quickOrder(HttpServletRequest request,
         HttpServletResponse response, Model model,
         @ModelAttribute("addToCartItem") AddToCartItem addToCartItem)
         throws IOException, PricingException, AddToCartException {
      Map<String, String> itemAttribute = addToCartItem.getItemAttributes();
      String size = request.getParameter("sizeVarience");
      HttpSession session = request.getSession();
      if (CustomerState.getCustomer().isAnonymous()) {
         //String referrer = request.getHeader("Referer");
      //   session.setAttribute("referrer", referrer);
         String referrer1 = "/cart/anonymousOrder";
         itemAttribute.put("OfferStatus", "NoOffer");
         itemAttribute.put("Size_" + addToCartItem.getProductId(), "M");
         addToCartItem.setItemAttributes(itemAttribute);
         session.setAttribute("addToCart", addToCartItem);
         session.setAttribute("url_prior_login", referrer1);
         // model.addAttribute("addToCartItem", addToCartItem);
         return "redirect:login";
      } else {
         AddToCartItem addToCartItem1 = (AddToCartItem) session
               .getAttribute("addToCart");
         session.removeAttribute("addToCart");
         //String url = (String) session.getAttribute("referrer");
      //   session.removeAttribute("referrer");
         this.addJson(request, response, model, addToCartItem1);
         RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
         redirectStrategy.sendRedirect(request, response, "/cart");
         return null;
      }

   }

Re: Not able to get the skuId and offer status in addtoCartItem

Posted: Sat Mar 08, 2014 4:04 pm
by RapidTransit
If I'm remembering correctly by default addToCartItem won't contain a skuId unless you explicitly add one, in your form or look it up via the attributes.

Re: Not able to get the skuId and offer status in addtoCartItem

Posted: Mon Mar 10, 2014 2:20 am
by henry
Hi RapidTransit in my jquery u see itemRequest variable, in this size and offerStatus are populated. i found it when i debug the jquery.As u see itemRequest is serialiazed form which is sending through ajax in my spring controller.In my method i am able to get productId and some of the attribute in addToCartItem but size and offerStauts specifically is not coming. Why is this so can u please see my controller method is there any mistakes i have done while sending it through ajax
This is the snippet in jquery where every is populated

Code: Select all

else {
          $options.each(function(index, element) {
             var optionType = $(element).data('optiontype');
             var value;
             
             if ("TEXT" == optionType) {
                value = $(element).next().find('input').val();
             } else {
             var   value = $(element).text();
             }//need to add other types(date,long, etc) as needed
             $("#sizeVarience").val(value);
             itemRequest['itemAttributes[' + $(element).attr('id') + ']'] = value;
          });

Re: Not able to get the skuId and offer status in addtoCartItem

Posted: Sun Mar 23, 2014 6:57 pm
by phillipuniverse
I would check the session object that you pull back to see if it's the same Java object as what you originally set maybe?

Actually, on second thought I guess that wouldn't be the case; the session attributes are probably serialized back and forth so you would always have a new java object.

I"m not really sure what to say about what you've got there.