Adding Configurable Product to Cart from Wishlist in Magento – Keep Item in Wishlist

configurable-productproductwishlist

I have a configurable item in the wishlist. I hope to add this product to the cart. default functionality takes this item out of the wishlist using something like $item->unset. How can I add this item to the cart without unsetting it from the wishlist itself? Dumping some info about the object reveals this structure here:

 Array
(
    [info_buyRequest] => Mage_Wishlist_Model_Item_Option Object
        (
            [_item:protected] => Mage_Wishlist_Model_Item Object
                (
                    [_customOptionDownloadUrl:protected] => wishlist/index/downloadCustomOption
                    [_eventPrefix:protected] => wishlist_item
                    [_eventObject:protected] => item
                    [_options:protected] => Array
                        (
                            [0] => Mage_Wishlist_Model_Item_Option Object
 *RECURSION*
                            [1] => Mage_Wishlist_Model_Item_Option Object
                                (
                                    [_item:protected] => Mage_Wishlist_Model_Item Object
 *RECURSION*
                                    [_product:protected] => 
                                    [_eventPrefix:protected] => core_abstract
                                    [_eventObject:protected] => object
                                    [_resourceName:protected] => wishlist/item_option
                                    [_resource:protected] => 
                                    [_resourceCollectionName:protected] => wishlist/item_option_collection
                                    [_cacheTag:protected] => 
                                    [_dataSaveAllowed:protected] => 1
                                    [_isObjectNew:protected] => 
                                    [_data:protected] => Array
                                        (
                                            [option_id] => 123
                                            [wishlist_item_id] => 123
                                            [product_id] => 123123
                                            [code] => attributes
                                            [value] => a:1:{i:123;s:4:"123123";}
                                        )

How do I go about accessing these attribute values? Is there a better way?

The above information is found by using $item->getProduct()->getCustomOptions() on the configurable wishlist item.

Best Answer

I am totally agree with Marius.

when you go to the class Mage_Wishlist_IndexController.

/**
 * Add wishlist item to shopping cart and remove from wishlist
 *
 * If Product has required options - item removed from wishlist and redirect
 * to product view page with message about needed defined required options
 */
public function cartAction()
{
    if (!$this->_validateFormKey()) {
        return $this->_redirect('*/*');
    }
    $itemId = (int) $this->getRequest()->getParam('item');

    /* @var $item Mage_Wishlist_Model_Item */
    $item = Mage::getModel('wishlist/item')->load($itemId);
    if (!$item->getId()) {
        return $this->_redirect('*/*');
    }
    $wishlist = $this->_getWishlist($item->getWishlistId());
    if (!$wishlist) {
        return $this->_redirect('*/*');
    }

    // Set qty
    $qty = $this->getRequest()->getParam('qty');
    if (is_array($qty)) {
        if (isset($qty[$itemId])) {
            $qty = $qty[$itemId];
        } else {
            $qty = 1;
        }
    }
    $qty = $this->_processLocalizedQty($qty);
    if ($qty) {
        $item->setQty($qty);
    }

    /* @var $session Mage_Wishlist_Model_Session */
    $session    = Mage::getSingleton('wishlist/session');
    $cart       = Mage::getSingleton('checkout/cart');

    $redirectUrl = Mage::getUrl('*/*');

    try {
        $options = Mage::getModel('wishlist/item_option')->getCollection()
                ->addItemFilter(array($itemId));
        $item->setOptions($options->getOptionsByItem($itemId));

        $buyRequest = Mage::helper('catalog/product')->addParamsToBuyRequest(
            $this->getRequest()->getParams(),
            array('current_config' => $item->getBuyRequest())
        );

        $item->mergeBuyRequest($buyRequest);

if ($item->addToCart($cart, false)) {

            $cart->save()->getQuote()->collectTotals();
        }

        $wishlist->save();
        Mage::helper('wishlist')->calculate();

        if (Mage::helper('checkout/cart')->getShouldRedirectToCart()) {
            $redirectUrl = Mage::helper('checkout/cart')->getCartUrl();
        } else if ($this->_getRefererUrl()) {
            $redirectUrl = $this->_getRefererUrl();
        }
        Mage::helper('wishlist')->calculate();
    } catch (Mage_Core_Exception $e) {
        if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_NOT_SALABLE) {
            $session->addError($this->__('This product(s) is currently out of stock'));
        } else if ($e->getCode() == Mage_Wishlist_Model_Item::EXCEPTION_CODE_HAS_REQUIRED_OPTIONS) {
            Mage::getSingleton('catalog/session')->addNotice($e->getMessage());
            $redirectUrl = Mage::getUrl('*/*/configure/', array('id' => $item->getId()));
        } else {
            Mage::getSingleton('catalog/session')->addNotice($e->getMessage());
            $redirectUrl = Mage::getUrl('*/*/configure/', array('id' => $item->getId()));
        }
    } catch (Exception $e) {
        Mage::logException($e);
        $session->addException($e, $this->__('Cannot add item to shopping cart'));
    }

    Mage::helper('wishlist')->calculate();

    return $this->_redirectUrl($redirectUrl);
}

Now when you add item to cart, then it will be still available in you wishlist.