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

Public Member Functions

 __construct (\Magento\Catalog\Model\ProductTypes\ConfigInterface $config, \Magento\Catalog\Model\Product\Type\Pool $productTypePool, \Magento\Catalog\Model\Product\Type\Price\Factory $priceFactory, \Magento\Framework\Pricing\PriceInfo\Factory $priceInfoFactory)
 
 factory ($product)
 
 priceFactory ($productType)
 
 getPriceInfo (Product $saleableItem)
 
 getOptionArray ()
 
 getAllOption ()
 
 getAllOptions ()
 
 getOptions ()
 
 getOptionText ($optionId)
 
 getTypes ()
 
 getCompositeTypes ()
 
 getTypesByPriority ()
 
 toOptionArray ()
 

Data Fields

const TYPE_SIMPLE = 'simple'
 
const TYPE_BUNDLE = 'bundle'
 
const TYPE_VIRTUAL = 'virtual'
 
const DEFAULT_TYPE = 'simple'
 
const DEFAULT_TYPE_MODEL = \Magento\Catalog\Model\Product\Type\Simple::class
 
const DEFAULT_PRICE_MODEL = \Magento\Catalog\Model\Product\Type\Price::class
 

Protected Attributes

 $_config
 
 $_types
 
 $_compositeTypes
 
 $_priceModels
 
 $_typesPriority
 
 $_productTypePool
 
 $_priceFactory
 
 $_priceInfoFactory
 

Detailed Description

Product type model

@api

Since
100.0.2

Definition at line 17 of file Type.php.

Constructor & Destructor Documentation

◆ __construct()

Construct

Parameters
\Magento\Catalog\Model\ProductTypes\ConfigInterface$config
\Magento\Catalog\Model\Product\Type\Pool$productTypePool
\Magento\Catalog\Model\Product\Type\Price\Factory$priceFactory
\Magento\Framework\Pricing\PriceInfo\Factory$priceInfoFactory

Definition at line 104 of file Type.php.

109  {
110  $this->_config = $config;
111  $this->_productTypePool = $productTypePool;
112  $this->_priceFactory = $priceFactory;
113  $this->_priceInfoFactory = $priceInfoFactory;
114  }
$config
Definition: fraud_order.php:17

Member Function Documentation

◆ factory()

factory (   $product)

Factory to product singleton product type instances

Parameters
\Magento\Catalog\Model\Product$product
Returns
\Magento\Catalog\Model\Product\Type\AbstractType

Definition at line 122 of file Type.php.

123  {
124  $types = $this->getTypes();
125  $typeId = $product->getTypeId();
126 
127  if (!empty($types[$typeId]['model'])) {
128  $typeModelName = $types[$typeId]['model'];
129  } else {
130  $typeModelName = self::DEFAULT_TYPE_MODEL;
131  $typeId = self::DEFAULT_TYPE;
132  }
133 
134  $typeModel = $this->_productTypePool->get($typeModelName);
135  $typeModel->setConfig($types[$typeId]);
136  return $typeModel;
137  }

◆ getAllOption()

getAllOption ( )

Get product type labels array with empty value

Returns
array

Definition at line 193 of file Type.php.

194  {
195  $options = $this->getOptionArray();
196  array_unshift($options, ['value' => '', 'label' => '']);
197  return $options;
198  }

◆ getAllOptions()

getAllOptions ( )

Get product type labels array with empty value for option element

Returns
array

Definition at line 205 of file Type.php.

206  {
207  $res = $this->getOptions();
208  array_unshift($res, ['value' => '', 'label' => '']);
209  return $res;
210  }

◆ getCompositeTypes()

getCompositeTypes ( )

Return composite product type Ids

Returns
array

Definition at line 260 of file Type.php.

261  {
262  if ($this->_compositeTypes === null) {
263  $this->_compositeTypes = [];
264  $types = $this->getTypes();
265  foreach ($types as $typeId => $typeInfo) {
266  if (array_key_exists('composite', $typeInfo) && $typeInfo['composite']) {
267  $this->_compositeTypes[] = $typeId;
268  }
269  }
270  }
271  return $this->_compositeTypes;
272  }

◆ getOptionArray()

getOptionArray ( )

Get product type labels array

Returns
array

Definition at line 179 of file Type.php.

180  {
181  $options = [];
182  foreach ($this->getTypes() as $typeId => $type) {
183  $options[$typeId] = (string)$type['label'];
184  }
185  return $options;
186  }
$type
Definition: item.phtml:13

◆ getOptions()

getOptions ( )

Get product type labels array for option element

Returns
array

Definition at line 217 of file Type.php.

218  {
219  $res = [];
220  foreach ($this->getOptionArray() as $index => $value) {
221  $res[] = ['value' => $index, 'label' => $value];
222  }
223  return $res;
224  }
$value
Definition: gender.phtml:16
$index
Definition: list.phtml:44

◆ getOptionText()

getOptionText (   $optionId)

Get product type label

