function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tyler HarrisTyler Harris 

Conditionally display commandButton based on dataTable variable?

Hello, I want to conditionally display a "Checkout" button if a merchandise item is added to the cart in the visualforce dataTable. How can I accomplish this in visualforce?
 
<apex:commandButton value="Checkout" action="{!checkout}" rendered="{!carti} <>null"/>
    </apex:form>


 
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
  <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
  <h1>Store Front</h1>
   
  <apex:form >
      <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
          
          <apex:column headerValue="Product">
              <apex:outputText value="{!pitem.merchandise.name}" />
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!pitem.merchandise.Price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:inputText value="{!pitem.tempCount}"/>
          </apex:column>
   
      </apex:dataTable>
      
      <br/>
      <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cart">
      
      </apex:commandButton>
  </apex:form>
  
    <apex:outputPanel id="msg">{!message}</apex:outputPanel><br/>    
    <h1>Your Basket</h1>
    
<apex:form >    
     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
			
   <apex:column headerValue="ID" rendered="false">
       <apex:outputText value="{!cart[carti].merchandise.Id}" >
       </apex:outputText>
          </apex:column>
          <apex:column headerValue="Product">
              <apex:outputText value="{!cart[carti].merchandise.name}">
          	
         </apex:outputText>
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!cart[carti].merchandise.price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:outputText value="{!cart[carti].count}"/>
          </apex:column>
         <apex:column headerValue="Remove?">
             <apex:commandButton action="{!Remove}" value="Remove" reRender="cart">
                 <apex:param name="rowDel" assignTo="{!rowDel}" value="{!carti}"/>
             </apex:commandButton>
         </apex:column>
                     

      </apex:dataTable>
    	<apex:commandButton value="Checkout" action="{!checkout}" rendered="{!rowDel}"/>
    </apex:form>
    
  
</apex:page>

Apex
public virtual class StoreFront2 {    
    public String message { get; set; }
    List<DisplayMerchandise> products;
    Map<Id, DisplayMerchandise> cart;

    public Id rowz;
    public id rowDel{get;set;}
    
    public PageReference shop(){
   
        handleTheBasket();
        message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.tempCount > 0){
               message += p.merchandise.name + ' (' + p.tempCount + ') ' ;
               }
            }
        return null;
    }
    
    public void remove(){
     rowz = (Id) ApexPages.currentPage().getParameters().get('rowDel');
        system.debug(rowz);
        if(cart.containsKey(rowz)){
            cart.remove(rowz);
    
            
        }  
         
    }

        public pageReference back(){
        PageReference doit = new PageReference('/apex/StoreCart');
        doit.setRedirect(false);
            return doit;
        }
    
    public Pagereference checkout(){
        PageReference send = new PageReference('/apex/ConfirmBuy');
   
        //send.setRedirect(false);
    	return send;    
    } 
    
    
    public void handleTheBasket(){
        for(DisplayMerchandise c : products){
            if(c.tempCount > 0){
            if(cart.containsKey(c.merchandise.Id)){
                
                cart.get(c.merchandise.Id).count += c.tempCount;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
                cart.get(c.merchandise.Id).count = c.tempCount;
             
          		

            } 
        
        }
        }
        
    }
    
    public Map<Id, DisplayMerchandise> getCart() {
        if(cart == null){
            cart = new Map<Id, DisplayMerchandise>();
 
                }
       
        return cart;
    }

    public class DisplayMerchandise {
        public Merchandise__c merchandise{get; set;}
        public Decimal count{get; set;}
        public Decimal tempCount{get;set;}
        public DisplayMerchandise(Merchandise__c item){
            this.merchandise = item;
            
        }
    }

    public List<DisplayMerchandise> getProducts() {
        if (products == null){
            products = new List<DisplayMerchandise>();
    
            for (Merchandise__c item :
            [SELECT id, name, description__c, price__c
            FROM Merchandise__c
            WHERE Total_Inventory__c > 0]) {
           
            products.add(new DisplayMerchandise(item));
            }
        }
        return products;
    }

}

 
Best Answer chosen by Tyler Harris
Mudasir WaniMudasir Wani
Hello Tyler,

