<?php


namespace Magetop\Advancepreview\Model\ReWrite;


use Magento\Catalog\Model\Product;
use Magento\Framework\Stdlib\DateTime;
class Wishlist extends \Magento\Wishlist\Model\Wishlist
{

    protected function _addCatalogProduct(Product $product, $qty = 1, $forciblySetQty = false)
    {
        $item = null;
        foreach ($this->getItemCollection() as $_item) {
            if ($_item->representProduct($product)) {
                $item = $_item;
                break;
            }
        }
        $storeId = $product->hasWishlistStoreId() ? $product->getWishlistStoreId() : $this->getStore()->getId();
        $item = $this->_wishlistItemFactory->create();
        $item->setProductId($product->getId());
        $item->setWishlistId($this->getId());
        $item->setAddedAt((new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT));
        $item->setStoreId($storeId);
        $item->setOptions($product->getCustomOptions());
        $item->setProduct($product);
        $item->setQty($qty);
        $item->save();
        if ($item->getId()) {
            $this->getItemCollection()->addItem($item);
        }
        $this->addItem($item);
        return $item;
    }
}
