Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Attributes
Product Class Reference
Inheritance diagram for Product:
Data AbstractHelper

Public Member Functions

 __construct (\Magento\Framework\App\Helper\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Model\Session $catalogSession, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Framework\Registry $coreRegistry, \Magento\Catalog\Model\Attribute\Config $attributeConfig, $reindexPriceIndexerData, $reindexProductCategoryIndexerData, ProductRepositoryInterface $productRepository, CategoryRepositoryInterface $categoryRepository)
 
 isDataForPriceIndexerWasChanged ($data)
 
 isDataForProductCategoryIndexerWasChanged (\Magento\Catalog\Model\Product $data)
 
 getProductUrl ($product)
 
 getPrice ($product)
 
 getFinalPrice ($product)
 
 getImageUrl ($product)
 
 getSmallImageUrl ($product)
 
 getThumbnailUrl ($product)
 
 getEmailToFriendUrl ($product)
 
 getStatuses ()
 
 canShow ($product, $where='catalog')
 
 canUseCanonicalTag ($store=null)
 
 getAttributeInputTypes ($inputType=null)
 
 getAttributeBackendModelByInputType ($inputType)
 
 getAttributeSourceModelByInputType ($inputType)
 
 initProduct ($productId, $controller, $params=null)
 
 prepareProductOptions ($product, $buyRequest)
 
 addParamsToBuyRequest ($buyRequest, $params)
 
 setSkipSaleableCheck ($skipSaleableCheck=false)
 
 getSkipSaleableCheck ()
 
 getFieldsAutogenerationMasks ()
 
 getAttributesAllowedForAutogeneration ()
 
- Public Member Functions inherited from Data
 getCurrentBase64Url ()
 
 getEncodedUrl ($url=null)
 
 addRequestParam ($url, $param)
 
 removeRequestParam ($url, $paramKey, $caseSensitive=false)
 
- Public Member Functions inherited from AbstractHelper
 __construct (Context $context)
 
 isModuleOutputEnabled ($moduleName=null)
 

Data Fields

const XML_PATH_PRODUCT_URL_USE_CATEGORY = 'catalog/seo/product_use_categories'
 
const XML_PATH_USE_PRODUCT_CANONICAL_TAG = 'catalog/seo/product_canonical_tag'
 
const XML_PATH_AUTO_GENERATE_MASK = 'catalog/fields_masks'
 

Protected Attributes

 $_skipSaleableCheck = false
 
 $_statuses
 
 $_priceBlock
 
 $_assetRepo
 
 $_coreRegistry
 
 $_attributeConfig
 
 $_catalogSession
 
 $_reindexProductCategoryIndexerData
 
 $_reindexPriceIndexerData
 
 $productRepository
 
 $categoryRepository
 
 $_storeManager
 
- Protected Attributes inherited from AbstractHelper
 $_moduleName
 
 $_request
 
 $_moduleManager
 
 $_logger
 
 $_urlBuilder
 
 $_httpHeader
 
 $_eventManager
 
 $_remoteAddress
 
 $urlEncoder
 
 $urlDecoder
 
 $scopeConfig
 
 $_cacheConfig
 

Additional Inherited Members

- Protected Member Functions inherited from AbstractHelper
 _getRequest ()
 
 _getModuleName ()
 
 _getUrl ($route, $params=[])
 

Detailed Description

Catalog category helper @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 18 of file Product.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\App\Helper\Context  $context,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Catalog\Model\Session  $catalogSession,
\Magento\Framework\View\Asset\Repository  $assetRepo,
\Magento\Framework\Registry  $coreRegistry,
\Magento\Catalog\Model\Attribute\Config  $attributeConfig,
  $reindexPriceIndexerData,
  $reindexProductCategoryIndexerData,
ProductRepositoryInterface  $productRepository,
CategoryRepositoryInterface  $categoryRepository 
)
Parameters
\Magento\Framework\App\Helper\Context$context
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Catalog\Model\Session$catalogSession
\Magento\Framework\View\Asset\Repository$assetRepo
\Magento\Framework\Registry$coreRegistry
\Magento\Catalog\Model\Attribute\Config$attributeConfig
array$reindexPriceIndexerData
array$reindexProductCategoryIndexerData
ProductRepositoryInterface$productRepository
CategoryRepositoryInterface$categoryRepository@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 109 of file Product.php.