The problem is in your below method 
Line 5 It should be inside if method
public Map<Id, DisplayMerchandise> getCart() {
        if(cart == null){
            cart = new Map<Id, DisplayMerchandise>();
                }
       incart = false;
        return cart;
    }

The updated method should be 
public Map<Id, DisplayMerchandise> getCart() {
        if(cart == null){
            cart = new Map<Id, DisplayMerchandise>();
             incart = false;
        }
       return cart;
    }
Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
 

All Answers

Ajay Nagar 7Ajay Nagar 7
Hi Tyler,
Use this :
<apex:commandButton value="Checkout" action="{!checkout}" rendered="{! cart.size > 0 }" />
Mudasir WaniMudasir Wani

Hi Tyler Harris,
The code given by ajay should work for you.The only thing you need to check is for Null check
 
<apex:commandButton value="Checkout" action="{!checkout}" rendered="{!If(AND(cart != Null,cart.size > 0),true,false }" />



If you have a requirement where you are adding and removing the cart items then you need to follow below approach.

You need to Wrap your button inside a panel and add an Id to it say id="panelid"and use the rendered attribute on "cart.size".
Another important thing you need to do is to have a reRender function on the action where you are updating the cart(adding or deleting the card records)
say you have a buttons  in that use the reRender attribute as reRender"panelid" and you are done !!!!!!!!!!!!!!!!!!!!!!

Let me in case of any issue
Rahul SharmaRahul Sharma
Also one more thing, if your original issue is resolved. 
Never use multiple apex:form, It degrades the performance of the page while increasing view state.
Eswar Prasad@Sfdc11Eswar Prasad@Sfdc11
hi Tyler harris,
do one thing what ever the products getting to datalist right in each product row we can create button and add into cartpage,once we saw flipkart we can get a idea about this issue
Tyler HarrisTyler Harris
Sorry, none of these worked. I'm getting a syntax error "StoreCart Line 1 Syntax error. Missing ')'
Tyler HarrisTyler Harris
It's also throwing a "Unknown property 'cart.size'.
Rahul SharmaRahul Sharma
Try this, He missed a round brace.
<apex:commandButton value="Checkout" action="{!checkout}" rendered="{!IF(cart != Null && cart.size > 0)}" />
If error still persists, Paste code snippet here which you had tried!
Tyler HarrisTyler Harris
I'm still getting a Unknown Property "cart.size"
Rahul SharmaRahul Sharma
Could you paste your updated code, seems like you might have changed the property name cart, as I can see its still used in page.
Tyler HarrisTyler Harris
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
  <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
  <h1>Store Front</h1>
   
  <apex:form >
      <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
          
          <apex:column headerValue="Product">
              <apex:outputText value="{!pitem.merchandise.name}" />
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!pitem.merchandise.Price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:inputText value="{!pitem.tempCount}"/>
          </apex:column>
   
      </apex:dataTable>
      
      <br/>
      <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cart">
      
      </apex:commandButton>
  </apex:form>
  
    <apex:outputPanel id="msg">{!message}</apex:outputPanel><br/>    
    <h1>Your Basket</h1>
    
<apex:form > 

     <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">
			
   <apex:column headerValue="ID" rendered="false">
       <apex:outputText value="{!cart[carti].merchandise.Id}" >
       </apex:outputText>
          </apex:column>
          <apex:column headerValue="Product">
              <apex:outputText value="{!cart[carti].merchandise.name}">
          	
         </apex:outputText>
          </apex:column>
          <apex:column headervalue="Price">
              <apex:outputText value="{!cart[carti].merchandise.price__c}" />
          </apex:column>
          <apex:column headerValue="#Items">
              <apex:outputText value="{!cart[carti].count}"/>
          </apex:column>
         <apex:column headerValue="Remove?">
             <apex:commandButton action="{!Remove}" value="Remove" reRender="cart">
                 <apex:param name="rowDel" assignTo="{!rowDel}" value="{!carti}"/>
             </apex:commandButton>
         </apex:column>             

      </apex:dataTable>

    		<apex:commandButton value="Checkout" action="{!checkout}" rendered="{!IF(cart != Null && cart.size > 0)}" />
    </apex:form>
  
