Example #1
0
 /**
  * Assign options to item
  *
  * @param \Magento\Sales\Model\Quote\Item $item
  * @param array $options
  * @return $this
  */
 protected function _assignOptionsToItem(\Magento\Sales\Model\Quote\Item $item, $options)
 {
     $optionIds = $item->getOptionByCode('option_ids');
     if ($optionIds) {
         foreach (explode(',', $optionIds->getValue()) as $optionId) {
             $item->removeOption('option_' . $optionId);
         }
         $item->removeOption('option_ids');
     }
     if ($item->getOptionByCode('additional_options')) {
         $item->removeOption('additional_options');
     }
     $item->save();
     if (!empty($options['options'])) {
         $item->addOption(new \Magento\Framework\Object(array('product' => $item->getProduct(), 'code' => 'option_ids', 'value' => implode(',', array_keys($options['options'])))));
         foreach ($options['options'] as $optionId => $optionValue) {
             $item->addOption(new \Magento\Framework\Object(array('product' => $item->getProduct(), 'code' => 'option_' . $optionId, 'value' => $optionValue)));
         }
     }
     if (!empty($options['additional_options'])) {
         $item->addOption(new \Magento\Framework\Object(array('product' => $item->getProduct(), 'code' => 'additional_options', 'value' => serialize($options['additional_options']))));
     }
     return $this;
 }