Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Item Class Reference
Inheritance diagram for Item:
AbstractItem CartItemInterface AbstractExtensibleModel ItemInterface ExtensibleDataInterface AbstractModel CustomAttributesDataInterface DataObject ExtensibleDataInterface

Public Member Functions

 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, \Magento\Sales\Model\Status\ListFactory $statusListFactory, \Magento\Framework\Locale\FormatInterface $localeFormat, \Magento\Quote\Model\Quote\Item\OptionFactory $itemOptionFactory, \Magento\Quote\Model\Quote\Item\Compare $quoteItemCompare, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[], \Magento\Framework\Serialize\Serializer\Json $serializer=null)
 
 beforeSave ()
 
 getAddress ()
 
 setQuote (\Magento\Quote\Model\Quote $quote)
 
 getQuote ()
 
 addQty ($qty)
 
 setQty ($qty)
 
 setQtyOptions ($qtyOptions)
 
 setProduct ($product)
 
 representProduct ($product)
 
 compareOptions ($options1, $options2)
 
 compare ($item)
 
 getProductType ()
 
 getRealProductType ()
 
 toArray (array $arrAttributes=[])
 
 setOptions ($options)
 
 getOptions ()
 
 getOptionsByCode ()
 
 addOption ($option)
 
 updateQtyOption (\Magento\Framework\DataObject $option, $value)
 
 removeOption ($code)
 
 getOptionByCode ($code)
 
 saveItemOptions ()
 
 setIsOptionsSaved ($flag)
 
 isOptionsSaved ()
 
 afterSave ()
 
 __clone ()
 
 getBuyRequest ()
 
 setHasError ($flag)
 
 addErrorInfo ($origin=null, $code=null, $message=null, $additionalData=null)
 
 getErrorInfos ()
 
 removeErrorInfosByParams ($params)
 
 getItemId ()
 
 setItemId ($itemID)
 
 getSku ()
 
 setSku ($sku)
 
 getQty ()
 
 getName ()
 
 setName ($name)
 
 getPrice ()
 
 setPrice ($price)
 
 setProductType ($productType)
 
 getQuoteId ()
 
 setQuoteId ($quoteId)
 
 getProductOption ()
 
 setProductOption (\Magento\Quote\Api\Data\ProductOptionInterface $productOption)
 
 getExtensionAttributes ()
 
 setExtensionAttributes (\Magento\Quote\Api\Data\CartItemExtensionInterface $extensionAttributes)
 
- Public Member Functions inherited from AbstractItem
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
 
 getQuote ()
 
 getAddress ()
 
 getProduct ()
 
 getFileDownloadParams ()
 
 beforeSave ()
 
 setParentItem ($parentItem)
 
 getParentItem ()
 
 getChildren ()
 
 addChild ($child)
 
 setMessage ($messages)
 
 addMessage ($message)
 
 getMessage ($string=true)
 
 removeMessageByText ($text)
 
 clearMessage ()
 
 getStore ()
 
 checkData ()
 
 getQty ()
 
 getTotalQty ()
 
 calcRowTotal ()
 
 getCalculationPrice ()
 
 getCalculationPriceOriginal ()
 
 getBaseCalculationPrice ()
 
 getBaseCalculationPriceOriginal ()
 
 getOriginalPrice ()
 
 setOriginalPrice ($price)
 
 getBaseOriginalPrice ()
 
 setCustomPrice ($value)
 
 getPrice ()
 
 setPrice ($value)
 
 getConvertedPrice ()
 
 setConvertedPrice ($value)
 
 __clone ()
 
 isChildrenCalculated ()
 
 isShipSeparately ()
 
 getTotalDiscountAmount ()
 
- Public Member Functions inherited from AbstractExtensibleModel
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
 
 getCustomAttributes ()
 
 getCustomAttribute ($attributeCode)
 
 setCustomAttributes (array $attributes)
 
 setCustomAttribute ($attributeCode, $attributeValue)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 setId ($value)
 
 __sleep ()
 
 __wakeup ()
 
- Public Member Functions inherited from AbstractModel
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
 
 __sleep ()
 
 __wakeup ()
 
 setIdFieldName ($name)
 
 getIdFieldName ()
 
 getId ()
 
 setId ($value)
 
 isDeleted ($isDeleted=null)
 
 hasDataChanges ()
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 setDataChanges ($value)
 
 getOrigData ($key=null)
 
 setOrigData ($key=null, $data=null)
 
 dataHasChangedFor ($field)
 
 getResourceName ()
 
 getResourceCollection ()
 
 getCollection ()
 
 load ($modelId, $field=null)
 
 beforeLoad ($identifier, $field=null)
 
 afterLoad ()
 
 isSaveAllowed ()
 
 setHasDataChanges ($flag)
 
 save ()
 
 afterCommitCallback ()
 
 isObjectNew ($flag=null)
 
 beforeSave ()
 
 validateBeforeSave ()
 
 getCacheTags ()
 
 cleanModelCache ()
 
 afterSave ()
 
 delete ()
 
 beforeDelete ()
 
 afterDelete ()
 
 afterDeleteCommit ()
 
 getResource ()
 
 getEntityId ()
 
 setEntityId ($entityId)
 
 clearInstance ()
 
 getStoredData ()
 
 getEventPrefix ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 
