123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\Wishlist\Model;
- use Magento\Catalog\Api\ProductRepositoryInterface;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Framework\Serialize\Serializer\Json;
- use Magento\Wishlist\Model\ResourceModel\Item\CollectionFactory;
- use Magento\Wishlist\Model\ResourceModel\Wishlist as ResourceWishlist;
- use Magento\Wishlist\Model\ResourceModel\Wishlist\Collection;
- /**
- * Wishlist model
- *
- * @method int getShared()
- * @method \Magento\Wishlist\Model\Wishlist setShared(int $value)
- * @method string getSharingCode()
- * @method \Magento\Wishlist\Model\Wishlist setSharingCode(string $value)
- * @method string getUpdatedAt()
- * @method \Magento\Wishlist\Model\Wishlist setUpdatedAt(string $value)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @SuppressWarnings(PHPMD.TooManyFields)
- *
- * @api
- * @since 100.0.2
- */
- class Wishlist extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
- {
- /**
- * Cache tag
- */
- const CACHE_TAG = 'wishlist';
- /**
- * Prefix of model events names
- *
- * @var string
- */
- protected $_eventPrefix = 'wishlist';
- /**
- * Wishlist item collection
- *
- * @var \Magento\Wishlist\Model\ResourceModel\Item\Collection
- */
- protected $_itemCollection;
- /**
- * Store filter for wishlist
- *
- * @var \Magento\Store\Model\Store
- */
- protected $_store;
- /**
- * Shared store ids (website stores)
- *
- * @var array
- */
- protected $_storeIds;
- /**
- * Wishlist data
- *
- * @var \Magento\Wishlist\Helper\Data
- */
- protected $_wishlistData;
- /**
- * Catalog product
- *
- * @var \Magento\Catalog\Helper\Product
- */
- protected $_catalogProduct;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * @var \Magento\Framework\Stdlib\DateTime\DateTime
- */
- protected $_date;
- /**
- * @var ItemFactory
- */
- protected $_wishlistItemFactory;
- /**
- * @var CollectionFactory
- */
- protected $_wishlistCollectionFactory;
- /**
- * @var \Magento\Catalog\Model\ProductFactory
- */
- protected $_productFactory;
- /**
- * @var \Magento\Framework\Math\Random
- */
- protected $mathRandom;
- /**
- * @var \Magento\Framework\Stdlib\DateTime
- */
- protected $dateTime;
- /**
- * @var bool
- */
- protected $_useCurrentWebsite;
- /**
- * @var ProductRepositoryInterface
- */
- protected $productRepository;
- /**
- * @var Json
- */
- private $serializer;
- /**
- * Constructor
- *
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\Catalog\Helper\Product $catalogProduct
- * @param \Magento\Wishlist\Helper\Data $wishlistData
- * @param ResourceWishlist $resource
- * @param Collection $resourceCollection
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
- * @param ItemFactory $wishlistItemFactory
- * @param CollectionFactory $wishlistCollectionFactory
- * @param \Magento\Catalog\Model\ProductFactory $productFactory
- * @param \Magento\Framework\Math\Random $mathRandom
- * @param \Magento\Framework\Stdlib\DateTime $dateTime
- * @param ProductRepositoryInterface $productRepository
- * @param bool $useCurrentWebsite
- * @param array $data
- * @param Json|null $serializer
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- \Magento\Framework\Model\Context $context,
- \Magento\Framework\Registry $registry,
- \Magento\Catalog\Helper\Product $catalogProduct,
- \Magento\Wishlist\Helper\Data $wishlistData,
- ResourceWishlist $resource,
- Collection $resourceCollection,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Magento\Framework\Stdlib\DateTime\DateTime $date,
- ItemFactory $wishlistItemFactory,
- CollectionFactory $wishlistCollectionFactory,
- \Magento\Catalog\Model\ProductFactory $productFactory,
- \Magento\Framework\Math\Random $mathRandom,
- \Magento\Framework\Stdlib\DateTime $dateTime,
- ProductRepositoryInterface $productRepository,
- $useCurrentWebsite = true,
- array $data = [],
- Json $serializer = null
- ) {
- $this->_useCurrentWebsite = $useCurrentWebsite;
- $this->_catalogProduct = $catalogProduct;
- $this->_wishlistData = $wishlistData;
- $this->_storeManager = $storeManager;
- $this->_date = $date;
- $this->_wishlistItemFactory = $wishlistItemFactory;
- $this->_wishlistCollectionFactory = $wishlistCollectionFactory;
- $this->_productFactory = $productFactory;
- $this->mathRandom = $mathRandom;
- $this->dateTime = $dateTime;
- $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
- parent::__construct($context, $registry, $resource, $resourceCollection, $data);
- $this->productRepository = $productRepository;
- }
- /**
- * Load wishlist by customer id
- *
- * @param int $customerId
- * @param bool $create Create wishlist if don't exists
- * @return $this
- */
- public function loadByCustomerId($customerId, $create = false)
- {
- if ($customerId === null) {
- return $this;
- }
- $customerId = (int)$customerId;
- $customerIdFieldName = $this->_getResource()->getCustomerIdFieldName();
- $this->_getResource()->load($this, $customerId, $customerIdFieldName);
- if (!$this->getId() && $create) {
- $this->setCustomerId($customerId);
- $this->setSharingCode($this->_getSharingRandomCode());
- $this->save();
- }
- return $this;
- }
- /**
- * Retrieve wishlist name
- *
- * @return string
- */
- public function getName()
- {
- $name = $this->_getData('name');
- if ($name === null || !strlen($name)) {
- return $this->_wishlistData->getDefaultWishlistName();
- }
- return $name;
- }
- /**
- * Set random sharing code
- *
- * @return $this
- */
- public function generateSharingCode()
- {
- $this->setSharingCode($this->_getSharingRandomCode());
- return $this;
- }
- /**
- * Load by sharing code
- *
- * @param string $code
- * @return $this
- */
- public function loadByCode($code)
- {
- $this->_getResource()->load($this, $code, 'sharing_code');
- if (!$this->getShared()) {
- $this->setId(null);
- }
- return $this;
- }
- /**
- * Retrieve sharing code (random string)
- *
- * @return string
- */
- protected function _getSharingRandomCode()
- {
- return $this->mathRandom->getUniqueHash();
- }
- /**
- * Set date of last update for wishlist
- *
- * @return $this
- */
- public function beforeSave()
- {
- parent::beforeSave();
- $this->setUpdatedAt($this->_date->gmtDate());
- return $this;
- }
- /**
- * Save related items
- *
- * @return $this
- */
- public function afterSave()
- {
- parent::afterSave();
- if (null !== $this->_itemCollection) {
- $this->getItemCollection()->save();
- }
- return $this;
- }
- /**
- * Add catalog product object data to wishlist
- *
- * @param \Magento\Catalog\Model\Product $product
- * @param int $qty
- * @param bool $forciblySetQty
- *
- * @return Item
- */
- protected function _addCatalogProduct(\Magento\Catalog\Model\Product $product, $qty = 1, $forciblySetQty = false)
- {
- $item = null;
- foreach ($this->getItemCollection() as $_item) {
- if ($_item->representProduct($product)) {
- $item = $_item;
- break;
- }
- }
- if ($item === null) {
- $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(\Magento\Framework\Stdlib\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);
- }
- } else {
- $qty = $forciblySetQty ? $qty : $item->getQty() + $qty;
- $item->setQty($qty)->save();
- }
- $this->addItem($item);
- return $item;
- }
- /**
- * Retrieve wishlist item collection
- *
- * @return \Magento\Wishlist\Model\ResourceModel\Item\Collection
- */
- public function getItemCollection()
- {
- if ($this->_itemCollection === null) {
- $this->_itemCollection = $this->_wishlistCollectionFactory->create()->addWishlistFilter(
- $this
- )->addStoreFilter(
- $this->getSharedStoreIds()
- )->setVisibilityFilter();
- }
- return $this->_itemCollection;
- }
- /**
- * Retrieve wishlist item collection
- *
- * @param int $itemId
- * @return false|Item
- */
- public function getItem($itemId)
- {
- if (!$itemId) {
- return false;
- }
- return $this->getItemCollection()->getItemById($itemId);
- }
- /**
- * Adding item to wishlist
- *
- * @param Item $item
- * @return $this
- */
- public function addItem(Item $item)
- {
- $item->setWishlist($this);
- if (!$item->getId()) {
- $this->getItemCollection()->addItem($item);
- $this->_eventManager->dispatch('wishlist_add_item', ['item' => $item]);
- }
- return $this;
- }
- /**
- * Adds new product to wishlist.
- *
- * Returns new item or string on error.
- *
- * @param int|\Magento\Catalog\Model\Product $product
- * @param \Magento\Framework\DataObject|array|string|null $buyRequest
- * @param bool $forciblySetQty
- * @throws \Magento\Framework\Exception\LocalizedException
- * @return Item|string
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function addNewItem($product, $buyRequest = null, $forciblySetQty = false)
- {
- /*
- * Always load product, to ensure:
- * a) we have new instance and do not interfere with other products in wishlist
- * b) product has full set of attributes
- */
- if ($product instanceof \Magento\Catalog\Model\Product) {
- $productId = $product->getId();
- // Maybe force some store by wishlist internal properties
- $storeId = $product->hasWishlistStoreId() ? $product->getWishlistStoreId() : $product->getStoreId();
- } else {
- $productId = (int)$product;
- if (isset($buyRequest) && $buyRequest->getStoreId()) {
- $storeId = $buyRequest->getStoreId();
- } else {
- $storeId = $this->_storeManager->getStore()->getId();
- }
- }
- try {
- $product = $this->productRepository->getById($productId, false, $storeId);
- } catch (NoSuchEntityException $e) {
- throw new \Magento\Framework\Exception\LocalizedException(__('Cannot specify product.'));
- }
- if ($buyRequest instanceof \Magento\Framework\DataObject) {
- $_buyRequest = $buyRequest;
- } elseif (is_string($buyRequest)) {
- $isInvalidItemConfiguration = false;
- try {
- $buyRequestData = $this->serializer->unserialize($buyRequest);
- if (!is_array($buyRequestData)) {
- $isInvalidItemConfiguration = true;
- }
- } catch (\InvalidArgumentException $exception) {
- $isInvalidItemConfiguration = true;
- }
- if ($isInvalidItemConfiguration) {
- throw new \InvalidArgumentException('Invalid wishlist item configuration.');
- }
- $_buyRequest = new \Magento\Framework\DataObject($buyRequestData);
- } elseif (is_array($buyRequest)) {
- $_buyRequest = new \Magento\Framework\DataObject($buyRequest);
- } else {
- $_buyRequest = new \Magento\Framework\DataObject();
- }
- /* @var $product \Magento\Catalog\Model\Product */
- $cartCandidates = $product->getTypeInstance()->processConfiguration($_buyRequest, clone $product);
- /**
- * Error message
- */
- if (is_string($cartCandidates)) {
- return $cartCandidates;
- }
- /**
- * If prepare process return one object
- */
- if (!is_array($cartCandidates)) {
- $cartCandidates = [$cartCandidates];
- }
- $errors = [];
- $items = [];
- foreach ($cartCandidates as $candidate) {
- if ($candidate->getParentProductId()) {
- continue;
- }
- $candidate->setWishlistStoreId($storeId);
- $qty = $candidate->getQty() ? $candidate->getQty() : 1;
- // No null values as qty. Convert zero to 1.
- $item = $this->_addCatalogProduct($candidate, $qty, $forciblySetQty);
- $items[] = $item;
- // Collect errors instead of throwing first one
- if ($item->getHasError()) {
- $errors[] = $item->getMessage();
- }
- }
- $this->_eventManager->dispatch('wishlist_product_add_after', ['items' => $items]);
- return $item;
- }
- /**
- * Set customer id
- *
- * @param int $customerId
- * @return $this
- */
- public function setCustomerId($customerId)
- {
- return $this->setData($this->_getResource()->getCustomerIdFieldName(), $customerId);
- }
- /**
- * Retrieve customer id
- *
- * @return int
- */
- public function getCustomerId()
- {
- return $this->getData($this->_getResource()->getCustomerIdFieldName());
- }
- /**
- * Retrieve data for save
- *
- * @return array
- */
- public function getDataForSave()
- {
- $data = [];
- $data[$this->_getResource()->getCustomerIdFieldName()] = $this->getCustomerId();
- $data['shared'] = (int)$this->getShared();
- $data['sharing_code'] = $this->getSharingCode();
- return $data;
- }
- /**
- * Retrieve shared store ids for current website or all stores if $current is false
- *
- * @return array
- */
- public function getSharedStoreIds()
- {
- if ($this->_storeIds === null || !is_array($this->_storeIds)) {
- if ($this->_useCurrentWebsite) {
- $this->_storeIds = $this->getStore()->getWebsite()->getStoreIds();
- } else {
- $_storeIds = [];
- $stores = $this->_storeManager->getStores();
- foreach ($stores as $store) {
- $_storeIds[] = $store->getId();
- }
- $this->_storeIds = $_storeIds;
- }
- }
- return $this->_storeIds;
- }
- /**
- * Set shared store ids
- *
- * @param array $storeIds
- * @return $this
- */
- public function setSharedStoreIds($storeIds)
- {
- $this->_storeIds = (array)$storeIds;
- return $this;
- }
- /**
- * Retrieve wishlist store object
- *
- * @return \Magento\Store\Model\Store
- */
- public function getStore()
- {
- if ($this->_store === null) {
- $this->setStore($this->_storeManager->getStore());
- }
- return $this->_store;
- }
- /**
- * Set wishlist store
- *
- * @param \Magento\Store\Model\Store $store
- * @return $this
- */
- public function setStore($store)
- {
- $this->_store = $store;
- return $this;
- }
- /**
- * Retrieve wishlist items count
- *
- * @return int
- */
- public function getItemsCount()
- {
- return $this->getItemCollection()->count();
- }
- /**
- * Retrieve wishlist has salable item(s)
- *
- * @return bool
- */
- public function isSalable()
- {
- foreach ($this->getItemCollection() as $item) {
- if ($item->getProduct()->getIsSalable()) {
- return true;
- }
- }
- return false;
- }
- /**
- * Check customer is owner this wishlist
- *
- * @param int $customerId
- * @return bool
- */
- public function isOwner($customerId)
- {
- return $customerId == $this->getCustomerId();
- }
- /**
- * Update wishlist Item and set data from request
- *
- * The $params sets how current item configuration must be taken into account and additional options.
- * It's passed to \Magento\Catalog\Helper\Product->addParamsToBuyRequest() to compose resulting buyRequest.
- *
- * Basically it can hold
- * - 'current_config', \Magento\Framework\DataObject or array - current buyRequest
- * that configures product in this item, used to restore currently attached files
- * - 'files_prefix': string[a-z0-9_] - prefix that was added at frontend to names of file options (file inputs),
- * so they won't intersect with other submitted options
- *
- * For more options see \Magento\Catalog\Helper\Product->addParamsToBuyRequest()
- *
- * @param int|Item $itemId
- * @param \Magento\Framework\DataObject $buyRequest
- * @param null|array|\Magento\Framework\DataObject $params
- * @return $this
- * @throws \Magento\Framework\Exception\LocalizedException
- *
- * @see \Magento\Catalog\Helper\Product::addParamsToBuyRequest()
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function updateItem($itemId, $buyRequest, $params = null)
- {
- $item = null;
- if ($itemId instanceof Item) {
- $item = $itemId;
- $itemId = $item->getId();
- } else {
- $item = $this->getItem((int)$itemId);
- }
- if (!$item) {
- throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t specify a wish list item.'));
- }
- $product = $item->getProduct();
- $productId = $product->getId();
- if ($productId) {
- if (!$params) {
- $params = new \Magento\Framework\DataObject();
- } elseif (is_array($params)) {
- $params = new \Magento\Framework\DataObject($params);
- }
- $params->setCurrentConfig($item->getBuyRequest());
- $buyRequest = $this->_catalogProduct->addParamsToBuyRequest($buyRequest, $params);
- $product->setWishlistStoreId($item->getStoreId());
- $items = $this->getItemCollection();
- $isForceSetQuantity = true;
- foreach ($items as $_item) {
- /* @var $_item Item */
- if ($_item->getProductId() == $product->getId() && $_item->representProduct(
- $product
- ) && $_item->getId() != $item->getId()
- ) {
- // We do not add new wishlist item, but updating the existing one
- $isForceSetQuantity = false;
- }
- }
- $resultItem = $this->addNewItem($product, $buyRequest, $isForceSetQuantity);
- /**
- * Error message
- */
- if (is_string($resultItem)) {
- throw new \Magento\Framework\Exception\LocalizedException(__($resultItem));
- }
- if ($resultItem->getId() != $itemId) {
- if ($resultItem->getDescription() != $item->getDescription()) {
- $resultItem->setDescription($item->getDescription())->save();
- }
- $item->isDeleted(true);
- $this->setDataChanges(true);
- } else {
- $resultItem->setQty($buyRequest->getQty() * 1);
- $resultItem->setOrigData('qty', 0);
- }
- } else {
- throw new \Magento\Framework\Exception\LocalizedException(__('The product does not exist.'));
- }
- return $this;
- }
- /**
- * Return unique ID(s) for each object in system
- *
- * @return array
- */
- public function getIdentities()
- {
- $identities = [];
- if ($this->getId()) {
- $identities = [self::CACHE_TAG . '_' . $this->getId()];
- }
- return $identities;
- }
- }
|