Reputation: 127
Scenario:
Want to add multiple products to cart from wishlist through check box, such that the selected products along with their quantity are transferred to cart and the selected items are removed from the wishlist table.
Referred many blogs.
I tried to implement it by (applying) adding related-products javascript (logic).
But still not getting.
[updated]
This is the checkbox column (time being hard code)
<input type="checkbox" class="checkbox related-checkbox" id="related-checkbox7" name="related_products[]" value="7">
Added Javascript for this part:
<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>
Add to cart
<button type="button" title="Add to Cart" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span>Add to Cart</span></span></button>
And javascript for this part:
<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>
Thanks in advance
Upvotes: 1
Views: 6109
Reputation: 2373
On clicking (add all to cart) the form action goes to /wishlist/update part here in this part. Add updatenew function and add the following code
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');
}
}
In the above code we are adding product into cart programmatically and simultaneously we are deleting it from wishlist.
First in 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() ?>" />
You can adjust it accordingly.
Now in checkbox part pass product_id and wishlist_id with quantity of product(already qty field is there) in post value.
ALAs ! ! !
Upvotes: 1