0

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.

0
+50

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.

2

Rewrite the method Mage_Wishlist_IndexController::cartAction() and replace the line

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

with

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

This should add the item to the cart but still keep it in the wishlist.

5
  • do you know of a way to get a wishlist item by specific wishlist item id? Apr 6 '15 at 14:32
  • @easymoden00b Mage::getModel('wishlist/item')->load($Id);
    – Marius
    Apr 6 '15 at 14:48
  • apparently it's now throwing a 'Please specify the product's option(s) for...' exception when I attempt to add a configurable item in this manner. Apr 6 '15 at 15:21
  • In what manner? And does it work with true instead of false?
    – Marius
    Apr 6 '15 at 15:22
  • Yes it now occurs when the parameter is set to true. Curiously, when the total wishlist collection is iterated over and each product is added using the exact same functionality (except $collection = $wishlist->getItemCollection()->setVisibilityFilter()) it is added without an exception thrown. Apr 6 '15 at 15:30

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.