- Public Member Functions inherited from CartItemInterface
 setProductOption (\Magento\Quote\Api\Data\ProductOptionInterface $productOption)
 
 setExtensionAttributes (\Magento\Quote\Api\Data\CartItemExtensionInterface $extensionAttributes)
 

Protected Member Functions

 _construct ()
 
 _prepareQty ($qty)
 
 _addOptionCode ($option)
 
 _hasModelChanged ()
 
 _setHasError ($flag)
 
 _clearErrorInfo ()
 
- Protected Member Functions inherited from AbstractExtensibleModel
 filterCustomAttributes ($data)
 
 initializeCustomAttributes ()
 
 convertCustomAttributeValues (array &$customAttributes)
 
 getCustomAttributesCodes ()
 
 _setExtensionAttributes (\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
 
 _getExtensionAttributes ()
 
- Protected Member Functions inherited from AbstractModel
 _construct ()
 
 _init ($resourceModel)
 
 _setResourceModel ($resourceName, $collectionName=null)
 
 _getResource ()
 
 _getEventData ()
 
 _beforeLoad ($modelId, $field=null)
 
 _afterLoad ()
 
 _hasModelChanged ()
 
 _getValidatorBeforeSave ()
 
 _createValidatorBeforeSave ()
 
 _getValidationRulesBeforeSave ()
 
 _clearReferences ()
 
 _clearData ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_eventPrefix = 'sales_quote_item'
 
 $_eventObject = 'item'
 
 $_quote
 
 $_options = []
 
 $_optionsByCode = []
 
 $_notRepresentOptions = ['info_buyRequest']
 
 $_flagOptionsSaved
 
 $_errorInfos
 
 $_localeFormat
 
 $_itemOptionFactory
 
 $quoteItemCompare
 
 $stockRegistry
 
- Protected Attributes inherited from AbstractItem
 $_parentItem = null
 
 $_children = []
 
 $_messages = []
 
 $_optionsByCode
 
 $productRepository
 
 $priceCurrency
 
- Protected Attributes inherited from AbstractExtensibleModel
 $extensionAttributesFactory
 
 $extensionAttributes
 
 $customAttributeFactory
 
 $customAttributesCodes = null
 
 $customAttributesChanged = false
 
- Protected Attributes inherited from AbstractModel
 $_eventPrefix = 'core_abstract'
 
 $_eventObject = 'object'
 
 $_idFieldName = 'id'
 
 $_hasDataChanges = false
 
 $_origData
 
 $_isDeleted = false
 
 $_resource
 
 $_resourceCollection
 
 $_resourceName
 
 $_collectionName
 
 $_cacheTag = false
 
 $_dataSaveAllowed = true
 
 $_isObjectNew = null
 
 $_validatorBeforeSave = null
 
 $_eventManager
 
 $_cacheManager
 
 $_registry
 
 $_logger
 
 $_appState
 
 $_actionValidator
 
 $storedData = []
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Data Fields inherited from CustomAttributesDataInterface
const CUSTOM_ATTRIBUTES = 'custom_attributes'
 
- Data Fields inherited from ExtensibleDataInterface
const EXTENSION_ATTRIBUTES_KEY = 'extension_attributes'
 
- Data Fields inherited from CartItemInterface
const KEY_ITEM_ID = 'item_id'
 
const KEY_SKU = 'sku'
 
const KEY_QTY = 'qty'
 
const KEY_NAME = 'name'
 
const KEY_PRICE = 'price'
 
const KEY_PRODUCT_TYPE = 'product_type'
 
const KEY_QUOTE_ID = 'quote_id'
 
const KEY_PRODUCT_OPTION = 'product_option'
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Sales Quote Item Model

@api @method string getCreatedAt() @method \Magento\Quote\Model\Quote\Item setCreatedAt(string $value) @method string getUpdatedAt() @method \Magento\Quote\Model\Quote\Item setUpdatedAt(string $value) @method int getStoreId() @method \Magento\Quote\Model\Quote\Item setStoreId(int $value) @method int getParentItemId() @method \Magento\Quote\Model\Quote\Item setParentItemId(int $value) @method int getIsVirtual() @method \Magento\Quote\Model\Quote\Item setIsVirtual(int $value) @method string getDescription() @method \Magento\Quote\Model\Quote\Item setDescription(string $value) @method string getAdditionalData() @method \Magento\Quote\Model\Quote\Item setAdditionalData(string $value) @method int getFreeShipping() @method \Magento\Quote\Model\Quote\Item setFreeShipping(int $value) @method int getIsQtyDecimal() @method \Magento\Quote\Model\Quote\Item setIsQtyDecimal(int $value) @method int getNoDiscount() @method \Magento\Quote\Model\Quote\Item setNoDiscount(int $value) @method float getWeight() @method \Magento\Quote\Model\Quote\Item setWeight(float $value) @method float getBasePrice() @method \Magento\Quote\Model\Quote\Item setBasePrice(float $value) @method float getCustomPrice() @method float getTaxPercent() @method \Magento\Quote\Model\Quote\Item setTaxPercent(float $value) @method \Magento\Quote\Model\Quote\Item setTaxAmount(float $value) @method \Magento\Quote\Model\Quote\Item setBaseTaxAmount(float $value) @method \Magento\Quote\Model\Quote\Item setRowTotal(float $value) @method \Magento\Quote\Model\Quote\Item setBaseRowTotal(float $value) @method float getRowTotalWithDiscount() @method \Magento\Quote\Model\Quote\Item setRowTotalWithDiscount(float $value) @method float getRowWeight() @method \Magento\Quote\Model\Quote\Item setRowWeight(float $value) @method float getBaseTaxBeforeDiscount() @method \Magento\Quote\Model\Quote\Item setBaseTaxBeforeDiscount(float $value) @method float getTaxBeforeDiscount() @method \Magento\Quote\Model\Quote\Item setTaxBeforeDiscount(float $value) @method float getOriginalCustomPrice() @method \Magento\Quote\Model\Quote\Item setOriginalCustomPrice(float $value) @method string getRedirectUrl() @method \Magento\Quote\Model\Quote\Item setRedirectUrl(string $value) @method float getBaseCost() @method \Magento\Quote\Model\Quote\Item setBaseCost(float $value) @method \Magento\Quote\Model\Quote\Item setPriceInclTax(float $value) @method float getBasePriceInclTax() @method \Magento\Quote\Model\Quote\Item setBasePriceInclTax(float $value) @method \Magento\Quote\Model\Quote\Item setRowTotalInclTax(float $value) @method float getBaseRowTotalInclTax() @method \Magento\Quote\Model\Quote\Item setBaseRowTotalInclTax(float $value) @method int getGiftMessageId() @method \Magento\Quote\Model\Quote\Item setGiftMessageId(int $value) @method string getWeeeTaxApplied() @method \Magento\Quote\Model\Quote\Item setWeeeTaxApplied(string $value) @method float getWeeeTaxAppliedAmount() @method \Magento\Quote\Model\Quote\Item setWeeeTaxAppliedAmount(float $value) @method float getWeeeTaxAppliedRowAmount() @method \Magento\Quote\Model\Quote\Item setWeeeTaxAppliedRowAmount(float $value) @method float getBaseWeeeTaxAppliedAmount() @method \Magento\Quote\Model\Quote\Item setBaseWeeeTaxAppliedAmount(float $value) @method float getBaseWeeeTaxAppliedRowAmnt() @method \Magento\Quote\Model\Quote\Item setBaseWeeeTaxAppliedRowAmnt(float $value) @method float getWeeeTaxDisposition() @method \Magento\Quote\Model\Quote\Item setWeeeTaxDisposition(float $value) @method float getWeeeTaxRowDisposition() @method \Magento\Quote\Model\Quote\Item setWeeeTaxRowDisposition(float $value) @method float getBaseWeeeTaxDisposition() @method \Magento\Quote\Model\Quote\Item setBaseWeeeTaxDisposition(float $value) @method float getBaseWeeeTaxRowDisposition() @method \Magento\Quote\Model\Quote\Item setBaseWeeeTaxRowDisposition(float $value) @method float getDiscountTaxCompensationAmount() @method \Magento\Quote\Model\Quote\Item setDiscountTaxCompensationAmount(float $value) @method float getBaseDiscountTaxCompensationAmount() @method \Magento\Quote\Model\Quote\Item setBaseDiscountTaxCompensationAmount(float $value) @method null|bool getHasConfigurationUnavailableError() @method \Magento\Quote\Model\Quote\Item setHasConfigurationUnavailableError(bool $value) @method \Magento\Quote\Model\Quote\Item unsHasConfigurationUnavailableError() @SuppressWarnings(PHPMD.CouplingBetweenObjects) @SuppressWarnings(PHPMD.ExcessivePublicCount) @SuppressWarnings(PHPMD.ExcessiveClassComplexity)

Since
100.0.2

Definition at line 98 of file Item.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Model\Context  $context,
\Magento\Framework\Registry  $registry,
ExtensionAttributesFactory  $extensionFactory,
AttributeValueFactory  $customAttributeFactory,
\Magento\Catalog\Api\ProductRepositoryInterface  $productRepository,
\Magento\Framework\Pricing\PriceCurrencyInterface  $priceCurrency,
\Magento\Sales\Model\Status\ListFactory  $statusListFactory,
\Magento\Framework\Locale\FormatInterface  $localeFormat,
\Magento\Quote\Model\Quote\Item\OptionFactory  $itemOptionFactory,
\Magento\Quote\Model\Quote\Item\Compare  $quoteItemCompare,
\Magento\CatalogInventory\Api\StockRegistryInterface  $stockRegistry,
\Magento\Framework\Model\ResourceModel\AbstractResource  $resource = null,
\Magento\Framework\Data\Collection\AbstractDb  $resourceCollection = null,
array  $data = [],
\Magento\Framework\Serialize\Serializer\Json  $serializer = null 
)
Parameters
\Magento\Framework\Model\Context$context
\Magento\Framework\Registry$registry
ExtensionAttributesFactory$extensionFactory
AttributeValueFactory$customAttributeFactory
\Magento\Catalog\Api\ProductRepositoryInterface$productRepository
\Magento\Framework\Pricing\PriceCurrencyInterface$priceCurrency
\Magento\Sales\Model\Status\ListFactory$statusListFactory
\Magento\Framework\Locale\FormatInterface$localeFormat
Item\OptionFactory$itemOptionFactory
Item\Compare$quoteItemCompare
\Magento\CatalogInventory\Api\StockRegistryInterface$stockRegistry
\Magento\Framework\Model\ResourceModel\AbstractResource$resource
\Magento\Framework\Data\Collection\AbstractDb$resourceCollection
array$data
\Magento\Framework\Serialize\Serializer\Json$serializer@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 204 of file Item.php.

