/**
  * Return custom option html
  *
  * @param array $optionInfo
  * @return string
  */
 public function getCustomizedOptionValue($optionInfo)
 {
     // render customized option view
     $_default = $optionInfo['value'];
     if (isset($optionInfo['option_type'])) {
         try {
             $group = $this->_optionFactory->create()->groupFactory($optionInfo['option_type']);
             return $group->getCustomizedView($optionInfo);
         } catch (\Exception $e) {
             return $_default;
         }
     }
     return $_default;
 }
Example #2
0
 /**
  * Accept option value and return its formatted view
  *
  * @param string|array $optionValue
  * Method works well with these $optionValue format:
  *      1. String
  *      2. Indexed array e.g. array(val1, val2, ...)
  *      3. Associative array, containing additional option info, including option value, e.g.
  *          array
  *          (
  *              [label] => ...,
  *              [value] => ...,
  *              [print_value] => ...,
  *              [option_id] => ...,
  *              [option_type] => ...,
  *              [custom_view] =>...,
  *          )
  * @param array $params
  * All keys are options. Following supported:
  *  - 'maxLength': truncate option value if needed, default: do not truncate
  *  - 'cutReplacer': replacer for cut off value part when option value exceeds maxLength
  *
  * @return array
  */
 public function getFormattedOptionValue($optionValue, $params = null)
 {
     // Init params
     if (!$params) {
         $params = array();
     }
     $maxLength = isset($params['max_length']) ? $params['max_length'] : null;
     $cutReplacer = isset($params['cut_replacer']) ? $params['cut_replacer'] : '...';
     // Proceed with option
     $optionInfo = array();
     // Define input data format
     if (is_array($optionValue)) {
         if (isset($optionValue['option_id'])) {
             $optionInfo = $optionValue;
             if (isset($optionInfo['value'])) {
                 $optionValue = $optionInfo['value'];
             }
         } else {
             if (isset($optionValue['value'])) {
                 $optionValue = $optionValue['value'];
             }
         }
     }
     // Render customized option view
     if (isset($optionInfo['custom_view']) && $optionInfo['custom_view']) {
         $_default = array('value' => $optionValue);
         if (isset($optionInfo['option_type'])) {
             try {
                 $group = $this->_productOptionFactory->create()->groupFactory($optionInfo['option_type']);
                 return array('value' => $group->getCustomizedView($optionInfo));
             } catch (\Exception $e) {
                 return $_default;
             }
         }
         return $_default;
     }
     // Truncate standard view
     if (is_array($optionValue)) {
         $truncatedValue = implode("\n", $optionValue);
         $truncatedValue = nl2br($truncatedValue);
         return array('value' => $truncatedValue);
     } else {
         if ($maxLength) {
             $truncatedValue = $this->filter->truncate($optionValue, array('length' => $maxLength, 'etc' => ''));
         } else {
             $truncatedValue = $optionValue;
         }
         $truncatedValue = nl2br($truncatedValue);
     }
     $result = array('value' => $truncatedValue);
     if ($maxLength && $this->string->strlen($optionValue) > $maxLength) {
         $result['value'] = $result['value'] . $cutReplacer;
         $optionValue = nl2br($optionValue);
         $result['full_view'] = $optionValue;
     }
     return $result;
 }
