开发者

Add bundle product to cart without having to specify the options

开发者 https://www.devze.com 2023-03-03 16:07 出处:网络
I have bundle products with 3 checkboxes checked as default. I want to add a bundle product from the page category list (lis开发者_如何学Ct.phtml) without having to specify the options. How can I do t

I have bundle products with 3 checkboxes checked as default. I want to add a bundle product from the page category list (lis开发者_如何学Ct.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:

  • 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!


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>


Joras' solution works, but it can create multiple items in the quote, each containing the same bundle (with identical selections). The reason is that when comparing a a newly added bundle with the quote items, Magento (tested on 1.9.2.2) creates a bundel_identity string composed out of

[product_id]_[option_id_1]_[option_qty_]_[option_id_2]_[option_qty_2]...

The this string depends on the order of the parameters submitted. To follow the order specified in the product settings use :

 $typeInstance = $product->getTypeInstance(true)
                    ->setStoreFilter($product->getStoreId(), $product);

                $optionCollection = $typeInstance->getOptionsCollection($product);
 foreach ($optionCollection as $option) {
// build your query string here....

}

Also it is quite a hacky¡ to do that inside the template. Better option imo

override Mage_Catalog_Block_Product_List::getAddToCartUrl($product, $additional = array())
0

精彩评论

暂无评论...
验证码 换一张
取 消