0

Class:

public class StoreFrontController {
    list<productDisplay> prd;
    list<productDisplay> shoppingCart = new list<productDisplay>();

    public pageReference addToCart()
    {
        for(productDisplay ploop : prd)
        {
            if(0<ploop.quantity)
            {
                shoppingCart.add(ploop);
            }
        }
        return null;
    }
    public String getCartContents()
    {
        if(0==shoppingCart.size())
        {
            return '(Empty)';
        }
        String msg = '<ul>\n';
        for(productDisplay pdLoop : shoppingCart)
        {
            msg += '<li>';
            msg += pdLoop.name + ' (' + pdLoop.quantity + ')';
            msg += '<li>\n';
        }
        msg += '</ul>';
        return msg;
    }    
    public list<productDisplay> getProducts()
    {
        if(prd == null)
        {
            prd = new list<productDisplay>();
            for(product__C item : [ SELECT Id, Name, quantity__c, condition__c, Price__c, 
                                       stock__c FROM product__c]) 
            {
                prd.add(new productDisplay(item));
            }
        }
        return prd;
    }
    public class productDisplay
{
    public Product__c p;
    productDisplay(Product__c itemDisplay) {
        this.p = itemDisplay;
    }
    public String name
    {
        get{return p.name;}
    }
    public String condition
    {
        get{return p.condition__c;}
    }
    public Decimal price
    {
        get{return p.price__c;}
    }

    public boolean stock
    {
        get{return(0<p.Stock__c);}

    }
    public Decimal quantity 
    {
        get{ return(p.Quantity__c); }
        set{ p.Quantity__c = value; }
    }
}
}

VF Page

<apex:page controller="StoreFrontController" tabStyle="Product__c" >
<apex:form> 
    <apex:pageBlock title="Our Products">
        <apex:pageBlockSection columns="1">
            <apex:pageBlockTable value="{!products}" var="pitem">
                <apex:column headerValue="Product">
                <apex:outputText value="{!pitem.Name}"/>
                </apex:column>
                <apex:column headerValue="Condition">
                <apex:outputText value="{!pitem.condition}"/>
                </apex:column>
               <apex:column headerValue="Price">
                <apex:outputText value="{0,number,currency}" style="text-align: right;">
                <apex:param value="{!pitem.Price}"/>
                </apex:outputText>
                   </apex:column>
                <apex:column headerValue="Stock">
                <apex:outputText value="{!pitem.stock}"/>
                </apex:column>
                <apex:column headerValue="Qty to Buy">
                <apex:inputText value="{!pitem.quantity}" rendered="{! pitem.Stock}"/>
                <apex:outputText value="Out of Stock" rendered="{! NOT(pitem.Stock)}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
        <apex:pageBlockSection>
            <apex:commandButton title="Add to Cart" value="{!addToCart}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:pageBlock title="Cart Contents" id="shopping_cart">
        <apex:outputText value="{!cartContents}" escape="false" />
    </apex:pageBlock>
    </apex:form>
</apex:page>

1 Answer 1

1

You are using <apex:commandButton />'s value property incorrectly. Replace your Add to Cart button code as follows.

<apex:commandButton title="Add to Cart" action="{!addToCart}" value="Add to Cart" />

also add a reRender attribute as well if need. Read the documentation for more detail.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .