VirtueMart 2 Add to Cart button on Category Page - Joomla & VirtueMart Tips & How To's

This is a simple copy and paste of how to show the Add to Cart button on the VirtueMart 2 category pages.

Home Joomla VirtueMart Tips VirtueMart 2 Add to Cart button on Category Page

Thu

31

Jan

VirtueMart 2 Add to Cart button on Category Page
This is a simple copy and paste of how to show the "Add to Cart" button on the VirtueMart 2 category pages.

So your code isn't overwritten on the next update make sure you do this with a template overide!

virtuemart 2 add to cart button on category pageConstruct the following folder hierarchy in your current Joomla template folder.

Then browse to components/com_virtuemart/views/category/tmpl/default.php and copy this file to the category folder you just made so the path will be
templates/beez_20/html/com_virtuemart/category/default.php

Open this file and paste the following code in around line 309 just after the below code.
 
<p>
<?php // Product Details Button
  echo JHTML::link ($product->link, JText::_ ('COM_VIRTUEMART_PRODUCT_DETAILS'), array('title' => $product->product_name, 'class' => 'product-details'));?>
</p>
 

Paste in this code:
 
<div class="clear"> </div>
<div class="width100">  
<?php // Add To Cart Button
  if (!VmConfig::get('use_as_catalog', 0) and !empty($product->prices)) {?>
<div class="addtocart-area"><form method="post" class="product js-recalculate" action="index.php">
  <?php // Product custom_fields
  if (!empty($product->customfieldsCart)) {  ?>
<div class="product-fields">
  <?php foreach ($product->customfieldsCart as $field)
  { ?>
<div style="text-align: left;" class="product-field product-field-type-<?php echo $field->field_type ?>">
  <span class="product-fields-title"><b><?php echo  JText::_($field->custom_title) ?></b></span>
  <?php if ($field->custom_tip) echo JHTML::tooltip($field->custom_tip,  JText::_($field->custom_title), 'tooltip.png'); ?>
 
       <span class="product-field-display"><?php echo $field->display ?></span>
 
  <span class="product-field-desc"><?php echo $field->custom_field_desc ?></span>
  </div>
 
  <?php
    }
  ?>
  </div>
  <?php }
  /* Product custom Childs
   * to display a simple link use $field->virtuemart_product_id as link to child product_id
   * custom_value is relation value to child
  */
 
  if (!empty($product->customsChilds)) {  ?>
<div class="product-fields">
  <?php foreach ($product->customsChilds as $field) {  ?>
<div style="display: inline-block; float: right; padding: 3px;" class="product-field product-field-type-<?php echo $field->field->field_type ?>">
  <span class="product-fields-title"><b><?php echo JText::_($field->field->custom_title) ?></b></span>
  <span class="product-field-desc"><?php echo JText::_($field->field->custom_value) ?></span>
  <span class="product-field-display"><?php echo $field->display ?></span>
 
  </div>
 
  <?php } ?>
  </div>
  <?php } ?>
<div class="addtocart-bar">
 
  <?php // Display the quantity box ?>
    <label for="quantity<?php echo $product->virtuemart_product_id;?>" class="quantity_box"><?php echo JText::_('COM_VIRTUEMART_CART_QUANTITY'); ?>: </label>
 
    <span class="quantity-box">
        <input type="text" class="quantity-input js-recalculate" name="quantity[]" value="<?php if (isset($product->min_order_level) && (int)$product->min_order_level > 0) {
            echo $product->min_order_level;
        } else {
            echo '1';
        } ?>"/>
        </span>
                <span class="quantity-controls js-recalculate">
        <input type="button" class="quantity-controls quantity-plus"/>
        <input type="button" class="quantity-controls quantity-minus"/>
        </span>
  <!-- Display the quantity box END -->
 
  <?php // Add the button
  $button_lbl = JText::_('COM_VIRTUEMART_CART_ADD_TO');
  $button_cls = 'addtocart-button'; //$button_cls = 'addtocart_button';
  $button_name = 'addtocart'; //$button_cls = 'addtocart_button';
 
 
  // Display the add to cart button
  $stockhandle = VmConfig::get('stockhandle','none');
  if(($stockhandle=='disableit' or $stockhandle=='disableadd') and ($product->product_in_stock - $product->product_ordered)<1){
  $button_lbl = JText::_('COM_VIRTUEMART_CART_NOTIFY');
  $button_cls = 'notify-button';
  $button_name = 'notifycustomer';
  }
  vmdebug('$stockhandle '.$stockhandle.' and stock '.$product->product_in_stock.' ordered '.$product->product_ordered);
  ?>
  <span class="addtocart-button">
  <?php if ($button_cls == "notify-button") { ?>
         <span class="outofstock"><?php echo JText::_('COM_VIRTUEMART_CART_PRODUCT_OUT_OF_STOCK'); ?></span>
 
           <?php } else {?>
           <input name="<?php echo $button_name ?>" class="<?php echo $button_cls ?>" value="<?php echo $button_lbl ?>" title="<?php echo $button_lbl ?>" type="submit" />
        <?php } ?>
  </span>