</apex:page>
Rahul SharmaRahul Sharma
Try following in rendered condition for command button: rendered="{!NOT(ISBLANK(cart[0]))}" If above doesn't work, use a boolean property in controller which is false by default, make it true when you fill the data. And use that property to conditionally display the button. Note:- You should use lists to display data in column, and if your data is complex use list of wrapper classes.
Tyler HarrisTyler Harris
Hi Rahul,

I've got a property
 
public Boolean incart{get;set;}

How can I pass it to the rendered attribute?
Rahul SharmaRahul Sharma
Rendered condition accepts a condition or a boolean. You can pass the value directly as rendered="{!inCart}" Set the property's value in controller method appropriately!
Tyler HarrisTyler Harris
Hi Rahul,

I've added the logic in my controller, but it's not rendering on the page.

Apex
public virtual class StoreFront2 {    
    public String message { get; set; }
    List<DisplayMerchandise> products;
    Map<Id, DisplayMerchandise> cart;
    public Boolean incart{get;set;}
    public Id rowz;
    public id rowDel{get;set;}
    
    public PageReference shop(){
   
        handleTheBasket();
        message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.tempCount > 0){
               message += p.merchandise.name + ' (' + p.tempCount + ') ' ;
               }
            }
        return null;
    }
    
    public void remove(){
     rowz = (Id) ApexPages.currentPage().getParameters().get('rowDel');
        system.debug(rowz);
        if(cart.containsKey(rowz)){
            cart.remove(rowz);
            if(cart.isEmpty()){
                incart = false;
            }
    
            
        }  
         
    }

        public pageReference back(){
        PageReference doit = new PageReference('/apex/StoreCart');
        doit.setRedirect(false);
            return doit;
        }
    
    public Pagereference checkout(){
        if(cart.isEmpty()){
            ApexPages.Message myError = new ApexPages.Message(ApexPages.Severity.ERROR, 'Shopping Cart is Empty');
            ApexPages.addMessage(myError);
            System.debug('on');
            return null;
            
        }
        else{
        PageReference send = new PageReference('/apex/ConfirmBuy');
        return send;
        }
    } 
    
    
    public void handleTheBasket(){
        for(DisplayMerchandise c : products){
            if(c.tempCount > 0){
            if(cart.containsKey(c.merchandise.Id)){
                
                cart.get(c.merchandise.Id).count += c.tempCount;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
                cart.get(c.merchandise.Id).count = c.tempCount;
                incart = true;
                System.debug(incart);
            
            } 
        
        }
        }
        
    }
    
    public Map<Id, DisplayMerchandise> getCart() {
        if(cart == null){
            cart = new Map<Id, DisplayMerchandise>();
 			incart = false;
                }
       
        return cart;
    }

    public class DisplayMerchandise {
        public Merchandise__c merchandise{get; set;}
        public Decimal count{get; set;}
        public Decimal tempCount{get;set;}
        public DisplayMerchandise(Merchandise__c item){
            this.merchandise = item;
            
        }
    }

    public List<DisplayMerchandise> getProducts() {
        if (products == null){
            products = new List<DisplayMerchandise>();
    
            for (Merchandise__c item :
            [SELECT id, name, description__c, price__c
            FROM Merchandise__c
            WHERE Total_Inventory__c > 0]) {
           
            products.add(new DisplayMerchandise(item));
            }
        }
        return products;
    }

}

 
Mudasir WaniMudasir Wani
Modify the page code 
<apex:commandButton value="Checkout" action="{!checkout}" rendered="{!IF(cart != Null && cart.size > 0)}" />
With the following
<apex:commandButton value="Checkout" action="{!checkout}" rendered="{!incart}" />


 
Tyler HarrisTyler Harris
Yes, I've already added this to the Visualforce page and it's not working.
Mudasir WaniMudasir Wani
Paste the code and the challenges you are facing.
Give little more description what are you expecting  and what it is actually showing.
 
Rahul SharmaRahul Sharma

Seems like you are not rerendering the command button on click of Add to Cart!
When you paste the code, make sure the intendation is proper. It make your code readable easily :)
Changed the intendation properly, and removed the additional unneeded apex:form. Now its is rerendering the whole cartPanel on click of Add to Cart
 

