Here is an example of how you can modify the catalog/product_list_toolbar.phtml file in your Magento theme to enable AJAX functionality for adding products to the cart.
The main changes involve replacing the standard “Add to Cart” button with a custom JavaScript function that sends an AJAX request to the server, and then updates the page content dynamically based on the response. This allows users to add items to their cart without leaving the current page or refreshing it.
I’ll provide you with the modified code for the catalog/product_list_toolbar.phtml file:
<code>
<?php if (!isset($_product)): ?>
<p class="note-msg"><?php echo $this->__('We can\'t find products that match your request.') ?></p>
<?php else: ?>
<div class="toolbar-bottom">
<?php if ($this->getMode() == 'grid'): ?>
<ul id="product_list_toolbar" class="products-grid items-<?php echo count($_product); ?>">
<?php $i = 0; foreach ($_product as $_product): ?>
<li class="item<?php if(++$i == sizeof($_product)): ?> last</li>
<div class="item-box">
<ul class="add-to-links" style="display:none;" id="addedoverlay"><br />
<li style="text-align:center;">
<p style="font-size:13px;"><?php echo $this->__('Adding product...') ?></p>
</li>
</ul>
<div id="added" style="display:none; width: 250px; height: 70px; background-image:none; padding:10px 0px;">
<span class="close" id="closewindow" href="#" onclick="hidewindow()">&times;</span>
</div>
<div class="product-view">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->escapeHtml($this->instantiateLayout('catalog_product_view')->createBlock('core/template')->setProduct($_product)->getPriceHtml()) ?>"
id="productimgover<?php echo $_product->getId(); ?>" alt="<?php echo $this->escapeHtml($_product->getName()) ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'image') ?>"
width="200" height="200" alt="<?php echo $this->escapeHtml($this->instantiateLayout('catalog_product_view')->createBlock('core/template')->setProduct($_product)->getPriceHtml()) ?>" />
</a>
</div>
<p class="add-to-cart">
<?php if ($_product->isSaleable()): ?>
<?php $stockData = $_product->getStockItem(); ?>
<?php if ($stockData->getIsInStock() == 1): ?>
<?php echo $this->__('Add to Cart') ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>"
class="button btn-cart" onclick="setLocationAjax('<?php echo $this->getAddToCartUrl($_product) ?>','<?php echo $_product->getId()?>')">
<span><span>Add to Cart</span></span>
</button>
<?php else: ?>
<p class="availability out-of-stock">
<?php echo $this->__('Out of stock') ?>
</p>
<?php endif; ?>
<?php endif; ?>
</p>
<div class="clear" style="display:none;" id="productname<?php echo $_product->getId(); ?>"></div>
</div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</code>
The modified code includes a new addedoverlay div that acts as an overlay window when a product is being added to the cart. It displays a loading message while the AJAX request is in progress.
The added div contains the actual content of the overlay window, which is initially hidden and updated dynamically with the response from the server after successfully adding a product to the cart.
The productimgover element stores the product image that will be displayed inside the overlay window when the item is added to the cart.
The add-to-cart paragraph contains the “Add to Cart” button, which triggers the setLocationAjax function when clicked. This function sends an AJAX request to the server with the URL of the add-to-cart page and the product ID as parameters.
The JavaScript code for the AJAX functionality is included at the end of the file:
<code>
<script type="text/javascript">
function hidewindow(windowBox,windowOver){
windowOver.fadeOut(400);
windowBox.fadeOut(400).html('');
}
</script>
</code>
The hidewindow function is used to close the overlay window when the user clicks on it or presses the “Continue shopping” button.
You can download the modified code and copy it into your theme’s catalog/product_list_toolbar.phtml file. Make sure you have enabled the AJAX functionality in your Magento configuration by setting the following settings:
- Go to System > Configuration
- Select the desired scope (default or store view)
- Expand the “Sales” section
- Click on “Checkout”
- Set “Enable Onepage Checkout” to “Yes”
After applying these changes, you should be able to add products to your cart using AJAX without leaving the current page. The content of the page will update dynamically based on the server response.