我有捆绑产品,默认情况下选中了3个复选框。我想从页面类别列表(list.phtml
)添加捆绑产品,而不必指定选项。我怎么能这样做?
答案 0 :(得分:13)
我的项目只需要为捆绑产品显示一行,隐藏选项默认选择并在购买捆绑时购买。该产品可从类别视图中购买。
捆绑配置:
我进入了自定义类别视图模板并添加了以下内容:
<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>
上面创建了一个add-to-cart-form,如果我们有一个bundle并且默认所有选项,则检索bundle子产品。 像魅力一样工作!
答案 1 :(得分:4)
好的,我终于以我认为应该的方式工作了。
Wgenie让我朝着正确的方向前进。
我使用此代码而不是Wgenie's,它不仅将项目添加到购物车,还控制捆绑选项的库存并显示如果一个选项缺货则不可用:
<?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; ?>
答案 2 :(得分:1)
我必须在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>
答案 3 :(得分:0)
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....
}
在模板中执行此操作也是非常hacky。更好的选择imo
override Mage_Catalog_Block_Product_List::getAddToCartUrl($product, $additional = array())