220  {
221  $this->_errorInfos = $statusListFactory->create();
222  $this->_localeFormat = $localeFormat;
223  $this->_itemOptionFactory = $itemOptionFactory;
224  $this->quoteItemCompare = $quoteItemCompare;
225  $this->stockRegistry = $stockRegistry;
227  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
228  parent::__construct(
229  $context,
230  $registry,
231  $extensionFactory,
235  $resource,
236  $resourceCollection,
237  $data
238  );
239  }
$resource
Definition: bulk.php:12

Member Function Documentation

◆ __clone()

__clone ( )

Clone quote item

Returns
$this

Definition at line 802 of file Item.php.

803  {
804  parent::__clone();
805  $options = $this->getOptions();
806  $this->_quote = null;
807  $this->_options = [];
808  $this->_optionsByCode = [];
809  foreach ($options as $option) {
810  $this->addOption(clone $option);
811  }
812  return $this;
813  }

◆ _addOptionCode()

_addOptionCode (   $option)
protected

Register option code

Parameters
\Magento\Quote\Model\Quote\Item\Option$option
Returns
$this
Exceptions

Definition at line 694 of file Item.php.

695  {
696  if (!isset($this->_optionsByCode[$option->getCode()])) {
697  $this->_optionsByCode[$option->getCode()] = $option;
698  } else {
699  throw new \Magento\Framework\Exception\LocalizedException(
700  __('An item option with code %1 already exists.', $option->getCode())
701  );
702  }
703  return $this;
704  }
__()
Definition: __.php:13

