Adding Bundle Products to Cart Without Selecting Options
Adding bundle products to your Magento cart without selecting options can enhance user experience by allowing customers to quickly purchase pre-defined product combinations. In this post, we’ll guide you through the process of enabling this functionality for your bundle products.
Understanding Magento’s Default Behavior
By default, Magento requires users to select the component options before adding a bundle product to their cart. This is especially true when you’re displaying bundles in category listings or search results (list.phtml).
However, there are situations where you might want to offer customers a quick way to add these pre-configured products to their cart. For example, if a bundle consists of popular combinations that rarely change, allowing direct addition can speed up the checkout process.
Enabling Direct Bundle Addition
To allow direct addition of bundle products without selecting options, you’ll need to modify the core Magento code. This involves updating the <code>list.phtml</code> template and the cart add action.
Step 1: Update the list.phtml Template
Open your theme’s <code>list.phtml</code> file located under app/design/frontend/{vendor}/{theme}/Magento_Catalog/templates/product/list.phtml. Find the section where bundle products are displayed in the grid.
The default code looks like this:
<div class="actions-primary">
<a href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($item) ?>" title="<?php /* @escapeNotVerified */ echo $block->__('View Details') ?>">
<?php /* @escapeNotVerified */ echo $block->__('View Details'); ?>
</a>
</div>
To allow direct addition, you’ll need to add the following code:
<div class="actions-primary">
<button type="button" data-product-id="<?php /* @escapeNotVerified */ echo $item->getId() ?>"
class="action-tooltip action-add" title="<?php /* @escapeNotVerified */ echo $block->__('Add to Cart') ?>">
<?php /* @escapeNotVerified */ echo $block->__('Add to Cart') ?>
</button>
</div>
<script type="text/x-magento-init">
{
"Magento_Catalog/js/catalog-add-to-cart": {
"template": "Magento_Catalog/addtocart.phtml"
}
}
</script>
This code adds a new button for each bundle product that triggers the catalog.add.to.cart JavaScript action.
Step 2: Modify the Cart Addition Logic
Open <code>app/code/Magento/Catalog/view/frontend/templates/product/view/options.phtml</code> and find the following line:
<button type="button" data-action="add-to-cart"
data-product="<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>"
class="action add-to-cart primary">
<span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>
Replace it with:
<button type="button" data-action="add-bundle-to-cart"
data-product-id="<?php /* @escapeNotVerified */ echo $block->getProduct()->getId() ?>"
class="action add-to-cart primary">
<span><?php /* @escapeNotVerified */ echo __('Add to Cart') ?></span>
</button>
This update changes the data-action attribute from add-to-cart to add-bundle-to-cart. This new action will be used when adding pre-configured bundle products.
Conclusion
By following these steps, you can now offer your customers a convenient way to add pre-defined bundle combinations directly to their cart without having to select the options first. This can significantly improve the user experience for your Magento-powered e-commerce store.
Remember that modifying core Magento files is generally not recommended as it can cause issues during updates. If possible, consider using a custom module or theme extension to achieve this functionality while maintaining upgrade compatibility. You can learn more about how to create a custom Magento module and working with themes on our developer blog.