Product.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Helper;
  7. use Magento\Catalog\Api\CategoryRepositoryInterface;
  8. use Magento\Catalog\Api\ProductRepositoryInterface;
  9. use Magento\Catalog\Model\Product as ModelProduct;
  10. use Magento\Framework\Exception\NoSuchEntityException;
  11. use Magento\Store\Model\Store;
  12. /**
  13. * Catalog category helper
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class Product extends \Magento\Framework\Url\Helper\Data
  17. {
  18. const XML_PATH_PRODUCT_URL_USE_CATEGORY = 'catalog/seo/product_use_categories';
  19. const XML_PATH_USE_PRODUCT_CANONICAL_TAG = 'catalog/seo/product_canonical_tag';
  20. const XML_PATH_AUTO_GENERATE_MASK = 'catalog/fields_masks';
  21. /**
  22. * Flag that shows if Magento has to check product to be saleable (enabled and/or inStock)
  23. *
  24. * @var boolean
  25. */
  26. protected $_skipSaleableCheck = false;
  27. /**
  28. * @var array
  29. */
  30. protected $_statuses;
  31. /**
  32. * @var mixed
  33. */
  34. protected $_priceBlock;
  35. /**
  36. * @var \Magento\Framework\View\Asset\Repository
  37. */
  38. protected $_assetRepo;
  39. /**
  40. * Core registry
  41. *
  42. * @var \Magento\Framework\Registry
  43. */
  44. protected $_coreRegistry;
  45. /**
  46. * @var \Magento\Catalog\Model\Attribute\Config
  47. */
  48. protected $_attributeConfig;
  49. /**
  50. * Catalog session
  51. *
  52. * @var \Magento\Catalog\Model\Session
  53. */
  54. protected $_catalogSession;
  55. /**
  56. * Invalidate product category indexer params
  57. *
  58. * @var array
  59. */
  60. protected $_reindexProductCategoryIndexerData;
  61. /**
  62. * Invalidate price indexer params
  63. *
  64. * @var array
  65. */
  66. protected $_reindexPriceIndexerData;
  67. /**
  68. * @var ProductRepositoryInterface
  69. */
  70. protected $productRepository;
  71. /**
  72. * @var CategoryRepositoryInterface
  73. */
  74. protected $categoryRepository;
  75. /**
  76. * @var \Magento\Store\Model\StoreManagerInterface
  77. */
  78. protected $_storeManager;
  79. /**
  80. * @param \Magento\Framework\App\Helper\Context $context
  81. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  82. * @param \Magento\Catalog\Model\Session $catalogSession
  83. * @param \Magento\Framework\View\Asset\Repository $assetRepo
  84. * @param \Magento\Framework\Registry $coreRegistry
  85. * @param \Magento\Catalog\Model\Attribute\Config $attributeConfig
  86. * @param array $reindexPriceIndexerData
  87. * @param array $reindexProductCategoryIndexerData
  88. * @param ProductRepositoryInterface $productRepository
  89. * @param CategoryRepositoryInterface $categoryRepository
  90. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  91. */
  92. public function __construct(
  93. \Magento\Framework\App\Helper\Context $context,
  94. \Magento\Store\Model\StoreManagerInterface $storeManager,
  95. \Magento\Catalog\Model\Session $catalogSession,
  96. \Magento\Framework\View\Asset\Repository $assetRepo,
  97. \Magento\Framework\Registry $coreRegistry,
  98. \Magento\Catalog\Model\Attribute\Config $attributeConfig,
  99. $reindexPriceIndexerData,
  100. $reindexProductCategoryIndexerData,
  101. ProductRepositoryInterface $productRepository,
  102. CategoryRepositoryInterface $categoryRepository
  103. ) {
  104. $this->_catalogSession = $catalogSession;
  105. $this->_attributeConfig = $attributeConfig;
  106. $this->_coreRegistry = $coreRegistry;
  107. $this->_assetRepo = $assetRepo;
  108. $this->_reindexPriceIndexerData = $reindexPriceIndexerData;
  109. $this->productRepository = $productRepository;
  110. $this->categoryRepository = $categoryRepository;
  111. $this->_reindexProductCategoryIndexerData = $reindexProductCategoryIndexerData;
  112. $this->_storeManager = $storeManager;
  113. parent::__construct($context);
  114. }
  115. /**
  116. * Retrieve data for price indexer update
  117. *
  118. * @param \Magento\Catalog\Model\Product|array $data
  119. * @return bool
  120. */
  121. public function isDataForPriceIndexerWasChanged($data)
  122. {
  123. if ($data instanceof ModelProduct) {
  124. foreach ($this->_reindexPriceIndexerData['byDataResult'] as $param) {
  125. if ($data->getData($param)) {
  126. return true;
  127. }
  128. }
  129. foreach ($this->_reindexPriceIndexerData['byDataChange'] as $param) {
  130. if ($data->dataHasChangedFor($param)) {
  131. return true;
  132. }
  133. }
  134. } elseif (is_array($data)) {
  135. foreach ($this->_reindexPriceIndexerData['byDataChange'] as $param) {
  136. if (isset($data[$param])) {
  137. return true;
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. /**
  144. * Retrieve data for product category indexer update
  145. *
  146. * @param \Magento\Catalog\Model\Product $data
  147. * @return bool
  148. */
  149. public function isDataForProductCategoryIndexerWasChanged(\Magento\Catalog\Model\Product $data)
  150. {
  151. foreach ($this->_reindexProductCategoryIndexerData['byDataChange'] as $param) {
  152. if ($data->dataHasChangedFor($param)) {
  153. return true;
  154. }
  155. }
  156. return false;
  157. }
  158. /**
  159. * Retrieve product view page url
  160. *
  161. * @param int|ModelProduct $product
  162. * @return string|bool
  163. */
  164. public function getProductUrl($product)
  165. {
  166. if ($product instanceof ModelProduct) {
  167. return $product->getProductUrl();
  168. } elseif (is_numeric($product)) {
  169. return $this->productRepository->getById($product)->getProductUrl();
  170. }
  171. return false;
  172. }
  173. /**
  174. * Retrieve product price
  175. *
  176. * @param ModelProduct $product
  177. * @return float
  178. */
  179. public function getPrice($product)
  180. {
  181. return $product->getPrice();
  182. }
  183. /**
  184. * Retrieve product final price
  185. *
  186. * @param ModelProduct $product
  187. * @return float
  188. */
  189. public function getFinalPrice($product)
  190. {
  191. return $product->getFinalPrice();
  192. }
  193. /**
  194. * Retrieve base image url
  195. *
  196. * @param ModelProduct|\Magento\Framework\DataObject $product
  197. * @return string|bool
  198. */
  199. public function getImageUrl($product)
  200. {
  201. $url = false;
  202. $attribute = $product->getResource()->getAttribute('image');
  203. if (!$product->getImage()) {
  204. $url = $this->_assetRepo->getUrl('Magento_Catalog::images/product/placeholder/image.jpg');
  205. } elseif ($attribute) {
  206. $url = $attribute->getFrontend()->getUrl($product);
  207. }
  208. return $url;
  209. }
  210. /**
  211. * Retrieve small image url
  212. *
  213. * @param ModelProduct|\Magento\Framework\DataObject $product
  214. * @return string|bool
  215. */
  216. public function getSmallImageUrl($product)
  217. {
  218. $url = false;
  219. $attribute = $product->getResource()->getAttribute('small_image');
  220. if (!$product->getSmallImage()) {
  221. $url = $this->_assetRepo->getUrl('Magento_Catalog::images/product/placeholder/small_image.jpg');
  222. } elseif ($attribute) {
  223. $url = $attribute->getFrontend()->getUrl($product);
  224. }
  225. return $url;
  226. }
  227. /**
  228. * Retrieve thumbnail image url
  229. *
  230. * @param ModelProduct|\Magento\Framework\DataObject $product
  231. * @return string|bool
  232. */
  233. public function getThumbnailUrl($product)
  234. {
  235. $url = false;
  236. $attribute = $product->getResource()->getAttribute('thumbnail');
  237. if (!$product->getThumbnail()) {
  238. $url = $this->_assetRepo->getUrl('Magento_Catalog::images/product/placeholder/thumbnail.jpg');
  239. } elseif ($attribute) {
  240. $url = $attribute->getFrontend()->getUrl($product);
  241. }
  242. return $url;
  243. }
  244. /**
  245. * @param ModelProduct $product
  246. * @return string
  247. */
  248. public function getEmailToFriendUrl($product)
  249. {
  250. $categoryId = null;
  251. $category = $this->_coreRegistry->registry('current_category');
  252. if ($category) {
  253. $categoryId = $category->getId();
  254. }
  255. return $this->_getUrl('sendfriend/product/send', ['id' => $product->getId(), 'cat_id' => $categoryId]);
  256. }
  257. /**
  258. * @return array
  259. */
  260. public function getStatuses()
  261. {
  262. if (null === $this->_statuses) {
  263. $this->_statuses = [];
  264. }
  265. return $this->_statuses;
  266. }
  267. /**
  268. * Check if a product can be shown
  269. *
  270. * @param ModelProduct|int $product
  271. * @param string $where
  272. * @return bool
  273. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  274. */
  275. public function canShow($product, $where = 'catalog')
  276. {
  277. if (is_int($product)) {
  278. try {
  279. $product = $this->productRepository->getById($product);
  280. } catch (NoSuchEntityException $e) {
  281. return false;
  282. }
  283. } else {
  284. if (!$product->getId()) {
  285. return false;
  286. }
  287. }
  288. return $product->isVisibleInCatalog() && $product->isVisibleInSiteVisibility();
  289. }
  290. /**
  291. * Check if <link rel="canonical"> can be used for product
  292. *
  293. * @param null|string|bool|int|Store $store
  294. * @return bool
  295. */
  296. public function canUseCanonicalTag($store = null)
  297. {
  298. return $this->scopeConfig->getValue(
  299. self::XML_PATH_USE_PRODUCT_CANONICAL_TAG,
  300. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  301. $store
  302. );
  303. }
  304. /**
  305. * Return information array of product attribute input types
  306. * Only a small number of settings returned, so we won't break anything in current data flow
  307. * As soon as development process goes on we need to add there all possible settings
  308. *
  309. * @param string $inputType
  310. * @return array
  311. */
  312. public function getAttributeInputTypes($inputType = null)
  313. {
  314. /**
  315. * @todo specify there all relations for properties depending on input type
  316. */
  317. $inputTypes = [
  318. 'multiselect' => ['backend_model' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class],
  319. 'boolean' => ['source_model' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class],
  320. ];
  321. if ($inputType === null) {
  322. return $inputTypes;
  323. } else {
  324. if (isset($inputTypes[$inputType])) {
  325. return $inputTypes[$inputType];
  326. }
  327. }
  328. return [];
  329. }
  330. /**
  331. * Return default attribute backend model by input type
  332. *
  333. * @param string $inputType
  334. * @return string|null
  335. */
  336. public function getAttributeBackendModelByInputType($inputType)
  337. {
  338. $inputTypes = $this->getAttributeInputTypes();
  339. if (!empty($inputTypes[$inputType]['backend_model'])) {
  340. return $inputTypes[$inputType]['backend_model'];
  341. }
  342. return null;
  343. }
  344. /**
  345. * Return default attribute source model by input type
  346. *
  347. * @param string $inputType
  348. * @return string|null
  349. */
  350. public function getAttributeSourceModelByInputType($inputType)
  351. {
  352. $inputTypes = $this->getAttributeInputTypes();
  353. if (!empty($inputTypes[$inputType]['source_model'])) {
  354. return $inputTypes[$inputType]['source_model'];
  355. }
  356. return null;
  357. }
  358. /**
  359. * Inits product to be used for product controller actions and layouts
  360. * $params can have following data:
  361. * 'category_id' - id of category to check and append to product as current.
  362. * If empty (except FALSE) - will be guessed (e.g. from last visited) to load as current.
  363. *
  364. * @param int $productId
  365. * @param \Magento\Framework\App\Action\Action $controller
  366. * @param \Magento\Framework\DataObject $params
  367. *
  368. * @return bool|ModelProduct
  369. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  370. * @SuppressWarnings(PHPMD.NPathComplexity)
  371. */
  372. public function initProduct($productId, $controller, $params = null)
  373. {
  374. // Prepare data for routine
  375. if (!$params) {
  376. $params = new \Magento\Framework\DataObject();
  377. }
  378. // Init and load product
  379. $this->_eventManager->dispatch(
  380. 'catalog_controller_product_init_before',
  381. ['controller_action' => $controller, 'params' => $params]
  382. );
  383. if (!$productId) {
  384. return false;
  385. }
  386. try {
  387. $product = $this->productRepository->getById($productId, false, $this->_storeManager->getStore()->getId());
  388. } catch (NoSuchEntityException $e) {
  389. return false;
  390. }
  391. if (!$this->canShow($product)) {
  392. return false;
  393. }
  394. if (!in_array($this->_storeManager->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
  395. return false;
  396. }
  397. // Load product current category
  398. $categoryId = $params->getCategoryId();
  399. if (!$categoryId && $categoryId !== false) {
  400. $lastId = $this->_catalogSession->getLastVisitedCategoryId();
  401. if ($product->canBeShowInCategory($lastId)) {
  402. $categoryId = $lastId;
  403. }
  404. } elseif (!$product->canBeShowInCategory($categoryId)) {
  405. $categoryId = null;
  406. }
  407. if ($categoryId) {
  408. try {
  409. $category = $this->categoryRepository->get($categoryId);
  410. } catch (NoSuchEntityException $e) {
  411. $category = null;
  412. }
  413. if ($category) {
  414. $product->setCategory($category);
  415. $this->_coreRegistry->register('current_category', $category);
  416. }
  417. }
  418. // Register current data and dispatch final events
  419. $this->_coreRegistry->register('current_product', $product);
  420. $this->_coreRegistry->register('product', $product);
  421. try {
  422. $this->_eventManager->dispatch(
  423. 'catalog_controller_product_init_after',
  424. ['product' => $product, 'controller_action' => $controller]
  425. );
  426. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  427. $this->_logger->critical($e);
  428. return false;
  429. }
  430. return $product;
  431. }
  432. /**
  433. * Prepares product options by buyRequest: retrieves values and assigns them as default.
  434. * Also parses and adds product management related values - e.g. qty
  435. *
  436. * @param ModelProduct $product
  437. * @param \Magento\Framework\DataObject $buyRequest
  438. * @return Product
  439. */
  440. public function prepareProductOptions($product, $buyRequest)
  441. {
  442. $optionValues = $product->processBuyRequest($buyRequest);
  443. $optionValues->setQty($buyRequest->getQty());
  444. $product->setPreconfiguredValues($optionValues);
  445. return $this;
  446. }
  447. /**
  448. * Process $buyRequest and sets its options before saving configuration to some product item.
  449. * This method is used to attach additional parameters to processed buyRequest.
  450. *
  451. * $params holds parameters of what operation must be performed:
  452. * - 'current_config', \Magento\Framework\DataObject or array - current buyRequest
  453. * that configures product in this item, used to restore currently attached files
  454. * - 'files_prefix': string[a-z0-9_] - prefix that was added at frontend to names of file inputs,
  455. * so they won't intersect with other submitted options
  456. *
  457. * @param \Magento\Framework\DataObject|array $buyRequest
  458. * @param \Magento\Framework\DataObject|array $params
  459. * @return \Magento\Framework\DataObject
  460. */
  461. public function addParamsToBuyRequest($buyRequest, $params)
  462. {
  463. if (is_array($buyRequest)) {
  464. $buyRequest = new \Magento\Framework\DataObject($buyRequest);
  465. }
  466. if (is_array($params)) {
  467. $params = new \Magento\Framework\DataObject($params);
  468. }
  469. // Ensure that currentConfig goes as \Magento\Framework\DataObject - for easier work with it later
  470. $currentConfig = $params->getCurrentConfig();
  471. if ($currentConfig) {
  472. if (is_array($currentConfig)) {
  473. $params->setCurrentConfig(new \Magento\Framework\DataObject($currentConfig));
  474. } elseif (!$currentConfig instanceof \Magento\Framework\DataObject) {
  475. $params->unsCurrentConfig();
  476. }
  477. }
  478. /*
  479. * Notice that '_processing_params' must always be object to protect processing forged requests
  480. * where '_processing_params' comes in $buyRequest as array from user input
  481. */
  482. $processingParams = $buyRequest->getData('_processing_params');
  483. if (!$processingParams || !$processingParams instanceof \Magento\Framework\DataObject) {
  484. $processingParams = new \Magento\Framework\DataObject();
  485. $buyRequest->setData('_processing_params', $processingParams);
  486. }
  487. $processingParams->addData($params->getData());
  488. return $buyRequest;
  489. }
  490. /**
  491. * Set flag that shows if Magento has to check product to be saleable (enabled and/or inStock)
  492. *
  493. * For instance, during order creation in the backend admin has ability to add any products to order
  494. *
  495. * @param bool $skipSaleableCheck
  496. * @return Product
  497. */
  498. public function setSkipSaleableCheck($skipSaleableCheck = false)
  499. {
  500. $this->_skipSaleableCheck = $skipSaleableCheck;
  501. return $this;
  502. }
  503. /**
  504. * Get flag that shows if Magento has to check product to be saleable (enabled and/or inStock)
  505. *
  506. * @return bool
  507. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  508. */
  509. public function getSkipSaleableCheck()
  510. {
  511. return $this->_skipSaleableCheck;
  512. }
  513. /**
  514. * Get masks for auto generation of fields
  515. *
  516. * @return mixed
  517. */
  518. public function getFieldsAutogenerationMasks()
  519. {
  520. return $this->scopeConfig->getValue(Product::XML_PATH_AUTO_GENERATE_MASK, 'default');
  521. }
  522. /**
  523. * Retrieve list of attributes that allowed for autogeneration
  524. *
  525. * @return array
  526. */
  527. public function getAttributesAllowedForAutogeneration()
  528. {
  529. return $this->_attributeConfig->getAttributeNames('used_in_autogeneration');
  530. }
  531. }