【问题标题】:Add bundle product to cart without having to specify the options无需指定选项即可将捆绑产品添加到购物车
【发布时间】:2015-10-31 00:06:18
【问题描述】:

我有捆绑产品,默认选中 3 个复选框。我想从页面类别列表 (list.phtml) 添加捆绑产品,而无需指定选项。我该怎么做?

【问题讨论】:

  • 你有什么运气吗?我真的需要能够做到这一点,据我所知,这是一个 Magento 错误..

标签: magento


【解决方案1】:

我的项目只需要为捆绑产品显示一行,购买捆绑产品时默认选择和购买隐藏选项。该产品可以从类别视图中购买。

捆绑配置:

  • 与动态价格捆绑
  • 选项配置为必填,默认值和单选按钮默认选中值

我进入了我的自定义类别视图模板并添加了以下内容:

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

上面创建了一个添加到购物车的表单,如果我们有一个捆绑包,则检索捆绑包子产品,并默认所有选项。 像魅力一样工作!

【讨论】:

  • 这个是不久前发布的,但想让你知道它非常有用,谢谢!
  • 这对我不起作用。 Magento 1.7.0.0。当我以这种方式购买捆绑产品时,该按钮仅关联四个可用选项中的一个。似乎有效,但如果您查看选项的订单和库存,只有一个发生了变化。
  • 这太棒了!像 Carlos 一样,它只是添加了第一项,但我认为这取决于您的产品配置方式。对我来说,我只需要将两个隐藏的输入回显替换为:echo '&lt;input type="text" name="bundle_option[' . $option-&gt;option_id . '][]" value="' . $option-&gt;selection_id . '" /&gt;';
【解决方案2】:

好的,我终于让它按我认为应该的方式工作了。

Wgenie 让我朝着正确的方向前进。

我使用此代码而不是 Wgenie 的代码,它不仅将商品添加到购物车,还控制捆绑选项的库存,并在一个选项缺货时显示不可用:

<?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; ?>

【讨论】:

  • 是的,这个解决方案更好。
【解决方案3】:

我不得不在 Magento 1.7 网站上做类似的事情。我能够从产品列表中将捆绑的产品添加到购物车,而无需重定向到产品页面。

/app/design/frontend/your_package/your_theme/template/catalog/product/list.phtml

替换出现的

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

<?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>

【讨论】:

    【解决方案4】:

    Joras 的解决方案有效,但它可以在报价单中创建多个项目,每个项目都包含相同的捆绑包(具有相同的选择)。原因是在将新添加的捆绑包与报价项目进行比较时,Magento(在 1.9.2.2 上测试)会创建一个由

    组成的 bundel_identity 字符串

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

    这个字符串取决于提交参数的顺序。要遵循产品设置中指定的顺序,请使用:

     $typeInstance = $product->getTypeInstance(true)
                        ->setStoreFilter($product->getStoreId(), $product);
    
                    $optionCollection = $typeInstance->getOptionsCollection($product);
     foreach ($optionCollection as $option) {
    // build your query string here....
    
    }
    

    在模板中执行此操作也很麻烦。更好的选择 imo

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多