Example #3
0
 /**
  * Accept option value and return its formatted view
  *
  * @param mixed $optionValue
  * Method works well with these $optionValue format:
  *      1. String
  *      2. Indexed array e.g. array(val1, val2, ...)
  *      3. Associative array, containing additional option info, including option value, e.g.
  *          array
  *          (
  *              [label] => ...,
  *              [value] => ...,
  *              [print_value] => ...,
  *              [option_id] => ...,
  *              [option_type] => ...,
  *              [custom_view] =>...,
  *          )
  *
  * @return array
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function getFormatedOptionValue($optionValue)
 {
     $optionInfo = [];
     // define input data format
     if (is_array($optionValue)) {
         if (isset($optionValue['option_id'])) {
             $optionInfo = $optionValue;
             if (isset($optionInfo['value'])) {
                 $optionValue = $optionInfo['value'];
             }
         } elseif (isset($optionValue['value'])) {
             $optionValue = $optionValue['value'];
         }
     }
     // render customized option view
     if (isset($optionInfo['custom_view']) && $optionInfo['custom_view']) {
         $_default = ['value' => $optionValue];
         if (isset($optionInfo['option_type'])) {
             try {
                 $group = $this->_productOptionFactory->create()->groupFactory($optionInfo['option_type']);
                 return ['value' => $group->getCustomizedView($optionInfo)];
             } catch (\Exception $e) {
                 return $_default;
             }
         }
         return $_default;
     }
     // truncate standard view
     $result = [];
     if (is_array($optionValue)) {
         $truncatedValue = implode("\n", $optionValue);
         $truncatedValue = nl2br($truncatedValue);
         return ['value' => $truncatedValue];
     } else {
         $truncatedValue = $this->filterManager->truncate($optionValue, ['length' => 55, 'etc' => '']);
         $truncatedValue = nl2br($truncatedValue);
     }
     $result = ['value' => $truncatedValue];
     if ($this->string->strlen($optionValue) > 55) {
         $result['value'] = $result['value'] . ' <a href="#" class="dots tooltip toggle" onclick="return false">...</a>';
         $optionValue = nl2br($optionValue);
         $result = array_merge($result, ['full_view' => $optionValue]);
     }
     return $result;
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function remove($productSku, $optionId)
 {
     /** @var \Magento\Catalog\Model\Product $product */
     $product = $this->productRepository->get($productSku);
     $options = $product->getOptions();
     $option = $this->optionFactory->create();
     $option->load($optionId);
     if (!$option->getId() || !isset($options[$option->getId()])) {
         throw NoSuchEntityException::singleField('optionId', $optionId);
     }
     unset($options[$optionId]);
     try {
         $option->delete();
         if (empty($options)) {
             $product->setHasOptions(false);
             $product->save();
         }
     } catch (\Exception $e) {
         throw new CouldNotSaveException('Could not remove custom option');
     }
     return true;
 }
Example #5
0
 /**
  * Adding product custom options to result collection
  *
  * @return $this
  */
 public function addOptionsToResult()
 {
     $productsByLinkId = [];
     foreach ($this as $product) {
         $productId = $product->getData($product->getResource()->getLinkField());
         $productsByLinkId[$productId] = $product;
     }
     if (!empty($productsByLinkId)) {
         $options = $this->_productOptionFactory->create()->getCollection()->addTitleToResult($this->_storeManager->getStore()->getId())->addPriceToResult($this->_storeManager->getStore()->getId())->addProductToFilter(array_keys($productsByLinkId))->addValuesToResult();
         foreach ($options as $option) {
             if (isset($productsByLinkId[$option->getProductId()])) {
                 $productsByLinkId[$option->getProductId()]->addOption($option);
             }
         }
     }
     return $this;
 }
Example #6
0
 /**
  * Retrieve option instance
  *
  * @return Product\Option
  */
 public function getOptionInstance()
 {
     if (!isset($this->optionInstance)) {
         $this->optionInstance = $this->optionFactory->create();
         $this->optionInstance->setProduct($this);
     }
     return $this->optionInstance;
 }
Example #7
0
 /**
  * Adding product custom options to result collection
  *
  * @return $this
  */
 public function addOptionsToResult()
 {
     $productIds = array();
     foreach ($this as $product) {
         $productIds[] = $product->getId();
     }
     if (!empty($productIds)) {
         $options = $this->_productOptionFactory->create()->getCollection()->addTitleToResult($this->_storeManager->getStore()->getId())->addPriceToResult($this->_storeManager->getStore()->getId())->addProductToFilter($productIds)->addValuesToResult();
         foreach ($options as $option) {
             if ($this->getItemById($option->getProductId())) {
                 $this->getItemById($option->getProductId())->addOption($option);
             }
         }
     }
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 public function duplicate(\Magento\Catalog\Api\Data\ProductInterface $product, \Magento\Catalog\Api\Data\ProductInterface $duplicate)
 {
     return $this->optionResource->duplicate($this->optionFactory->create([]), $product->getId(), $duplicate->getId());
 }