2

This question has been asked before over Here but no answers were given. I have got the same task and am bit stuck on sharing.

Basically, i have to share the wishlist on Facebook. Just sharing link isn't enough as the wishlist can't be access without customer login.

I also tried sharing the wishlist using share URL which i retrieved using below code. But it is not working:

<?php echo Mage::getUrl('*/shared/index', array('code' => $this->getWishlistInstance()->getSharingCode())) ?>

Can anyone give an idea regarding how this can be achieved or clue from where should i start.

  • Would removing the logged in restriction work for you? – Marius Jan 7 '15 at 13:26
  • Arrr...never mind. I've re-read the question. I misunderstood something the first time. Sorry. – Marius Jan 7 '15 at 13:28
  • okay.. i am stucked finding the way across it.. Please let me know if you have some idea in your mind – Dexter Jan 7 '15 at 13:34
1

I did something like that, you have to save your wishlist before sharing the link.

So I overwrite the IndexController in the Wishlist to add a function that saves the wishlist.

Copy the

code/core/Mage/Wishlist/controllers/IndexController.php

to

code/local/Mage/Wishlist/controller/IndexController.php

and at the end paste this function (is the update function with some modifications)

public function sharedirectAction()
    {
        if (!$this->_validateFormKey()) {
            return $this->_redirect('*/*/');
        }

        $wishlist = $this->_getWishlist();
        if (!$wishlist) {
            return $this->norouteAction();
        }

        $post = $this->getRequest()->getPost();
        if($post && isset($post['description']) && is_array($post['description'])) {
            $updatedItems = 0;

            foreach ($post['description'] as $itemId => $description) {
                $item = Mage::getModel('wishlist/item')->load($itemId);
                if ($item->getWishlistId() != $wishlist->getId()) {
                    continue;
                }

                // Extract new values
                $description = (string) $description;
                if (!strlen($description)) {
                    $description = $item->getDescription();
                }

                $qty = null;
                if (isset($post['qty'][$itemId])) {
                    $qty = $this->_processLocalizedQty($post['qty'][$itemId]);
                }
                if (is_null($qty)) {
                    $qty = $item->getQty();
                    if (!$qty) {
                        $qty = 1;
                    }
                } elseif (0 == $qty) {
                    try {
                        $item->delete();
                    } catch (Exception $e) {
                        Mage::logException($e);
                        Mage::getSingleton('customer/session')->addError(
                            $this->__('Can\'t delete item from wishlist')
                        );
                    }
                }

                // Check that we need to save
                if (($item->getDescription() == $description) && ($item->getQty() == $qty)) {
                    continue;
                }
                try {
                    $item->setDescription($description)
                        ->setQty($qty)
                        ->save();
                    $updatedItems++;
                } catch (Exception $e) {
                    Mage::getSingleton('customer/session')->addError(
                        $this->__('Can\'t save description %s', Mage::helper('core')->htmlEscape($description))
                    );
                }
            }

            // save wishlist model for setting date of last update
            if ($updatedItems) {
                try {
                    $wishlist->save();
                    Mage::helper('wishlist')->calculate();
                }
                catch (Exception $e) {
                    Mage::getSingleton('customer/session')->addError($this->__('Can\'t update wishlist'));
                }
            }

        }

        try {
            $customer = Mage::getSingleton('customer/session')->getCustomer();

            $sharingCode = $wishlist->getSharingCode();

            $wishlist->setShared(1);
            $wishlist->save();
            $this->_redirect('*//*', array('wishlist_id' => $wishlist->getId()));
        }
        catch (Exception $e) {
            Mage::getSingleton('wishlist/session')->addError($e->getMessage());
        }
    }

and now in your view.phtml add this jquery code, this add the action that we just create to your form and then submit it.

jQuery(document).ready(function(){
           var shareURL = '<?php echo Mage::getUrl('*/shared/index', array('code' => $this->getWishlistInstance()->getSharingCode())) ?>';
            var fbUrl = "javascript:popWin('https://www.facebook.com/sharer/sharer.php?u="+shareURL+"', 'facebook', 'width=640,height=480,left=0,top=0,location=no,status=yes,scrollbars=yes,resizable=yes');";
            jQuery('button.btn-share-facebook').attr("onclick", fbUrl); 
            jQuery('button.btn-share-facebook').click(function(e){
                e.preventDefault();
                jQuery('#wishlist-view-form').attr("action","<?php echo $this->getUrl('*/*/sharedirect') ?>");
                jQuery('#wishlist-view-form').submit();
            });
        });

I don't know if is the best way, but this works for me.

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.