/** * {@inheritdoc} */ public function read(\Magento\Catalog\Model\Product\Option $option) { $output = []; foreach ($option->getValues() as $value) { $output[] = $this->metadataBuilder->populateWithArray([Metadata::PRICE => $value->getPrice(), Metadata::PRICE_TYPE => $value->getPriceType(), Metadata::SKU => $value->getSku(), Metadata::TITLE => $value->getTitle(), Metadata::SORT_ORDER => $value->getSortOrder(), Metadata::OPTION_TYPE_ID => $value->getId()])->create(); } return $output; }
/** * Validate option type fields * * @param Option $option * @return bool * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function validateOptionValue(Option $option) { $values = $option->getValues() ?: $option->getData('values'); if (!is_array($values) || $this->isEmpty($values)) { return false; } //forbid removal of last value for option if ($this->checkAllValuesRemoved($values)) { return false; } $storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID; if ($option->getProduct()) { $storeId = $option->getProduct()->getStoreId(); } foreach ($values as $value) { $type = isset($value['price_type']) ? $value['price_type'] : null; $price = isset($value['price']) ? $value['price'] : null; $title = isset($value['title']) ? $value['title'] : null; if (!$this->isValidOptionPrice($type, $price, $storeId) || !$this->isValidOptionTitle($title, $storeId)) { return false; } } return true; }
/** * Retrieve option values or false for options which has no values * * @param \Magento\Catalog\Model\Product\Option $option * @return array|bool */ protected function getOptionValues(\Magento\Catalog\Model\Product\Option $option) { $values = $option->getValues(); if (!empty($values)) { $result = []; /** @var $value \Magento\Catalog\Model\Product\Option\Value */ foreach ($values as $value) { $optionData = []; foreach ($this->_assertOptionValues as $assertKey) { if ($value->hasData($assertKey)) { $optionData[$assertKey] = $value->getData($assertKey); } } $result[] = $optionData; } return $result; } return false; }