0

I've created Guest Wishlist and now I am trying to add products from cookie to a Customer Wishlist.

Problem is that if I observe 'customer_register_success' wishlist is not created until new customer is fully logged in.

If I observe 'customer_login' then problem is that I want to add products from cookie to a Wishlist ONLY if it is a new registered customer.

Not sure how to get newly registered customer wishlist?

Here is my observer ($list is array of product Ids, from a cookie named 'guestwishlist':

namespace Vendor\GuestWishlist\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Wishlist\Model\WishlistFactory;

class CreateAccount implements ObserverInterface
{
    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected $productRepository;

    /** @var WishlistFactory */
    protected $wishlistFactory;

    protected $_helper;

    public function __construct(
        WishlistFactory $wishlistFactory,
        \Vendor\GuestWishlist\Helper\Data $helper,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    ) {
        $this->wishlistFactory = $wishlistFactory;
        $this->_helper = $helper;
        $this->productRepository = $productRepository;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customer = $observer->getEvent()->getData('customer');
        $customerId = $customer->getId();

        $list = $this->_helper->getGuestWishlist();

        if(isset($list)){

            foreach ($list as $productId){

                $product = $this->productRepository->getById($productId);

                $wishlist = $this->wishlistFactory->create()->loadByCustomerId($customerId);
                $wishlist->addNewItem($productId);
                $wishlist->save();

            }
        }
    }
}

Error that shows is 'We can\'t specify a wish list.' when I observe event 'customer_login'

0

So, of course we can think dipper and find really native solution to your task, but lets me provide you fast work around solution.

You can just put the flag at the customer_register_success event which will say "Hello, i'm new customer at this store". You can use for example your helper to set this flag, like

$this->_helper->isNewCustomer = true;.

It's seems like good workaround, but if you need really nice solution could you please upload your extension code to the GitHub and I will try to find any better way to solve this task.

1
  • Thank you, this is exactly what I had in mind. It does look like a quick one. I will try it and let you know. If no luck I will give you access to git. Thank you.
    – iva
    Apr 11 '17 at 10:50

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.