0

Ajax call

var wishlistForm = new VarienForm('product_addtocart_form');
wishlistForm.submitAjaxWishlist = function(button, url,id){
    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'];
        if (this.validator.validate()) {
            url = url.replace("wishlist/index","inquiry/index/index");
            var data = jQuery('#product_addtocart_form').serialize();
            data += '&isAjax=1';
            jQuery.ajax( {
                url : url,
                dataType : 'json',
                type : 'post',
                data : data,
                success : function(data) {

                    if(data.status == 'ERROR'){
                        alert(data.message);
                    }else{
                        alert(data.message);
                        window.location = '<?php echo Mage::getBaseUrl().'inquiry/index/index'; ?>';

                      /*  alert(data.message);
                        if(jQuery('.block-wishlist').length){
                            jQuery('.block-wishlist').replaceWith(data.sidebar);
                        }else{
                            if(jQuery('.col-right').length){
                                jQuery('.col-right').prepend(data.sidebar);
                            }
                        }
                        if(jQuery('.header .links').length){
                            jQuery('.header .links').replaceWith(data.toplink);
                        }*/
                    }
                }
            });
        }
        Object.extend(Validation.methods, nv);
    }
}.bind(wishlistForm);
</script>

Function called

 public function indexAction()
    {
        if($this->getRequest()->getParam("isAjax")){

        $customer = Mage::getSingleton('customer/session')->getCustomer();
        $wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true); 

                /*echo "<pre>";print_r($this->getRequest()->getParams());die;*/
            $product = Mage::getModel('catalog/product')->load($this->getRequest()->getParam("product"));
            $buyRequest = new Varien_Object($this->getRequest()->getParam("options")); 
            $result = $wishlist->addNewItem($product, $buyRequest);
            $wishlist->save();
            return;
        } 

}
1
  • try return $wishlist->save(); Dec 6 '16 at 6:36
0

Try below code in your controller

public function indexAction()
{
   $response = array();
   try {
       if ($this->getRequest()->getParam("isAjax")) {

            $customer = Mage::getSingleton('customer/session')->getCustomer();
            $wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true); 

            $product = Mage::getModel('catalog/product')->load($this->getRequest()->getParam("product"));
            $buyRequest = new Varien_Object($this->getRequest()->getParam("options")); 
            $result = $wishlist->addNewItem($product, $buyRequest);
            $wishlist->save();
            $response['status'] = 'SUCCESS';            
            $response['message'] = 'Success Message';            
        }
    } catch(Exception $e) {
        $response['status'] = 'ERROR';            
        $response['message'] = $e->getMessage();
    }
    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.