<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
    
    <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
    <h1>Store Front</h1>

    <apex:form >
        <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
            <apex:column headerValue="Product">
                <apex:outputText value="{!pitem.merchandise.name}" />
            </apex:column>
            <apex:column headervalue="Price">
                <apex:outputText value="{!pitem.merchandise.Price__c}" />
            </apex:column>
            <apex:column headerValue="#Items">
                <apex:inputText value="{!pitem.tempCount}"/>
            </apex:column>
        </apex:dataTable>

        <br/>

        <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg, cartPanel">
        </apex:commandButton>
    
        <apex:outputPanel id="msg">
            {!message}
        </apex:outputPanel>

        <br/>    

        <h1>Your Basket</h1>

        <apex:outputPanel id="cartPanel">
            <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">

                <apex:column headerValue="ID" rendered="false">
                    <apex:outputText value="{!cart[carti].merchandise.Id}" >
                    </apex:outputText>
                </apex:column>
                <apex:column headerValue="Product">
                    <apex:outputText value="{!cart[carti].merchandise.name}">
                    </apex:outputText>
                </apex:column>
                <apex:column headervalue="Price">
                    <apex:outputText value="{!cart[carti].merchandise.price__c}" />
                </apex:column>
                <apex:column headerValue="#Items">
                    <apex:outputText value="{!cart[carti].count}"/>
                </apex:column>
                <apex:column headerValue="Remove?">
                    <apex:commandButton action="{!Remove}" value="Remove" reRender="cart">
                        <apex:param name="rowDel" assignTo="{!rowDel}" value="{!carti}"/>
                    </apex:commandButton>
                </apex:column>             

            </apex:dataTable>

            <apex:commandButton value="Checkout" action="{!checkout}" rendered="{!incart}" />
        </apex:outputPanel>

    </apex:form>

</apex:page>
Some suggestions for having nice standard look and feel in your page, if you would like:
1. You can use apex:pageblock and apex:pageBlocksection instead of h1 tag.
2. For apex:commandButton you can enclose inside apex:pageBlockButtons.
3. Could use apex:pageBlockTable instead of apex:dataTable

Hopt it helps :)
Rahul SharmaRahul Sharma
Get rid of the bold(<b>) tags, had highlighted the modified code, but seems like code panel doesn't like it :D
Tyler HarrisTyler Harris
Hi Rahul,

This isn't working and now it's not adding to the cart.
 
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
    
    <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
    <h1>Store Front</h1>

    <apex:form >
        <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
            <apex:column headerValue="Product">
                <apex:outputText value="{!pitem.merchandise.name}" />
            </apex:column>
            <apex:column headervalue="Price">
                <apex:outputText value="{!pitem.merchandise.Price__c}" />
            </apex:column>
            <apex:column headerValue="#Items">
                <apex:inputText value="{!pitem.tempCount}"/>
            </apex:column>
        </apex:dataTable>

        <br/>

        <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg, <b>cartPanel</b>">
        </apex:commandButton>
    
        <apex:outputPanel id="msg">
            {!message}
        </apex:outputPanel>

        <br/>    

        <h1>Your Basket</h1>

        <apex:outputPanel id="cartPanel">
            <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">

                <apex:column headerValue="ID" rendered="false">
                    <apex:outputText value="{!cart[carti].merchandise.Id}" >
                    </apex:outputText>
                </apex:column>
                <apex:column headerValue="Product">
                    <apex:outputText value="{!cart[carti].merchandise.name}">
                    </apex:outputText>
                </apex:column>
                <apex:column headervalue="Price">
                    <apex:outputText value="{!cart[carti].merchandise.price__c}" />
                </apex:column>
                <apex:column headerValue="#Items">
                    <apex:outputText value="{!cart[carti].count}"/>
                </apex:column>
                <apex:column headerValue="Remove?">
                    <apex:commandButton action="{!Remove}" value="Remove" reRender="cart">
                        <apex:param name="rowDel" assignTo="{!rowDel}" value="{!carti}"/>
                    </apex:commandButton>
                </apex:column>             

            </apex:dataTable>

            <apex:commandButton value="Checkout" action="{!checkout}" rendered="{!incart}" />
        </apex:outputPanel>

    </apex:form>

</apex:page>

 
Mudasir WaniMudasir Wani
Hi Tyler,

