Search code examples
phpmagentoshopping-cartmage

Magento: Product Move from cart to Wishlist


I am trying to catch the event of move cart item to wishlist. I tried wishlist_product_add and wishlist_product_item events. But they not work on cart page. I want to save the custom options of product in wishlist. But when i move product from cart to wishlist all the options data are lost.


Solution

  • You can use following event

           <wishlist_item_add_after>
                <observers>
                   <cop_wishlist_price_options>         
                        <class>AAA_Addon_Model_Observer</class>
                        <method>addProductPriceOptionsToWishlist</method>           
                    </cop_wishlist_price_options>
                </observers>
            </wishlist_item_add_after>
    

    and in observer, you can have access to wishlist and quote. copy data/options form quote to wishlist items.

    public function addProductPriceOptionsToWishlist($observer){
    
    try {
    
        $wishlist = $observer->getEvent()->getWishlist();   
        $itemId = (int) $this->_getRequest()->getParam('item');
    
        $cart = Mage::getSingleton('checkout/cart');
        $session = Mage::getSingleton('checkout/session');
    
    
            $quote_item = $cart->getQuote()->getItemById($itemId);
            if (!$quote_item) {
                Mage::throwException(
                    Mage::helper('wishlist')->__("Requested cart item doesn't exist")
                );
            }
    
            $productId  = $quote_item->getProductId();
    
        $buyRequest = $quote_item->getBuyRequest();
        $buyRequestData = $buyRequest->getData();
    
    
        if($buyRequestData):
    
        $additional_op = $quote_item->getOptionByCode('additional_options');
        $additional_op = $additional_op->getData();
    
        $wishlist_item = Mage::getModel('wishlist/item')->load($productId,'product_id');
    
    
        //save additonal options to wishlist-options        
        $modelRes_w_item_op = Mage::getModel('wishlist/item_option');
        $modelRes_w_item_op->setWishlistItemId($wishlist_item->getId());
        $modelRes_w_item_op->setProductId($productId);
        $modelRes_w_item_op->setCode('additional_options');
        $modelRes_w_item_op->setValue($additional_op['value']);
        $modelRes_w_item_op->save();
    
        endif;
    
    
        } catch (Mage_Core_Exception $e) {
            Mage::log($e->getMessage());
        } catch (Exception $e) {
            Mage::log($e->getMessage());
        }
    
    
    }