120  {
121  $this->_catalogSession = $catalogSession;
122  $this->_attributeConfig = $attributeConfig;
123  $this->_coreRegistry = $coreRegistry;
124  $this->_assetRepo = $assetRepo;
125  $this->_reindexPriceIndexerData = $reindexPriceIndexerData;
126  $this->productRepository = $productRepository;
127  $this->categoryRepository = $categoryRepository;
128  $this->_reindexProductCategoryIndexerData = $reindexProductCategoryIndexerData;
129  $this->_storeManager = $storeManager;
130  parent::__construct($context);
131  }
$storeManager

Member Function Documentation

◆ addParamsToBuyRequest()

addParamsToBuyRequest (   $buyRequest,
  $params 
)

Process $buyRequest and sets its options before saving configuration to some product item. This method is used to attach additional parameters to processed buyRequest.

$params holds parameters of what operation must be performed:

  • '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 inputs, so they won't intersect with other submitted options
Parameters
\Magento\Framework\DataObject | array$buyRequest
\Magento\Framework\DataObject | array$params
Returns
\Magento\Framework\DataObject

Definition at line 508 of file Product.php.

509  {
510  if (is_array($buyRequest)) {
511  $buyRequest = new \Magento\Framework\DataObject($buyRequest);
512  }
513  if (is_array($params)) {
514  $params = new \Magento\Framework\DataObject($params);
515  }
516 
517  // Ensure that currentConfig goes as \Magento\Framework\DataObject - for easier work with it later
518  $currentConfig = $params->getCurrentConfig();
519  if ($currentConfig) {
520  if (is_array($currentConfig)) {
521  $params->setCurrentConfig(new \Magento\Framework\DataObject($currentConfig));
522  } elseif (!$currentConfig instanceof \Magento\Framework\DataObject) {
523  $params->unsCurrentConfig();
524  }
525  }
526 
527  /*
528  * Notice that '_processing_params' must always be object to protect processing forged requests
529  * where '_processing_params' comes in $buyRequest as array from user input
530  */
531  $processingParams = $buyRequest->getData('_processing_params');
532  if (!$processingParams || !$processingParams instanceof \Magento\Framework\DataObject) {
533  $processingParams = new \Magento\Framework\DataObject();
534  $buyRequest->setData('_processing_params', $processingParams);
535  }
536  $processingParams->addData($params->getData());
537 
538  return $buyRequest;
539  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ canShow()

canShow (   $product,
  $where = 'catalog' 
)

Check if a product can be shown

Parameters
ModelProduct | int$product
string$where
Returns
bool @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 304 of file Product.php.

305  {
306  if (is_int($product)) {
307  try {
308  $product = $this->productRepository->getById($product);
309  } catch (NoSuchEntityException $e) {
310  return false;
311  }
312  } else {
313  if (!$product->getId()) {
314  return false;
315  }
316  }
317  return $product->isVisibleInCatalog() && $product->isVisibleInSiteVisibility();
318  }

◆ canUseCanonicalTag()

canUseCanonicalTag (   $store = null)

Check if <link rel="canonical"> can be used for product

Parameters
null | string | bool | int | Store$store
Returns
bool

Definition at line 326 of file Product.php.

327  {
328  return $this->scopeConfig->getValue(
329  self::XML_PATH_USE_PRODUCT_CANONICAL_TAG,
331  $store
332  );
333  }

◆ getAttributeBackendModelByInputType()

getAttributeBackendModelByInputType (   $inputType)

Return default attribute backend model by input type

Parameters
string$inputType
Returns
string|null

Definition at line 369 of file Product.php.

370  {
371  $inputTypes = $this->getAttributeInputTypes();
372  if (!empty($inputTypes[$inputType]['backend_model'])) {
373  return $inputTypes[$inputType]['backend_model'];
374  }
375  return null;
376  }
getAttributeInputTypes($inputType=null)
Definition: Product.php:343

◆ getAttributeInputTypes()

getAttributeInputTypes (   $inputType = null)

Return information array of product attribute input types Only a small number of settings returned, so we won't break anything in current data flow As soon as development process goes on we need to add there all possible settings

Parameters
string$inputType
Returns
array
Todo:
specify there all relations for properties depending on input type

Definition at line 343 of file Product.php.

344  {
348  $inputTypes = [
349  'multiselect' => ['backend_model' => \Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend::class],
350  'boolean' => ['source_model' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class],
351  ];
352 
353  if ($inputType === null) {
354  return $inputTypes;
355  } else {
356  if (isset($inputTypes[$inputType])) {
357  return $inputTypes[$inputType];
358  }
359  }
360  return [];
361  }

◆ getAttributesAllowedForAutogeneration()

getAttributesAllowedForAutogeneration ( )

Retrieve list of attributes that allowed for autogeneration

Returns
array

Definition at line 581 of file Product.php.

582  {
583  return $this->_attributeConfig->getAttributeNames('used_in_autogeneration');
584  }

◆ getAttributeSourceModelByInputType()

getAttributeSourceModelByInputType (   $inputType)

Return default attribute source model by input type

Parameters
string$inputType
Returns
string|null

Definition at line 384 of file Product.php.

385  {
386  $inputTypes = $this->getAttributeInputTypes();
387  if (!empty($inputTypes[$inputType]['source_model'])) {
388  return $inputTypes[$inputType]['source_model'];
389  }
390  return null;
391  }
getAttributeInputTypes($inputType=null)
Definition: Product.php:343

◆ getEmailToFriendUrl()

getEmailToFriendUrl (   $product)
Parameters
ModelProduct$product
Returns
string

Definition at line 274 of file Product.php.

275  {
276  $categoryId = null;
277  $category = $this->_coreRegistry->registry('current_category');
278  if ($category) {
279  $categoryId = $category->getId();
280  }
281  return $this->_getUrl('sendfriend/product/send', ['id' => $product->getId(), 'cat_id' => $categoryId]);
282  }

◆ getFieldsAutogenerationMasks()

getFieldsAutogenerationMasks ( )

Get masks for auto generation of fields

Returns
mixed

Definition at line 571 of file Product.php.

572  {
573  return $this->scopeConfig->getValue(Product::XML_PATH_AUTO_GENERATE_MASK, 'default');
574  }

◆ getFinalPrice()

getFinalPrice (   $product)

Retrieve product final price

Parameters
ModelProduct$product
Returns
float

Definition at line 211 of file Product.php.

212  {
213  return $product->getFinalPrice();
214  }

◆ getImageUrl()

getImageUrl (   $product)

Retrieve base image url

Parameters
ModelProduct | \Magento\Framework\DataObject$product
Returns
string|bool

Definition at line 222 of file Product.php.

223  {
224  $url = false;
225  $attribute = $product->getResource()->getAttribute('image');
226  if (!$product->getImage()) {
227  $url = $this->_assetRepo->getUrl('Magento_Catalog::images/product/placeholder/image.jpg');
228  } elseif ($attribute) {
229  $url = $attribute->getFrontend()->getUrl($product);
230  }
231  return $url;
232  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ getPrice()

getPrice (   $product)

Retrieve product price

Parameters
ModelProduct$product
Returns
float

Definition at line 200 of file Product.php.

201  {
202  return $product->getPrice();
203  }

◆ getProductUrl()

getProductUrl (   $product)

Retrieve product view page url

Parameters
int | ModelProduct$product
Returns
string|bool

Definition at line 184 of file Product.php.

185  {
186  if ($product instanceof ModelProduct) {
187  return $product->getProductUrl();
188  } elseif (is_numeric($product)) {
189  return $this->productRepository->getById($product)->getProductUrl();
190  }
191  return false;
192  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ getSkipSaleableCheck()

getSkipSaleableCheck ( )

Get flag that shows if Magento has to check product to be saleable (enabled and/or inStock)

Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 561 of file Product.php.

562  {
564  }

◆ getSmallImageUrl()

getSmallImageUrl (   $product)

Retrieve small image url

Parameters
ModelProduct | \Magento\Framework\DataObject$product
Returns
string|bool

Definition at line 240 of file Product.php.

241  {
242  $url = false;
243  $attribute = $product->getResource()->getAttribute('small_image');
244  if (!$product->getSmallImage()) {
245  $url = $this->_assetRepo->getUrl('Magento_Catalog::images/product/placeholder/small_image.jpg');
246  } elseif ($attribute) {
247  $url = $attribute->getFrontend()->getUrl($product);
248  }
249  return $url;
250  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ getStatuses()

getStatuses ( )
Returns
array

Definition at line 287 of file Product.php.

288  {
289  if (null === $this->_statuses) {
290  $this->_statuses = [];
291  }
292 
293  return $this->_statuses;
294  }

◆ getThumbnailUrl()

getThumbnailUrl (   $product)

Retrieve thumbnail image url

Parameters
ModelProduct | \Magento\Framework\DataObject$product
Returns
string|bool

Definition at line 258 of file Product.php.

259  {
260  $url = false;
261  $attribute = $product->getResource()->getAttribute('thumbnail');
262  if (!$product->getThumbnail()) {
263  $url = $this->_assetRepo->getUrl('Magento_Catalog::images/product/placeholder/thumbnail.jpg');
264  } elseif ($attribute) {
265  $url = $attribute->getFrontend()->getUrl($product);
266  }
267  return $url;
268  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ initProduct()

initProduct (   $productId,
  $controller,
  $params = null 
)

Inits product to be used for product controller actions and layouts $params can have following data: 'category_id' - id of category to check and append to product as current. If empty (except FALSE) - will be guessed (e.g. from last visited) to load as current.

Parameters
int$productId
\Magento\Framework\App\Action\Action$controller
\Magento\Framework\DataObject$params
Returns
bool|ModelProduct @SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Definition at line 407 of file Product.php.

408  {
409  // Prepare data for routine
410  if (!$params) {
411  $params = new \Magento\Framework\DataObject();
412  }
413 
414  // Init and load product
415  $this->_eventManager->dispatch(
416  'catalog_controller_product_init_before',
417  ['controller_action' => $controller, 'params' => $params]
418  );
419 
420  if (!$productId) {
421  return false;
422  }
423 
424  try {
425  $product = $this->productRepository->getById($productId, false, $this->_storeManager->getStore()->getId());
426  } catch (NoSuchEntityException $e) {
427  return false;
428  }
429 
430  if (!$this->canShow($product)) {
431  return false;
432  }
433  if (!in_array($this->_storeManager->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
434  return false;
435  }
436 
437  // Load product current category
438  $categoryId = $params->getCategoryId();
439  if (!$categoryId && $categoryId !== false) {
440  $lastId = $this->_catalogSession->getLastVisitedCategoryId();
441  if ($product->canBeShowInCategory($lastId)) {
442  $categoryId = $lastId;
443  }
444  } elseif (!$product->canBeShowInCategory($categoryId)) {
445  $categoryId = null;
446  }
447 
448  if ($categoryId) {
449  try {
450  $category = $this->categoryRepository->get($categoryId);
451  } catch (NoSuchEntityException $e) {
452  $category = null;
453  }
454  if ($category) {
455  $product->setCategory($category);
456  $this->_coreRegistry->register('current_category', $category);
457  }
458  }
459 
460  // Register current data and dispatch final events
461  $this->_coreRegistry->register('current_product', $product);
462  $this->_coreRegistry->register('product', $product);
463 
464  try {
465  $this->_eventManager->dispatch(
466  'catalog_controller_product_init_after',
467  ['product' => $product, 'controller_action' => $controller]
468  );
469  } catch (\Magento\Framework\Exception\LocalizedException $e) {
470  $this->_logger->critical($e);
471  return false;
472  }
473 
474  return $product;
475  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
canShow($product, $where='catalog')
Definition: Product.php:304
$controller
Definition: info.phtml:14
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ isDataForPriceIndexerWasChanged()

isDataForPriceIndexerWasChanged (   $data)

Retrieve data for price indexer update

Parameters
\Magento\Catalog\Model\Product | array$data
Returns
bool

Definition at line 139 of file Product.php.

140  {
141  if ($data instanceof ModelProduct) {
142  foreach ($this->_reindexPriceIndexerData['byDataResult'] as $param) {
143  if ($data->getData($param)) {
144  return true;
145  }
146  }
147  foreach ($this->_reindexPriceIndexerData['byDataChange'] as $param) {
148  if ($data->dataHasChangedFor($param)) {
149  return true;
150  }
151  }
152  } elseif (is_array($data)) {
153  foreach ($this->_reindexPriceIndexerData['byDataChange'] as $param) {
154  if (isset($data[$param])) {
155  return true;
156  }
157  }
158  }
159  return false;
160  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ isDataForProductCategoryIndexerWasChanged()

isDataForProductCategoryIndexerWasChanged ( \Magento\Catalog\Model\Product  $data)

Retrieve data for product category indexer update

Parameters
\Magento\Catalog\Model\Product$data
Returns
bool

Definition at line 168 of file Product.php.

169  {
170  foreach ($this->_reindexProductCategoryIndexerData['byDataChange'] as $param) {
171  if ($data->dataHasChangedFor($param)) {
172  return true;
173  }
174  }
175  return false;
176  }

◆ prepareProductOptions()

prepareProductOptions (   $product,
  $buyRequest 
)

Prepares product options by buyRequest: retrieves values and assigns them as default. Also parses and adds product management related values - e.g. qty

Parameters
ModelProduct$product
\Magento\Framework\DataObject$buyRequest
Returns
Product

Definition at line 485 of file Product.php.

486  {
487  $optionValues = $product->processBuyRequest($buyRequest);
488  $optionValues->setQty($buyRequest->getQty());
489  $product->setPreconfiguredValues($optionValues);
490 
491  return $this;
492  }
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest

◆ setSkipSaleableCheck()

setSkipSaleableCheck (   $skipSaleableCheck = false)

Set flag that shows if Magento has to check product to be saleable (enabled and/or inStock)

For instance, during order creation in the backend admin has ability to add any products to order

Parameters
bool$skipSaleableCheck
Returns
Product

Definition at line 549 of file Product.php.

550  {
551  $this->_skipSaleableCheck = $skipSaleableCheck;
552  return $this;
553  }

Field Documentation

◆ $_assetRepo

$_assetRepo
protected

Definition at line 46 of file Product.php.

◆ $_attributeConfig

$_attributeConfig
protected

Definition at line 58 of file Product.php.

◆ $_catalogSession

$_catalogSession
protected

Definition at line 65 of file Product.php.

◆ $_coreRegistry

$_coreRegistry
protected

Definition at line 53 of file Product.php.

◆ $_priceBlock

$_priceBlock
protected

Definition at line 41 of file Product.php.

◆ $_reindexPriceIndexerData

$_reindexPriceIndexerData
protected

Definition at line 79 of file Product.php.

◆ $_reindexProductCategoryIndexerData

$_reindexProductCategoryIndexerData
protected

Definition at line 72 of file Product.php.

◆ $_skipSaleableCheck

$_skipSaleableCheck = false
protected

Definition at line 31 of file Product.php.

◆ $_statuses

$_statuses
protected

Definition at line 36 of file Product.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 94 of file Product.php.

◆ $categoryRepository

$categoryRepository
protected

Definition at line 89 of file Product.php.

◆ $productRepository

$productRepository
protected

Definition at line 84 of file Product.php.

◆ XML_PATH_AUTO_GENERATE_MASK

const XML_PATH_AUTO_GENERATE_MASK = 'catalog/fields_masks'

Definition at line 24 of file Product.php.

◆ XML_PATH_PRODUCT_URL_USE_CATEGORY

const XML_PATH_PRODUCT_URL_USE_CATEGORY = 'catalog/seo/product_use_categories'

Definition at line 20 of file Product.php.

◆ XML_PATH_USE_PRODUCT_CANONICAL_TAG

const XML_PATH_USE_PRODUCT_CANONICAL_TAG = 'catalog/seo/product_canonical_tag'

Definition at line 22 of file Product.php.


The documentation for this class was generated from the following file: