0

I am developing a website in Salesforce, which is going to be an online-shop. I have pages of products divided by categories. Product object is custom, it has fields Name, Price__c, Quantity__c, Description__c. I am trying to create Shopping cart page so that user could by click on the Add to cart button add products to his shopping cart. Products on the Shopping cart page should be displayed as a list with button buy near every item.
Unfortunately, every time I click Add to cart button list of products on the Shopping cart page is still empty. I would be grateful for any kind of help!

Here is my code for Shopping cart page:

    <div class="main">
    <apex:form id="cartForm">
        <apex:outputPanel id="productsDetail">
            <apex:pageBlock title="Your Shopping Cart" id="cartBlock">
                <apex:pageBlockTable value="{!shoppingCart}" var="product">
                    <apex:column >
                        <apex:commandLink value="{!product.Name}" >
                            <apex:param name="id" value="{!product.Id}"/>
                        </apex:commandLink>
                        <apex:pageBlockSection >
                            <apex:outputText value="{!product.Price__c}" id="productPrice"/><br/>
                        </apex:pageBlockSection>
                        <apex:commandButton value="Buy" action="{!buyProduct}" id="buyButton"></apex:commandButton>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:outputPanel>
    </apex:form>
    </div>                

And my controller code (controller is common for Shopping Cart page and all the types of products pages):

 public with sharing class ProductPagesController {
public List<Product__c> tvsList {set;get;}
public List<Product__c> laptopsList {set;get;}
public List<Product__c> mobilesList {set;get;}
public List<Product__c> monitorsList {set;get;}
public List<Product__c> productsList {set;get;}
public List<Product__c> shoppingCart {set;get;}
public String cartContents;
private String currentPage = ApexPages.currentPage().getParameters().get('Id');
private boolean choosenProduct;

public ProductPagesController(){
    tvsList = selectTVsList();
    laptopsList = selectLaptopsList();
    mobilesList = selectMobilesList();
    monitorsList = selectMonitorsList();
    productsList = new List<Product__c>();
    choosenProduct = false;
    productsList.addAll(tvsList);
    productsList.addAll(laptopsList);
    productsList.addAll(mobilesList);
    productsList.addAll(monitorsList);
}

public PageReference addProductToShoppingCart(){
    if(currentPage == null){
        PageReference pageRef = new PageReference('/apex/ShoppingCart');
        return pageRef;
    }else{
        for(Product__c product : productsList) {
            if (product.Quantity__c > 0) {
                shoppingCart.add(product);
            }
        }
        return null;
    }
}

All the methods starting from select just query products by specific category from database.

  • 2
    How do you get from a product page to the shopping cart? If you're just clicking a link, you're probably losing the viewstate which is carrying the information about which products you've selected. As an alternative to what you're trying to do now, have you considered making the shopping cart a separate object? – Derek F Mar 6 '17 at 18:35
  • Yes, I am just clicking the button and get to the Shopping cart page. I have thought about making a separate object, but I am not sure what fields it needs to have. Anyway, thanks for advice. I`ll try it. – Cassie Mar 6 '17 at 18:40
  • @DerekF could you help me? How should I create ShoppingCart object as whole or as ShoppingCartItem object? – Cassie Mar 6 '17 at 19:05

Your Answer

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

Browse other questions tagged or ask your own question.