I have bundle products with 3 checkboxes checked as default. I want to add a bundle product from the page category list (list.phtml
) without having to specify the options. How can I do this?
My project needed to only show a single line for a bundle product, hidden options default selected and purchased when buying the bundle. The product had be buyable from the category view.
Bundle config:
I went into my custom category view template and added the following:
<form action="<?php echo Mage::$this->helper('checkout/cart')->getAddUrl($product); ?>" method="post" id="product_addtocart_form_<?php echo $product->getId()?>">
<?php
// If we have a bundle:
if ($_product->getTypeId() == 'bundle'){
$selectionCollection = $_product->getTypeInstance(true)->getSelectionsCollection(
$_product->getTypeInstance(true)->getOptionsIds($_product), $_product
);
foreach($selectionCollection as $option) {
echo '<input type="hidden" name="bundle_option[' . $option->option_id . ']" value="' . $option->selection_id . '" />';
echo '<input type="hidden" name="bundle_option_qty[' . $option->option_id . ']" value="1" />';
}//end: foreach $selectionCollection
} // end: if $_product == bundle
?>
<input type="text" name="qty" class="qty" maxlength="4" value="1" />
<button type="button" onclick="this.form.submit()" />
</form>
The above creates a add-to-cart-form, retrieves the bundle sub-products if we have a bundle and defaults all the options. Works like a charm!
Ok, I finally got it working the way i think it should.
Wgenie put me in the right direction.
I use this code instead of Wgenie's and it not only adds the item to the cart but controls the stock of the bundle options and shows Not available if one option is out of stock:
<?php if ($_item->getTypeId() == 'bundle') : ?>
<form action="<?php echo Mage::helper('checkout/cart')->getAddUrl($_item); ?>" method="post" id="product_addtocart_form_<?php echo $_item->getId()?>">
<?php $selectionCollection = $_item->getTypeInstance(true)->getSelectionsCollection(
$_item->getTypeInstance(true)->getOptionsIds($_item), $_item
); ?>
<?php $saleable = true; ?>
<?php foreach($selectionCollection as $option) : ?>
<input type="hidden" name="bundle_option[<?php echo $option->option_id; ?>][]" value="<?php echo $option->selection_id; ?>" />
<input type="hidden" name="bundle_option_qty[<?php echo $option->option_id; ?>][]" value="1" />
<?php
//Stock control for each bundle option
$opt_product = Mage::getModel('catalog/product')->load($option->product_id);
$stocklevel = (int)Mage::getModel('cataloginventory/stock_item')
->loadByProduct($opt_product)->getQty();
if($stocklevel<=0)
$saleable = false;
?>
<?php endforeach; ?>
<?php if($saleable): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button" onclick="this.form.submit()"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</form><?php else : ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_item) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button><?php endif; ?>
I had to do similar thing in Magento 1.7 website. I was able to add bundled product to cart from products lists without redirecting to product page.
/app/design/frontend/your_package/your_theme/template/catalog/product/list.phtml
Replace occurences of
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
with
<?php
$productAddUrl = $this->helper('checkout/cart')->getAddUrl($_product);
if ($_product->getTypeId() == 'bundle'):
$bundleOptions = '?';
$selectionCollection = $_product->getTypeInstance(true)->getSelectionsCollection($_product->getTypeInstance(true)->getOptionsIds($_product), $_product);
foreach($selectionCollection as $option):
$bundleOptions .= '&bundle_option[' . $option->option_id . ']=' . $option->selection_id;
$bundleOptions .= '&bundle_option_qty[' . $option->option_id . ']=1';
endforeach;
$productAddUrl .= $bundleOptions;
endif;
?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $productAddUrl ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With