◆ _clearErrorInfo()

_clearErrorInfo ( )
protected

Clears list of errors, associated with this quote item. Also automatically removes error-flag from oneself.

Returns
$this

Definition at line 870 of file Item.php.

871  {
872  $this->_errorInfos->clear();
873  $this->_setHasError(false);
874  return $this;
875  }

◆ _construct()

_construct ( )
protected

Initialize resource model

Returns
void

Definition at line 246 of file Item.php.

247  {
248  $this->_init(\Magento\Quote\Model\ResourceModel\Quote\Item::class);
249  }

◆ _hasModelChanged()

_hasModelChanged ( )
protected

Checks that item model has data changes. Call save item options if model isn't need to save in DB

Returns
boolean

Definition at line 726 of file Item.php.

727  {
728  if (!$this->hasDataChanges()) {
729  return false;
730  }
731 
732  return $this->_getResource()->hasDataChanged($this);
733  }

◆ _prepareQty()

_prepareQty (   $qty)
protected

Prepare quantity

Parameters
float | int$qty
Returns
int|float

Definition at line 314 of file Item.php.

315  {
316  $qty = $this->_localeFormat->getNumber($qty);
317  $qty = $qty > 0 ? $qty : 1;
318  return $qty;
319  }

◆ _setHasError()

_setHasError (   $flag)
protected

Sets flag, whether this quote item has some error associated with it.

Parameters
bool$flag
Returns
\Magento\Quote\Model\Quote\Item

Definition at line 839 of file Item.php.

840  {
841  return $this->setData('has_error', $flag);
842  }

◆ addErrorInfo()

addErrorInfo (   $origin = null,
  $code = null,
  $message = null,
  $additionalData = null 
)

Adds error information to the quote item. Automatically sets error flag.

Parameters
string | null$originUsually a name of module, that embeds error
int | null$codeError code, unique for origin, that sets it
string | null$messageError message
\Magento\Framework\DataObject | null$additionalDataAny additional data, that caller would like to store
Returns
$this

Definition at line 887 of file Item.php.

888  {
889  $this->_errorInfos->addItem($origin, $code, $message, $additionalData);
890  if ($message !== null) {
891  $this->setMessage($message);
892  }
893  $this->_setHasError(true);
894 
895  return $this;
896  }
$message
$code
Definition: info.phtml:12

◆ addOption()

addOption (   $option)

Add option to item

Parameters
\Magento\Quote\Model\Quote\Item\Option | \Magento\Framework\DataObject$option
Returns
$this
Exceptions

Definition at line 614 of file Item.php.

