Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected function getProductOptions($productObj)
- {
- $objectManager = MagentoFrameworkAppObjectManager::getInstance();
- $customOptions = $objectManager->get('MagentoCatalogModelProductOption')->getProductOptionCollection($productObj);
- $options = [];
- if($customOptions->count() > 0) {
- foreach($customOptions as $customOption) {
- $optionsValue = [];
- $customOptionValues = $objectManager->get('MagentoCatalogModelProductOptionValue')->getValuesCollection($customOption);
- if($customOptionValues->count() > 0) {
- foreach($customOptionValues as $customOptionValue) {
- $optionsValue[] = array(
- 'record_id' => $customOptionValue->getRecordId(),
- 'title' => $customOptionValue->getTitle(),
- 'price' => $customOptionValue->getPrice(),
- 'price_type' => $customOptionValue->getPriceType(),
- 'sort_order' => $customOptionValue->getSortOrder(),
- 'sku' => $customOptionValue->getSku(),
- 'is_delete' => 0,
- );
- }
- }
- $sku = $customOption->getSku();
- $title = $customOption->getTitle();
- $type = $customOption->getType();
- $price = $customOption->getPrice();
- $price_type = $customOption->getPriceType();
- $record_id = $customOption->getRecordId();
- $options[] = array(
- 'sort_order' => $customOption->getSortOrder(),
- 'title' => $title,
- 'price_type' => $price_type,
- 'price' => $price,
- 'type' => $type,
- 'values' => $optionsValue,
- 'is_require' => 1,
- );
- }
- }
- return $options;
- }
- protected function addProductCustomOptions($product, $productOption)
- {
- $objectManager = MagentoFrameworkAppObjectManager::getInstance();
- $product->setHasOptions(1);
- $product->setCanSaveCustomOptions(true);
- foreach ($productOption as $arrayOption) {
- $option = $objectManager->create('MagentoCatalogModelProductOption')
- ->setProductId($product->getId())
- ->setStoreId($product->getStoreId())
- ->addData($arrayOption);
- $option->save();
- $product->addOption($option);
- }
- }
Add Comment
Please, Sign In to add comment