<div class="clear"> </div>
  </label></div>
 
  <?php // Display the add to cart button END ?>
  <input class="pname" value="<?php echo $product->product_name ?>" type="hidden" />
  <input name="option" value="com_virtuemart" type="hidden" />
  <input name="view" value="cart" type="hidden" />
 
  <input name="virtuemart_product_id[]" value="<?php echo $product->virtuemart_product_id ?>" type="hidden" />
  <?php /** @todo Handle the manufacturer view */ ?>
  <input name="virtuemart_manufacturer_id" value="<?php echo $product->virtuemart_manufacturer_id ?>" type="hidden" />
  <input name="virtuemart_category_id[]" value="<?php echo $product->virtuemart_category_id ?>" type="hidden" />
  </form>
        </div>
  <?php }  // Add To Cart Button END ?>
</div>
 


Now the "Add to Cart" button is displayed in your category views. Note this will incl. the quantity fields and any custom fields.

virtuemart 2 add to cart button on category page 2

If you need JUST an add to cart button - e.g. quick add - with no quantity or custom fields use the following code instead:
<form method="post" class="product" action="index.php" id="addtocartproduct<?php echo $product->virtuemart_product_id ?>">
<div class="addtocart-bar">
                <?php // Display the quantity box ?>
 
                <span class="quantity-box">
                  <input style="display: none;" class="quantity-input" name="quantity[]" value="1" type="text" />
                </span>
 
                <?php // Display the quantity box END ?>
                <?php // Add the button
                $button_lbl = JText::_('COM_VIRTUEMART_CART_ADD_TO');
                $button_cls = ''; //$button_cls = 'addtocart_button';
                if (VmConfig::get('check_stock') == '1' && !$this->product->product_in_stock) {
                  $button_lbl = JText::_('COM_VIRTUEMART_CART_NOTIFY');
                  $button_cls = 'notify-button';
                } ?>
                <?php // Display the add to cart button ?>
                <span class="addtocart-button">
                  <input name="addtocart" class="addtocart-button" value="<?php echo $button_lbl ?>" title="<?php echo $button_lbl ?>" type="submit" />
                </span>
<div class="clear"> </div>
              </div>
              <?php // Display the add to cart button END ?>
              <input class="pname" value="<?php echo $product->product_name ?>" type="hidden" />
              <input name="option" value="com_virtuemart" type="hidden" />
              <input name="view" value="cart" type="hidden" /><noscript><input name="task" value="add" type="hidden" /></noscript>
              <input name="virtuemart_product_id[]" value="<?php echo $product->virtuemart_product_id ?>" type="hidden" />
              <?php /** @todo Handle the manufacturer view */ ?>
              <input name="virtuemart_manufacturer_id" value="<?php echo $product->virtuemart_manufacturer_id ?>" type="hidden" />
              <input name="virtuemart_category_id[]" value="<?php echo $product->virtuemart_category_id ?>" type="hidden" />
          </form>
 


This will give you just an add to cart button.
Last Updated on Wednesday, 18 June 2014 13:05
 
VirtueMart
Your Cart is currently empty.

Latest Joomla & VirtueMart Tips

See all tips & examples



VirtueMart 2 Add to Cart button on Category Page|Joomla & VirtueMart Tips & How To's



This is a simple copy and paste of how to show the Add to Cart button on the VirtueMart 2 category pages.


product, custom, type, hidden, field, button, virtuemart, code, category, cart, add, title, folder, jtext, echo, following, value, quantity, stock, ordered, fields, paste, notify, customfieldscart, class, tip, level, order, customschilds, min, child, tooltip, display, link, details, views, template, file, default, copy


guru

We use cookies to improve our website and your experience when using it. Cookies used for the essential operation of the site have already been set.
To find out more about the cookies we use and how to delete them, see our privacy policy.

I accept cookies from this site.