It is because below command button has <b> after msg, so I will remove it and you can try.
<apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg, <b>cartPanel</b>">
</apex:commandButton>
 
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
    
    <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
    <h1>Store Front</h1>

    <apex:form >
        <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
            <apex:column headerValue="Product">
                <apex:outputText value="{!pitem.merchandise.name}" />
            </apex:column>
            <apex:column headervalue="Price">
                <apex:outputText value="{!pitem.merchandise.Price__c}" />
            </apex:column>
            <apex:column headerValue="#Items">
                <apex:inputText value="{!pitem.tempCount}"/>
            </apex:column>
        </apex:dataTable>

        <br/>

        <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cartPanel">
        </apex:commandButton>
    
        <apex:outputPanel id="msg">
            {!message}
        </apex:outputPanel>

        <br/>    

        <h1>Your Basket</h1>

        <apex:outputPanel id="cartPanel">
            <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">

                <apex:column headerValue="ID" rendered="false">
                    <apex:outputText value="{!cart[carti].merchandise.Id}" >
                    </apex:outputText>
                </apex:column>
                <apex:column headerValue="Product">
                    <apex:outputText value="{!cart[carti].merchandise.name}">
                    </apex:outputText>
                </apex:column>
                <apex:column headervalue="Price">
                    <apex:outputText value="{!cart[carti].merchandise.price__c}" />
                </apex:column>
                <apex:column headerValue="#Items">
                    <apex:outputText value="{!cart[carti].count}"/>
                </apex:column>
                <apex:column headerValue="Remove?">
                    <apex:commandButton action="{!Remove}" value="Remove" reRender="cart">
                        <apex:param name="rowDel" assignTo="{!rowDel}" value="{!carti}"/>
                    </apex:commandButton>
                </apex:column>             

            </apex:dataTable>

            <apex:commandButton value="Checkout" action="{!checkout}" rendered="{!incart}" />
        </apex:outputPanel>

    </apex:form>

</apex:page>

Try the above code and let us know.

 
Tyler HarrisTyler Harris
The command button "checkout" is not appearing still.
Rahul SharmaRahul Sharma
As I mentioned earlier try removing all the b tags from the places like, it is some kind of issue with code snippet attacher Like below:
Tyler HarrisTyler Harris
I've removed the <b> tags. Still not working:
 
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
    
    <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
    <h1>Store Front</h1>

    <apex:form >
        <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
            <apex:column headerValue="Product">
                <apex:outputText value="{!pitem.merchandise.name}" />
            </apex:column>
            <apex:column headervalue="Price">
                <apex:outputText value="{!pitem.merchandise.Price__c}" />
            </apex:column>
            <apex:column headerValue="#Items">
                <apex:inputText value="{!pitem.tempCount}"/>
            </apex:column>
        </apex:dataTable>

        <br/>

        <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cartPanel">
        </apex:commandButton>
    
        <apex:outputPanel id="msg">
            {!message}
        </apex:outputPanel>

        <br/>    

        <h1>Your Basket</h1>

        <apex:outputPanel id="cartPanel">
            <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">

                <apex:column headerValue="ID" rendered="false">
                    <apex:outputText value="{!cart[carti].merchandise.Id}" >
                    </apex:outputText>
                </apex:column>
                <apex:column headerValue="Product">
                    <apex:outputText value="{!cart[carti].merchandise.name}">
                    </apex:outputText>
                </apex:column>
                <apex:column headervalue="Price">
                    <apex:outputText value="{!cart[carti].merchandise.price__c}" />
                </apex:column>
                <apex:column headerValue="#Items">
                    <apex:outputText value="{!cart[carti].count}"/>
                </apex:column>
                <apex:column headerValue="Remove?">
                    <apex:commandButton action="{!Remove}" value="Remove" reRender="cart">
                        <apex:param name="rowDel" assignTo="{!rowDel}" value="{!carti}"/>
                    </apex:commandButton>
                </apex:column>             

            </apex:dataTable>

            <apex:commandButton value="Checkout" action="{!checkout}" rendered="{!incart}" />
        </apex:outputPanel>

    </apex:form>

</apex:page>

 
Rahul SharmaRahul Sharma
Check in debug if you the variable - inCart is setting properly on click of add to cart..
Tyler HarrisTyler Harris
Yup, it's setting Incart to True and False.

