简体   繁体   English

Magento:选中复选框同时将多个产品添加到购物车

[英]Magento: Add multiple product to the cart simultaneously with checkbox

We have a scenario where on the product page we have a few text links that reference a purchasable product "in addition to the actual product that the user is currently viewing". 我们有一种情况,即在产品页面上,除了“用户当前正在查看的实际产品之外”,还有一些文本链接引用了可购买的产品。

The goal would be to allow the user to make additional sections via either checkbox or possibly a radio button and then add the selected additional item to the cart when the user clicks the generic add to cart button. 目的是允许用户通过复选框或单选按钮创建其他部分,然后在用户单击通用的“添加到购物车”按钮时将所选的其他项目添加到购物车。

The idea is to sell additional services, such as a warranty, a membership or others at that same time as purchasing their main product. 这个想法是在购买主要产品的同时出售其他服务,例如保修,会员资格或其他服务。

While I do realize this could be done via grouped or bundled products, we wish to have a much more custom view of how the products are represented on the page thus causing us to pursue this method of adding multiple products on the fly. 虽然我确实知道可以通过组合或捆绑的产品来完成此操作,但我们希望对页面上的产品表示方式有更多的自定义视图,从而促使我们采用这种动态添加多个产品的方法。

The other difficulty is that we would like to try and avoid modifying any core files and stay within out theme and local files if at all possible. 另一个困难是,我们要尽量避免修改任何核心文件,并尽可能保留主题文件和本地文件。

Thank you in advance! 先感谢您!

[UPDATE] I have been testing a small piece of code that will add an actual stocked item via the following when the user uses the default add to cart [更新]我一直在测试一小段代码,当用户使用默认的添加到购物车时,该代码将通过以下方式添加实际的库存商品

<?php // add multiple items to cart
$cart = Mage::getModel("checkout/cart");
$cart->addProduct($someId, $someQty);                                
$cart->save();
?>

Found a solutions with the following: 找到了以下解决方案:

<script type="text/javascript">
//<![CDATA[
$$('.related-checkbox').each(function(elem){
Event.observe(elem, 'click', addRelatedToProduct)
});

var relatedProductsCheckFlag = false;
function addRelatedToProduct(){
var checkboxes = $$('.related-checkbox');
var values = [];
for(var i=0;i<checkboxes.length;i++){
    if(checkboxes[i].checked) values.push(checkboxes[i].value);
}
if($('related-products-field')){
    $('related-products-field').value = values.join(',');
}
}
//]]>
</script>

You can then add the following item(s): 然后,您可以添加以下项目:

<input type="checkbox" class="checkbox related-checkbox" id="related-checkbox35" name="related_products[]" value="35">

Hopefully this may help some one in the future trying to do the same thing! 希望这可能对将来尝试做同一件事的人有所帮助!

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM