本文整理汇总了PHP中Magento\Catalog\Api\Data\ProductInterface::getOptions方法的典型用法代码示例。如果您正苦于以下问题:PHP ProductInterface::getOptions方法的具体用法?PHP ProductInterface::getOptions怎么用?PHP ProductInterface::getOptions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Catalog\Api\Data\ProductInterface
的用法示例。
在下文中一共展示了ProductInterface::getOptions方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: processOptions
/**
* Process product options, creating new options, updating and deleting existing options
*
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @param array $newOptions
* @return $this
* @throws NoSuchEntityException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function processOptions(\Magento\Catalog\Api\Data\ProductInterface $product, $newOptions)
{
//existing options by option_id
/** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterface[] $existingOptions */
$existingOptions = $product->getOptions();
if ($existingOptions === null) {
$existingOptions = [];
}
$newOptionIds = [];
foreach ($newOptions as $key => $option) {
if (isset($option['option_id'])) {
//updating existing option
$optionId = $option['option_id'];
if (!isset($existingOptions[$optionId])) {
throw new NoSuchEntityException(__('Product option with id %1 does not exist', $optionId));
}
$existingOption = $existingOptions[$optionId];
$newOptionIds[] = $option['option_id'];
if (isset($option['values'])) {
//updating option values
$optionValues = $option['values'];
$valueIds = [];
foreach ($optionValues as $optionValue) {
if (isset($optionValue['option_type_id'])) {
$valueIds[] = $optionValue['option_type_id'];
}
}
$originalValues = $existingOption->getValues();
foreach ($originalValues as $originalValue) {
if (!in_array($originalValue->getOptionTypeId(), $valueIds)) {
$originalValue->setData('is_delete', 1);
$optionValues[] = $originalValue->getData();
}
}
$newOptions[$key]['values'] = $optionValues;
} else {
$existingOptionData = $this->optionConverter->toArray($existingOption);
if (isset($existingOptionData['values'])) {
$newOptions[$key]['values'] = $existingOptionData['values'];
}
}
}
}
$optionIdsToDelete = array_diff(array_keys($existingOptions), $newOptionIds);
foreach ($optionIdsToDelete as $optionId) {
$optionToDelete = $existingOptions[$optionId];
$optionDataArray = $this->optionConverter->toArray($optionToDelete);
$optionDataArray['is_delete'] = 1;
$newOptions[] = $optionDataArray;
}
$product->setProductOptions($newOptions);
return $this;
}