I have 4 main blocks pages on one page with lists of products in different categories and Shopping cart page.
I want by click on the custom button Add to cart
(near each item on the product page) put selected items into the list or any other collection, which should be displayed on the Shopping cart page.
I have this problem - everytime I click on the button my list of items stays empty. I tried different ways, but all I could get is only one item displayed. Somehow, selected items are not passed to the page, though I select them by chosen ID.
Here is the code for Shopping cart page:
<apex:form id="cartForm" >
<apex:outputPanel id="productsDetail">
<apex:pageBlock title="Your Shopping Cart" id="cartBlock">
<apex:pageBlockTable value="{!shoppingCart}" var="item">
<apex:column >
<apex:commandLink value="{!item.Name}" >
<apex:param name="id" value="{!chosenItemId}"/>
</apex:commandLink>
<apex:pageBlockSection >
<apex:outputText value="{!item.Price__c}" id="itemPrice"/><br/>
</apex:pageBlockSection>-->
<apex:commandButton value="Buy" action="{!buyProduct}" id="buyButton"></apex:commandButton>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
Common controller for all 4 pages with only relevant for this task methods and variables:
public with sharing class ProductPagesController {
public List<Product__c> shoppingCart { set; get; }
public Set<String> selectedIds { set; get; }
private String currentPage = ApexPages.currentPage().getParameters().get('Id');
public Id chosenItemId { set; get; }
public List<Product__c> tvsList { set; get; }
public ProductPagesController() {
renderTV = false;
renderShoppingCart = false;
tvsList = selectTVsList();
selectedIds = new Set<String>();
shoppingCart = new List<Product__c>();
}
private Product__c selectedItems(Id itemId) {
Product__c items = [
SELECT Id
, Name
, Price__c
, Description__c
, Image__c
, Quantity__c
FROM Product__c
WHERE Id=:itemId
];
return items;
}
public PageReference addProductToShoppingCart() {
Product__c shoppingCartItem;
for(Id itemId : selectedIds){
shoppingCartItem = selectedItems(itemId);
shoppingCart.add(shoppingCartItem);
}
update shoppingCart;
return null;
}
}
One of the products page (TV category):
<apex:form id="tvsForm">
<apex:outputPanel id="tvsDetail">
<apex:pageBlock title="TVs" id="tvsBlock">
<apex:pageBlockTable value="{!tvsList}" var="tv">
<apex:column >
<apex:commandLink value="{!tv.Name}" >
<apex:param name="id" value="{!tv.Id}"/>
</apex:commandLink>
<apex:pageBlockSection >
<apex:image value="{!URLFOR($Resource.TV, tvsImages[tv.Name])}" id="tvImage"/>
</apex:pageBlockSection>
<apex:pageBlockSection >
<apex:outputText value="{!tv.Price__c}" id="tvPrice"/><br/>
<apex:outputText value="{!tv.Description__c}"/>
</apex:pageBlockSection>
<br/>
<apex:commandButton value="Add to cart" action="{!addProductToShoppingCart}" id="monitorButton" reRender="cartForm">
<apex:param name="itemsId" value="{!tv.Id}" assignTo="{!selectedIds}"></apex:param>
</apex:commandButton>
<br/><br/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockSection >
<apex:outputText value="There are {!tvsQuantity} products in this category" id="productsNumber"/><br/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
selectedItems
method can only return one item. Because you're filtering byId
.