11:13:23:049 USER_DEBUG [78]|DEBUG|true

 
Mudasir WaniMudasir Wani


The reason behind this is that incart is false in the page load
You need to wrap the button inside a panel and reRender it 

Try following
 
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
    
    <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
    <h1>Store Front</h1>

    <apex:form >
        <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
            <apex:column headerValue="Product">
                <apex:outputText value="{!pitem.merchandise.name}" />
            </apex:column>
            <apex:column headervalue="Price">
                <apex:outputText value="{!pitem.merchandise.Price__c}" />
            </apex:column>
            <apex:column headerValue="#Items">
                <apex:inputText value="{!pitem.tempCount}"/>
            </apex:column>
        </apex:dataTable>

        <br/>

        <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cartPanel,cmdPanelId">
        </apex:commandButton>
    
        <apex:outputPanel id="msg">
            {!message}
        </apex:outputPanel>

        <br/>    

        <h1>Your Basket</h1>

        <apex:outputPanel id="cartPanel">
            <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">

                <apex:column headerValue="ID" rendered="false">
                    <apex:outputText value="{!cart[carti].merchandise.Id}" >
                    </apex:outputText>
                </apex:column>
                <apex:column headerValue="Product">
                    <apex:outputText value="{!cart[carti].merchandise.name}">
                    </apex:outputText>
                </apex:column>
                <apex:column headervalue="Price">
                    <apex:outputText value="{!cart[carti].merchandise.price__c}" />
                </apex:column>
                <apex:column headerValue="#Items">
                    <apex:outputText value="{!cart[carti].count}"/>
                </apex:column>
                <apex:column headerValue="Remove?">
                    <apex:commandButton action="{!Remove}" value="Remove" reRender="cart,cmdPanelId">
                        <apex:param name="rowDel" assignTo="{!rowDel}" value="{!carti}"/>
                    </apex:commandButton>
                </apex:column>             

            </apex:dataTable>
			<apex:outputPanel id="cmdPanelId">
				<apex:commandButton value="Checkout" action="{!checkout}" rendered="{!incart}" />
			</apex:outputPanel >
        </apex:outputPanel>

    </apex:form>

</apex:page>

 
Tyler HarrisTyler Harris
This didn't render the command 'checkout' button.
Mudasir WaniMudasir Wani
If after you add some products the incart is set to true then It should definitely work for you 
If you have not tried to add the products try and let us know 
Tyler HarrisTyler Harris
Here is my Apex code. When I debug and add an item to the cart it's debugging to True:

Apex:
 
public virtual class StoreFront2 {    
    public String message { get; set; }
    List<DisplayMerchandise> products;
    Map<Id, DisplayMerchandise> cart;
    public Boolean incart{get;set;}
    public Id rowz;
    public id rowDel{get;set;}
    
    
    public PageReference shop(){
   
        handleTheBasket();
        message = 'You bought: ';
        for (DisplayMerchandise p:products){
            if(p.tempCount > 0){
               message += p.merchandise.name + ' (' + p.tempCount + ') ' ;
               }
            }
        return null;
    }
    
    public void remove(){
     rowz = (Id) ApexPages.currentPage().getParameters().get('rowDel');
        system.debug(rowz);
        if(cart.containsKey(rowz)){
            cart.remove(rowz);
            if(cart.isEmpty()){
                incart = false;
                system.debug(incart);
            }
    
            
        }  
         
    }

        public pageReference back(){
        PageReference doit = new PageReference('/apex/StoreCart');
        doit.setRedirect(false);
            return doit;
        }
    
    public void confirm(){
        for(Id d:cart.keySet()){
            
        }
        
        
    }
    
    public Pagereference checkout(){
        if(cart.isEmpty()){
            ApexPages.Message myError = new ApexPages.Message(ApexPages.Severity.ERROR, 'Shopping Cart is Empty');
            ApexPages.addMessage(myError);
            System.debug('on');
            return null;
            
        }
        else{
        PageReference send = new PageReference('/apex/ConfirmBuy');
        return send;
        }
    } 
    
    
    public void handleTheBasket(){
        for(DisplayMerchandise c : products){
            if(c.tempCount > 0){
            if(cart.containsKey(c.merchandise.Id)){
                
                cart.get(c.merchandise.Id).count += c.tempCount;
                
            }
            else{
                cart.put(c.merchandise.Id, c);
                cart.get(c.merchandise.Id).count = c.tempCount;
                incart = true;
                System.debug(incart);
            
            } 
        
        }
        }
        
    }
    
    public Map<Id, DisplayMerchandise> getCart() {
        if(cart == null){
            cart = new Map<Id, DisplayMerchandise>();
                }
       incart = false;
        return cart;
    }

    public class DisplayMerchandise {
        public Merchandise__c merchandise{get; set;}
        public Decimal count{get; set;}
        public Decimal tempCount{get;set;}
        public DisplayMerchandise(Merchandise__c item){
            this.merchandise = item;
            
        }
    }

    public List<DisplayMerchandise> getProducts() {
        if (products == null){
            products = new List<DisplayMerchandise>();
    
            for (Merchandise__c item :
            [SELECT id, name, description__c, price__c
            FROM Merchandise__c
            WHERE Total_Inventory__c > 0]) {
           
            products.add(new DisplayMerchandise(item));
            }
        }
        return products;
    }

}