Parameters
string$optionId
Returns
null|string

Definition at line 232 of file Type.php.

233  {
234  $options = $this->getOptionArray();
235  return $options[$optionId] ?? null;
236  }

◆ getPriceInfo()

getPriceInfo ( Product  $saleableItem)

Get Product Price Info object

Parameters
Product$saleableItem
Returns
\Magento\Framework\Pricing\PriceInfoInterface

Definition at line 169 of file Type.php.

170  {
171  return $this->_priceInfoFactory->create($saleableItem);
172  }

◆ getTypes()

getTypes ( )

Get product types

Returns
array

Definition at line 243 of file Type.php.

244  {
245  if ($this->_types === null) {
246  $productTypes = $this->_config->getAll();
247  foreach ($productTypes as $productTypeKey => $productTypeConfig) {
248  $productTypes[$productTypeKey]['label'] = __($productTypeConfig['label']);
249  }
250  $this->_types = $productTypes;
251  }
252  return $this->_types;
253  }
__()
Definition: __.php:13

◆ getTypesByPriority()

getTypesByPriority ( )

Return product types by type indexing priority

Returns
array

Definition at line 279 of file Type.php.

280  {
281  if ($this->_typesPriority === null) {
282  $this->_typesPriority = [];
283  $simplePriority = [];
284  $compositePriority = [];
285 
286  $types = $this->getTypes();
287  foreach ($types as $typeId => $typeInfo) {
288  $priority = isset($typeInfo['index_priority']) ? abs(intval($typeInfo['index_priority'])) : 0;
289  if (!empty($typeInfo['composite'])) {
290  $compositePriority[$typeId] = $priority;
291  } else {
292  $simplePriority[$typeId] = $priority;
293  }
294  }
295 
296  asort($simplePriority, SORT_NUMERIC);
297  asort($compositePriority, SORT_NUMERIC);
298 
299  foreach (array_keys($simplePriority) as $typeId) {
300  $this->_typesPriority[$typeId] = $types[$typeId];
301  }
302  foreach (array_keys($compositePriority) as $typeId) {
303  $this->_typesPriority[$typeId] = $types[$typeId];
304  }
305  }
306  return $this->_typesPriority;
307  }

◆ priceFactory()

priceFactory (   $productType)

Product type price model factory

Parameters
string$productType
Returns
\Magento\Catalog\Model\Product\Type\Price

Definition at line 145 of file Type.php.

146  {
147  if (isset($this->_priceModels[$productType])) {
148  return $this->_priceModels[$productType];
149  }
150 
151  $types = $this->getTypes();
152 
153  if (!empty($types[$productType]['price_model'])) {
154  $priceModelName = $types[$productType]['price_model'];
155  } else {
156  $priceModelName = self::DEFAULT_PRICE_MODEL;
157  }
158 
159  $this->_priceModels[$productType] = $this->_priceFactory->create($priceModelName);
160  return $this->_priceModels[$productType];
161  }

◆ toOptionArray()

toOptionArray ( )

{Return array of options as value-label pairs

Returns
array Format: array(array('value' => '

', 'label' => '<label>'), ...)}

Implements OptionSourceInterface.

Definition at line 312 of file Type.php.

313  {
314  return $this->getOptions();
315  }

Field Documentation

◆ $_compositeTypes

$_compositeTypes
protected

Definition at line 61 of file Type.php.

◆ $_config

$_config
protected

Definition at line 47 of file Type.php.

◆ $_priceFactory

$_priceFactory
protected

Definition at line 89 of file Type.php.

◆ $_priceInfoFactory

$_priceInfoFactory
protected

Definition at line 94 of file Type.php.

◆ $_priceModels

$_priceModels
protected

Definition at line 68 of file Type.php.

◆ $_productTypePool

$_productTypePool
protected

Definition at line 82 of file Type.php.

◆ $_types

$_types
protected

Definition at line 54 of file Type.php.

◆ $_typesPriority

$_typesPriority
protected

Definition at line 75 of file Type.php.

◆ DEFAULT_PRICE_MODEL

const DEFAULT_PRICE_MODEL = \Magento\Catalog\Model\Product\Type\Price::class

Default price model

Definition at line 42 of file Type.php.

◆ DEFAULT_TYPE

const DEFAULT_TYPE = 'simple'

#- Default product type

Definition at line 32 of file Type.php.

◆ DEFAULT_TYPE_MODEL

const DEFAULT_TYPE_MODEL = \Magento\Catalog\Model\Product\Type\Simple::class

Default product type model

Definition at line 37 of file Type.php.

◆ TYPE_BUNDLE

const TYPE_BUNDLE = 'bundle'

Definition at line 24 of file Type.php.

◆ TYPE_SIMPLE

const TYPE_SIMPLE = 'simple'

#+ Available product types

Definition at line 22 of file Type.php.

◆ TYPE_VIRTUAL

const TYPE_VIRTUAL = 'virtual'

Definition at line 26 of file Type.php.


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