615  {
616  if (is_array($option)) {
617  $option = $this->_itemOptionFactory->create()->setData($option)->setItem($this);
618  } elseif ($option instanceof \Magento\Framework\DataObject &&
619  !$option instanceof \Magento\Quote\Model\Quote\Item\Option
620  ) {
621  $option = $this->_itemOptionFactory->create()->setData(
622  $option->getData()
623  )->setProduct(
624  $option->getProduct()
625  )->setItem(
626  $this
627  );
628  } elseif ($option instanceof \Magento\Quote\Model\Quote\Item\Option) {
629  $option->setItem($this);
630  } else {
631  throw new \Magento\Framework\Exception\LocalizedException(__('We found an invalid item option format.'));
632  }
633 
634  $exOption = $this->getOptionByCode($option->getCode());
635  if ($exOption) {
636  $exOption->addData($option->getData());
637  } else {
638  $this->_addOptionCode($option);
639  $this->_options[] = $option;
640  }
641  return $this;
642  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13

◆ addQty()

addQty (   $qty)

Adding quantity to quote item

Parameters
float$qty
Returns
$this

We can't modify quantity of existing items which have parent This qty declared just once during add process and is not editable

Definition at line 327 of file Item.php.

328  {
333  if (!$this->getParentItem() || !$this->getId()) {
334  $qty = $this->_prepareQty($qty);
335  $this->setQtyToAdd($qty);
336  $this->setQty($this->getQty() + $qty);
337  }
338  return $this;
339  }

◆ afterSave()

afterSave ( )

Save item options after item saved

Returns
\Magento\Quote\Model\Quote\Item

Definition at line 791 of file Item.php.

792  {
793  $this->saveItemOptions();
794  return parent::afterSave();
795  }

◆ beforeSave()

beforeSave ( )

Quote Item Before Save prepare data process

Returns
$this

Definition at line 256 of file Item.php.

257  {
258  parent::beforeSave();
259  $this->setIsVirtual($this->getProduct()->getIsVirtual());
260  if ($this->getQuote()) {
261  $this->setQuoteId($this->getQuote()->getId());
262  }
263  return $this;
264  }

◆ compare()

compare (   $item)

Compare items

Parameters
\Magento\Quote\Model\Quote\Item$item
Returns
bool

Definition at line 514 of file Item.php.

515  {
516  return $this->quoteItemCompare->compare($this, $item);
517  }

◆ compareOptions()

compareOptions (   $options1,
  $options2 
)

Check if two options array are identical First options array is prerogative Second options array checked against first one

Parameters
array$options1
array$options2
Returns
bool

Definition at line 494 of file Item.php.

495  {
496  foreach ($options1 as $option) {
497  $code = $option->getCode();
498  if (in_array($code, $this->_notRepresentOptions)) {
499  continue;
500  }
501  if (!isset($options2[$code]) || $options2[$code]->getValue() != $option->getValue()) {
502  return false;
503  }
504  }
505  return true;
506  }
$code
Definition: info.phtml:12

◆ getAddress()

getAddress ( )

Retrieve address model

Returns
\Magento\Quote\Model\Quote\Address

Definition at line 271 of file Item.php.

272  {
273  if ($this->getQuote()->getItemsQty() == $this->getQuote()->getVirtualItemsQty()) {
274  $address = $this->getQuote()->getBillingAddress();
275  } else {
276  $address = $this->getQuote()->getShippingAddress();
277  }
278 
279  return $address;
280  }
$address
Definition: customer.php:38

◆ getBuyRequest()

getBuyRequest ( )

Returns formatted buy request - object, holding request received from product view page with keys and options for configured product

Returns
\Magento\Framework\DataObject

Definition at line 821 of file Item.php.

822  {
823  $option = $this->getOptionByCode('info_buyRequest');
824  $data = $option ? $this->serializer->unserialize($option->getValue()) : [];
825  $buyRequest = new \Magento\Framework\DataObject($data);
826 
827  // Overwrite standard buy request qty, because item qty could have changed since adding to quote
828  $buyRequest->setOriginalQty($buyRequest->getQty())->setQty($this->getQty() * 1);
829 
830  return $buyRequest;
831  }
foreach($product->getExtensionAttributes() ->getBundleProductOptions() as $option) $buyRequest

◆ getErrorInfos()

getErrorInfos ( )

Retrieves all error infos, associated with this item

Returns
array

Definition at line 903 of file Item.php.

904  {
905  return $this->_errorInfos->getItems();
906  }

◆ getExtensionAttributes()

getExtensionAttributes ( )

{Retrieve existing extension attributes object or create a new one.

Returns
\Magento\Quote\Api\Data\CartItemExtensionInterface|null
}

Returns
\Magento\Quote\Api\Data\CartItemExtensionInterface|null

Implements CartItemInterface.

Definition at line 1058 of file Item.php.

◆ getItemId()

getItemId ( )

@codeCoverageIgnoreStart

{#- Returns the item ID.

Returns
int|null Item ID. Otherwise, null.
}

Implements CartItemInterface.

Definition at line 937 of file Item.php.

938  {
939  return $this->getData(self::KEY_ITEM_ID);
940  }

◆ getName()

getName ( )

{Returns the product name.

Returns
string|null Product name. Otherwise, null.
}

Implements CartItemInterface.

Definition at line 977 of file Item.php.

978  {
979  return $this->getData(self::KEY_NAME);
980  }

◆ getOptionByCode()

getOptionByCode (   $code)

Get item option by code

Parameters
string$code
Returns
\Magento\Quote\Model\Quote\Item\Option || null

Implements ItemInterface.

Definition at line 712 of file Item.php.

713  {
714  if (isset($this->_optionsByCode[$code]) && !$this->_optionsByCode[$code]->isDeleted()) {
715  return $this->_optionsByCode[$code];
716  }
717  return null;
718  }
$code
Definition: info.phtml:12

◆ getOptions()

getOptions ( )

Get all item options

@codeCoverageIgnore

Returns
\Magento\Quote\Model\Quote\Item\Option[]

Definition at line 590 of file Item.php.

591  {
592  return $this->_options;
593  }

◆ getOptionsByCode()

getOptionsByCode ( )

Get all item options as array with codes in array key

@codeCoverageIgnore

Returns
array

Definition at line 602 of file Item.php.

603  {
604  return $this->_optionsByCode;
605  }

◆ getPrice()

getPrice ( )

{Returns the product price.

Returns
float|null Product price. Otherwise, null.
}

Implements CartItemInterface.

Definition at line 993 of file Item.php.

994  {
995  return $this->getData(self::KEY_PRICE);
996  }

◆ getProductOption()

getProductOption ( )

Returns product option

Returns
\Magento\Quote\Api\Data\ProductOptionInterface|null

Implements CartItemInterface.

Definition at line 1035 of file Item.php.

1036  {
1037  return $this->getData(self::KEY_PRODUCT_OPTION);
1038  }

◆ getProductType()

getProductType ( )

Get item product type

Returns
string

Implements CartItemInterface.

Definition at line 524 of file Item.php.

525  {
526  $option = $this->getOptionByCode(self::KEY_PRODUCT_TYPE);
527  if ($option) {
528  return $option->getValue();
529  }
530  $product = $this->getProduct();
531  if ($product) {
532  return $product->getTypeId();
533  }
534  // $product should always exist or there will be an error in getProduct()
535  return $this->_getData(self::KEY_PRODUCT_TYPE);
536  }

◆ getQty()

getQty ( )

{Returns the product quantity.

Returns
float Product quantity.
}

Implements CartItemInterface.

Definition at line 969 of file Item.php.

970  {
971  return $this->getData(self::KEY_QTY);
972  }

◆ getQuote()

getQuote ( )

Retrieve quote model object

@codeCoverageIgnore

Returns
\Magento\Quote\Model\Quote

Definition at line 303 of file Item.php.

304  {
305  return $this->_quote;
306  }

◆ getQuoteId()

getQuoteId ( )

{Returns Quote id.

Returns
string
}

Implements CartItemInterface.

Definition at line 1017 of file Item.php.

1018  {
1019  return $this->getData(self::KEY_QUOTE_ID);
1020  }

◆ getRealProductType()

getRealProductType ( )

Return real product type of item

@codeCoverageIgnore

Returns
string

Definition at line 545 of file Item.php.

546  {
547  return $this->_getData(self::KEY_PRODUCT_TYPE);
548  }

◆ getSku()

getSku ( )

{Returns the product SKU.

Returns
string|null Product SKU. Otherwise, null.
}

Implements CartItemInterface.

Definition at line 953 of file Item.php.

954  {
955  return $this->getData(self::KEY_SKU);
956  }

◆ isOptionsSaved()

isOptionsSaved ( )

Were options saved

@codeCoverageIgnore

Returns
bool

Definition at line 781 of file Item.php.

782  {
784  }

◆ removeErrorInfosByParams()

removeErrorInfosByParams (   $params)

Removes error infos, that have parameters equal to passed in $params. $params can have following keys (if not set - then any item is good for this key): 'origin', 'code', 'message'

Parameters
array$params
Returns
$this

Definition at line 916 of file Item.php.

917  {
918  $removedItems = $this->_errorInfos->removeItemsByParams($params);
919  foreach ($removedItems as $item) {
920  if ($item['message'] !== null) {
921  $this->removeMessageByText($item['message']);
922  }
923  }
924 
925  if (!$this->_errorInfos->getItems()) {
926  $this->_setHasError(false);
927  }
928 
929  return $this;
930  }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ removeOption()

removeOption (   $code)

Remove option from item options

Parameters
string$code
Returns
$this

Definition at line 678 of file Item.php.

679  {
680  $option = $this->getOptionByCode($code);
681  if ($option) {
682  $option->isDeleted(true);
683  }
684  return $this;
685  }
$code
Definition: info.phtml:12

◆ representProduct()

representProduct (   $product)

Check product representation in item

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

Check maybe product is planned to be a child of some quote item - in this case we limit search only within same parent item

Definition at line 454 of file Item.php.

455  {
456  $itemProduct = $this->getProduct();
457  if (!$product || $itemProduct->getId() != $product->getId()) {
458  return false;
459  }
460 
465  $stickWithinParent = $product->getStickWithinParent();
466  if ($stickWithinParent) {
467  if ($this->getParentItem() !== $stickWithinParent) {
468  return false;
469  }
470  }
471 
472  // Check options
473  $itemOptions = $this->getOptionsByCode();
474  $productOptions = $product->getCustomOptions();
475 
476  if (!$this->compareOptions($itemOptions, $productOptions)) {
477  return false;
478  }
479  if (!$this->compareOptions($productOptions, $itemOptions)) {
480  return false;
481  }
482  return true;
483  }
compareOptions($options1, $options2)
Definition: Item.php:494

◆ saveItemOptions()

saveItemOptions ( )

Save item options

Returns
$this

Definition at line 740 of file Item.php.

741  {
742  foreach ($this->_options as $index => $option) {
743  if ($option->isDeleted()) {
744  $option->delete();
745  unset($this->_options[$index]);
746  unset($this->_optionsByCode[$option->getCode()]);
747  } else {
748  if (!$option->getItem() || !$option->getItem()->getId()) {
749  $option->setItem($this);
750  }
751  $option->save();
752  }
753  }
754 
755  $this->_flagOptionsSaved = true;
756  // Report to watchers that options were saved
757 
758  return $this;
759  }
$index
Definition: list.phtml:44

◆ setExtensionAttributes()

setExtensionAttributes ( \Magento\Quote\Api\Data\CartItemExtensionInterface  $extensionAttributes)

{}

Parameters
\Magento\Quote\Api\Data\CartItemExtensionInterface$extensionAttributes
Returns
$this

Definition at line 1069 of file Item.php.

1070  {
1072  }
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)

