gpt4 book ai didi

javascript - magento 通过复选框将多个产品从愿望 list 添加到购物车

转载 作者:行者123 更新时间:2023-11-28 02:35:29 34 4
gpt4 key购买 nike

场景:
希望通过复选框将多个产品从心愿单添加到购物车,以便将所选产品及其数量转移到购物车,并将所选商品从购物车中删除愿望 list 表。
引用了很多博客。
我尝试通过(应用)添加相关产品 javascript(逻辑)来实现它。
但仍然没有得到。

[已更新]

这是复选框列(暂时是硬编码)

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

为此部分添加了 Javascript:

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

var relatedProductsCheckFlag = false;
function selectAllRelated(txt){
if (relatedProductsCheckFlag == false) {
$$('.related-checkbox').each(function(elem){
elem.checked = true;
});
relatedProductsCheckFlag = true;
txt.innerHTML="unselect all";
} else {
$$('.related-checkbox').each(function(elem){
elem.checked = false;
});
relatedProductsCheckFlag = false;
txt.innerHTML="select all";
}
addRelatedToProduct();
}

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>

添加到购物车
<button type="button" title="Add to Cart" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span>Add to Cart</span></span></button>

这部分的 JavaScript:

<script type="text/javascript">
//<![CDATA[
var productAddToCartForm = new VarienForm('product_addtocart_form');
productAddToCartForm.submit = function(button, url) {
if (this.validator.validate()) {
var form = this.form;
var oldUrl = form.action;

if (url) {
form.action = url;
}
var e = null;
try {
this.form.submit();
} catch (e) {
}
this.form.action = oldUrl;
if (e) {
throw e;
}

if (button && button != 'undefined') {
button.disabled = true;
}
}
}.bind(productAddToCartForm);

productAddToCartForm.submitLight = function(button, url){
if(this.validator) {
var nv = Validation.methods;
delete Validation.methods['required-entry'];
delete Validation.methods['validate-one-required'];
delete Validation.methods['validate-one-required-by-name'];
// Remove custom datetime validators
for (var methodName in Validation.methods) {
if (methodName.match(/^validate-datetime-.*/i)) {
delete Validation.methods[methodName];
}
}

if (this.validator.validate()) {
if (url) {
this.form.action = url;
}
this.form.submit();
}
Object.extend(Validation.methods, nv);
}
}.bind(productAddToCartForm);
//]]>
</script>

提前致谢

最佳答案

单击(将全部添加到购物车)时,表单操作将转到本部分中的/wishlist/update 部分。添加updatenew函数并添加以下代码

public function updatenewAction()
{
$post = $this->getRequest()->getPost();
$product_id = $qtyarr=array();
$product_id = $this->getRequest()->getPost('product123');
$qtyarr = $this->getRequest()->getPost('qty');
if($product_id)
{
$procol = Mage::getModel('catalog/product');
$cart = Mage::getModel('checkout/cart');
foreach($product_id as $productid => $wishlistid)
{
$proid = $procol->load($productid);
$_product = $procol->load($productid);
$proname = $procol->getName();
$quantity = $qtyarr[$wishlistid] ;
$cart->init();
$cart->addProduct($productid,$quantity);
$cart->save();
Mage::getSingleton('core/session')->addSuccess($proname.' moved from Wishlist');

Mage::getModel('wishlist/item')->load($wishlistid)->delete();
}
return $this->_redirectUrl(Mage::helper('checkout/cart')->getCartUrl());
}
else
{
Mage::getSingleton('core/session')->addError('No Product Selected');
$this->_redirect('wishlist');
}

}

在上面的代码中,我们以编程方式将产品添加到购物车,同时我们将其从愿望 list 中删除。
首先在 default/template/wishlist/item/column/image.phtml

<input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $product->getId()?>" name="product123[<?php echo $product->getId() ?>]" value="<?php echo $item->getId() ?>" />

您可以进行相应调整。
现在在复选框部分传递product_id和wishlist_id以及发布值中的产品数量(已经有数量字段)。
阿拉斯加! ! !

关于javascript - magento 通过复选框将多个产品从愿望 list 添加到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13523095/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com