1

I have a product details page which displays 1 product, there's an Add to Cart button for user to click. When user click on Add to Cart, the cart will be displayed at the bottom on the same page. I've done the steps according to this tutorial(page 63) provided by SF.

However, I'm not sure how to store the session/cookie such that when user navigate to cart page it will store the user previous selection. I've tried using PageReference to redirect user to cart page, but the cart will be empty.

Product Details VF Page

<apex:pageBlockSection>
     <apex:commandButton action="{!addToCart}" value="Add to Cart" rerender="shopping_cart"/> </apex:pageBlockSection>
         </apex:pageblock>

      <apex:pageBlock title="Your Cart" id="shopping_cart">
  <apex:outputText value="{!cartContents}" escape="false"/> </apex:pageBlock>

Cart VF Page

<apex:pageBlock title="Your Cart" id="shopping_cart">
 <apex:outputText value="{!cartContents}" escape="false"/> </apex:pageBlock>

MyController

Map< String, Integer> shoppingCart = new Map<String, Integer>(); 

public PageReference addToCart() {
    PageReference pageRef = new PageReference('/apex/cart');
    for(DisplayProducts p : products) {
        if(0 < p.qtyToBuy) {
            shoppingCart.put(p.productid, p.qtyToBuy);
        }

    }
    pageRef.setRedirect(true);
     return pageRef;
}

public String getCartContents() {
    if(0 == shoppingCart.size()) {
        return '(cart is empty)';
    }
    String msg = '<ul>\n';
    for(String id : shoppingCart.keyset()) {
    msg += '<li>';
        msg +=  id + ' (' + shoppingCart.get(id) + ')';
        msg += '</li>\n';
    }
    msg += '</ul>';
    return msg;
}

I'm new to SF, any sorta help will be very useful. Thank you!

  • Hi could you please share your solution i have exact problem. – user37402 Sep 15 '16 at 19:33
  • Each redirect instantiates a new controller instance; hence, you either save cart state in a) Cookie or b) in URL params – cropredy Sep 16 '16 at 4:22
2

You have 2 options here that I can think of.

1) You can create a Wizard. To do this you essentially use multiple VF pages that use the same controller and move between pages with the page Reference. https://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_wizard.htm

2) Before leaving the page, save whatever information you need in an object. That way when you go back to that page you can reload that object. So like a Cart object might lookup to UserId and then you query for the associated record on load of the page.

  • hi @dphil, the wizard won't work for my case as if user were to browse on other pages, the session won't be save. Furthermore, the URL from the product details page to cart page won't changed if I were to implement wizard. – taradyn Dec 11 '14 at 15:41
  • @taradyn 3 questions, Is there something special about your case that would cause the session to not carry over in a Wizard? Why would the URL from detail to cart page now change? Why would it matter if it didn't? – dphil Dec 11 '14 at 15:48
  • it's a shopping cart feature. I would like user to continue browsing thru the website and add items. user should be able to see their items on cart page. If I were to implement wizard, user is unable to browse to other pages and the data would not be stored in a cart page. Is there any method in SF that allow us to store user session? Thanks! – taradyn Dec 11 '14 at 16:06
  • @taradyn I c. Well option 2 is your best bet. (Wizards actually allow navigation between pages. The only caveat is all the pages must use the same controller so if they go to do other things on the website is when it's not feasible.) – dphil Dec 11 '14 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.