◆ setHasError()

setHasError (   $flag)

Sets flag, whether this quote item has some error associated with it. When TRUE - also adds 'unknown' error information to list of quote item errors. When FALSE - clears whole list of quote item errors. It's recommended to use addErrorInfo() instead - to be able to remove error statuses later.

Parameters
bool$flag
Returns
$this
See also
addErrorInfo()

Definition at line 854 of file Item.php.

855  {
856  if ($flag) {
857  $this->addErrorInfo();
858  } else {
859  $this->_clearErrorInfo();
860  }
861  return $this;
862  }
addErrorInfo($origin=null, $code=null, $message=null, $additionalData=null)
Definition: Item.php:887

◆ setIsOptionsSaved()

setIsOptionsSaved (   $flag)

Mar option save requirement

@codeCoverageIgnore

Parameters
bool$flag
Returns
void

Definition at line 769 of file Item.php.

770  {
771  $this->_flagOptionsSaved = $flag;
772  }

◆ setItemId()

setItemId (   $itemID)

{Sets the item ID.

Parameters
int$itemId
Returns
$this
}

Implements CartItemInterface.

Definition at line 945 of file Item.php.

946  {
947  return $this->setData(self::KEY_ITEM_ID, $itemID);
948  }

◆ setName()

setName (   $name)

