Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add bundle product to cart without having to specify the options

Tags:

magento

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?

like image 897
Ahmed Ala Dali Avatar asked May 06 '11 12:05

Ahmed Ala Dali


3 Answers

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:

  • Bundle with dynamic price
  • Options are configured to be required, default values and radio buttons default selected values

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!

like image 108
Wgenie Avatar answered Oct 28 '22 04:10

Wgenie


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; ?>
like image 36
Carlos Faria Avatar answered Oct 28 '22 05:10

Carlos Faria


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>
like image 1
Saroj Avatar answered Oct 28 '22 06:10

Saroj