Visualforce
<apex:page standardStylesheets="false" showHeader="false" sidebar="false" controller="StoreFront2">
    
    <apex:stylesheet value="{!URLFOR($Resource.styles)}"/>
    <h1>Store Front</h1>

    <apex:form >
        <apex:dataTable value="{!products}" var="pitem" rowClasses="odd,even">
            <apex:column headerValue="Product">
                <apex:outputText value="{!pitem.merchandise.name}" />
            </apex:column>
            <apex:column headervalue="Price">
                <apex:outputText value="{!pitem.merchandise.Price__c}" />
            </apex:column>
            <apex:column headerValue="#Items">
                <apex:inputText value="{!pitem.tempCount}"/>
            </apex:column>
        </apex:dataTable>

        <br/>

        <apex:commandButton action="{!shop}" value="Add to Cart" reRender="msg,cartPanel,cmdPanelId">
        </apex:commandButton>
    
        <apex:outputPanel id="msg">
            {!message}
        </apex:outputPanel>

        <br/>    

        <h1>Your Basket</h1>

        <apex:outputPanel id="cartPanel">
            <apex:dataTable id="cart" value="{!cart}" var="carti" rowClasses="odd,even">

                <apex:column headerValue="ID" rendered="false">
                    <apex:outputText value="{!cart[carti].merchandise.Id}" >
                    </apex:outputText>
                </apex:column>
                <apex:column headerValue="Product">
                    <apex:outputText value="{!cart[carti].merchandise.name}">
                    </apex:outputText>
                </apex:column>
                <apex:column headervalue="Price">
                    <apex:outputText value="{!cart[carti].merchandise.price__c}" />
                </apex:column>
                <apex:column headerValue="#Items">
                    <apex:outputText value="{!cart[carti].count}"/>
                </apex:column>
                <apex:column headerValue="Remove?">
                    <apex:commandButton action="{!Remove}" value="Remove" reRender="cart,cmdPanelId">
                        <apex:param name="rowDel" assignTo="{!rowDel}" value="{!carti}"/>
                    </apex:commandButton>
                </apex:column>             

            </apex:dataTable>
			<apex:outputPanel id="cmdPanelId">
				<apex:commandButton value="Checkout" action="{!checkout}" rendered="{!incart}" />
			</apex:outputPanel >
        </apex:outputPanel>

    </apex:form>

</apex:page>

 
Mudasir WaniMudasir Wani
Hello Tyler,

The problem is in your below method 
Line 5 It should be inside if method
public Map<Id, DisplayMerchandise> getCart() {
        if(cart == null){
            cart = new Map<Id, DisplayMerchandise>();
                }
       incart = false;
        return cart;
    }

The updated method should be 
public Map<Id, DisplayMerchandise> getCart() {
        if(cart == null){
            cart = new Map<Id, DisplayMerchandise>();
             incart = false;
        }
       return cart;
    }
Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
 
This was selected as the best answer
Rahul SharmaRahul Sharma
Haha finally resolved, great work Mudasir :)
Tyler HarrisTyler Harris
Thank you guys! This was very helpful to me.