{Sets the product name.

Parameters
string$name
Returns
$this
}

Implements CartItemInterface.

Definition at line 985 of file Item.php.

986  {
987  return $this->setData(self::KEY_NAME, $name);
988  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ setOptions()

setOptions (   $options)

Initialize quote item options

Parameters
array$options
Returns
$this

Definition at line 573 of file Item.php.

574  {
575  if (is_array($options)) {
576  foreach ($options as $option) {
577  $this->addOption($option);
578  }
579  }
580  return $this;
581  }

◆ setPrice()

setPrice (   $price)

{Sets the product price.

Parameters
float$price
Returns
$this
}

Implements CartItemInterface.

Definition at line 1001 of file Item.php.

1002  {
1003  return $this->setData(self::KEY_PRICE, $price);
1004  }
$price

◆ setProduct()

setProduct (   $product)

Setup product for quote item

Parameters
\Magento\Catalog\Model\Product$product
Returns
$this

Definition at line 422 of file Item.php.

423  {
424  if ($this->getQuote()) {
425  $product->setStoreId($this->getQuote()->getStoreId());
426  $product->setCustomerGroupId($this->getQuote()->getCustomerGroupId());
427  }
428  $this->setData('product', $product)
429  ->setProductId($product->getId())
430  ->setProductType($product->getTypeId())
431  ->setSku($this->getProduct()->getSku())
432  ->setName($product->getName())
433  ->setWeight($this->getProduct()->getWeight())
434  ->setTaxClassId($product->getTaxClassId())
435  ->setBaseCost($product->getCost());
436 
437  $stockItem = $product->getExtensionAttributes()->getStockItem();
438  $this->setIsQtyDecimal($stockItem ? $stockItem->getIsQtyDecimal() : false);
439 
440  $this->_eventManager->dispatch(
441  'sales_quote_item_set_product',
442  ['product' => $product, 'quote_item' => $this]
443  );
444 
445  return $this;
446  }
setProductType($productType)
Definition: Item.php:1009

◆ setProductOption()

setProductOption ( \Magento\Quote\Api\Data\ProductOptionInterface  $productOption)

Sets product option

Parameters
\Magento\Quote\Api\Data\ProductOptionInterface$productOption
Returns
$this

Definition at line 1046 of file Item.php.

1047  {
1048  return $this->setData(self::KEY_PRODUCT_OPTION, $productOption);
1049  }

◆ setProductType()

setProductType (   $productType)

{Sets the product type.

Parameters
string$productType
Returns
$this
}

Implements CartItemInterface.

Definition at line 1009 of file Item.php.

1010  {
1011  return $this->setData(self::KEY_PRODUCT_TYPE, $productType);
1012  }

◆ setQty()

setQty (   $qty)

Declare quote item quantity

Parameters
float$qty
Returns
$this

Implements CartItemInterface.

Definition at line 347 of file Item.php.

348  {
349  $qty = $this->_prepareQty($qty);
350  $oldQty = $this->_getData(self::KEY_QTY);
351  $this->setData(self::KEY_QTY, $qty);
352 
353  $this->_eventManager->dispatch('sales_quote_item_qty_set_after', ['item' => $this]);
354 
355  if ($this->getQuote() && $this->getQuote()->getIgnoreOldQty()) {
356  return $this;
357  }
358 
359  if ($this->getUseOldQty()) {
360  $this->setData(self::KEY_QTY, $oldQty);
361  }
362 
363  return $this;
364  }

◆ setQtyOptions()

setQtyOptions (   $qtyOptions)

Set option product with Qty

@codeCoverageIgnore

Parameters
array$qtyOptions
Returns
$this

Definition at line 411 of file Item.php.

412  {
413  return $this->setData('qty_options', $qtyOptions);
414  }

◆ setQuote()

setQuote ( \Magento\Quote\Model\Quote  $quote)

Declare quote model object

Parameters
\Magento\Quote\Model\Quote$quote
Returns
$this

Definition at line 288 of file Item.php.

289  {
290  $this->_quote = $quote;
291  $this->setQuoteId($quote->getId());
292  $this->setStoreId($quote->getStoreId());
293  return $this;
294  }
$quote

◆ setQuoteId()

setQuoteId (   $quoteId)

{Sets Quote id.

Parameters
string$quoteId
Returns
$this
}

Implements CartItemInterface.

Definition at line 1025 of file Item.php.

1026  {
1027  return $this->setData(self::KEY_QUOTE_ID, $quoteId);
1028  }

◆ setSku()

setSku (   $sku)

{Sets the product SKU.

Parameters
string$sku
Returns
$this
}

Implements CartItemInterface.

Definition at line 961 of file Item.php.

962  {
963  return $this->setData(self::KEY_SKU, $sku);
964  }

◆ toArray()

toArray ( array  $arrAttributes = [])

Convert Quote Item to array

Parameters
array$arrAttributes
Returns
array

Definition at line 556 of file Item.php.

557  {
558  $data = parent::toArray($arrAttributes);
559 
560  $product = $this->getProduct();
561  if ($product) {
562  $data['product'] = $product->toArray();
563  }
564  return $data;
565  }

◆ updateQtyOption()

updateQtyOption ( \Magento\Framework\DataObject  $option,
  $value 
)

Can specify specific actions for ability to change given quote options values Example: cataloginventory decimal qty validation may change qty to int, so need to change quote item qty option value.

Parameters
\Magento\Framework\DataObject$option
int | float | null$value
Returns
$this

Definition at line 653 of file Item.php.

654  {
655  $optionProduct = $option->getProduct();
656  $options = $this->getQtyOptions();
657 
658  if (isset($options[$optionProduct->getId()])) {
659  $options[$optionProduct->getId()]->setValue($value);
660  }
661 
662  $this->getProduct()->getTypeInstance()->updateQtyOption(
663  $this->getOptions(),
664  $option,
665  $value,
666  $this->getProduct()
667  );
668 
669  return $this;
670  }
$value
Definition: gender.phtml:16

Field Documentation

◆ $_errorInfos

$_errorInfos
protected

Definition at line 155 of file Item.php.

◆ $_eventObject

$_eventObject = 'item'
protected

Definition at line 114 of file Item.php.

◆ $_eventPrefix

$_eventPrefix = 'sales_quote_item'
protected

Definition at line 105 of file Item.php.

◆ $_flagOptionsSaved

$_flagOptionsSaved
protected

Flag stating that options were successfully saved

Definition at line 148 of file Item.php.

◆ $_itemOptionFactory

$_itemOptionFactory
protected

Definition at line 165 of file Item.php.

◆ $_localeFormat

$_localeFormat
protected

Definition at line 160 of file Item.php.

◆ $_notRepresentOptions

$_notRepresentOptions = ['info_buyRequest']
protected

Definition at line 142 of file Item.php.

◆ $_options

$_options = []
protected

Definition at line 128 of file Item.php.

◆ $_optionsByCode

$_optionsByCode = []
protected

Definition at line 135 of file Item.php.

◆ $_quote

$_quote
protected

Definition at line 121 of file Item.php.

◆ $quoteItemCompare

$quoteItemCompare
protected

Definition at line 170 of file Item.php.

◆ $stockRegistry

$stockRegistry
protected

Definition at line 176 of file Item.php.


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