/**
  * @param \Magento\Sales\Model\Order\Item $orderItem
  * @param array $response
  * @return void
  */
 protected function assertOrderItem(\Magento\Sales\Model\Order\Item $orderItem, array $response)
 {
     $bundleOption = $orderItem->getBuyRequest()->getBundleOption();
     $bundleOptionQty = $orderItem->getBuyRequest()->getBundleOptionQty();
     $this->assertArrayHasKey('product_option', $response);
     $this->assertArrayHasKey('extension_attributes', $response['product_option']);
     $this->assertArrayHasKey('bundle_options', $response['product_option']['extension_attributes']);
     $actualOptions = $response['product_option']['extension_attributes']['bundle_options'];
     $this->assertEquals(array_keys($bundleOption), array_column($actualOptions, 'option_id'));
     $this->assertEquals($bundleOptionQty, array_column($actualOptions, 'option_qty', 'option_id'));
     foreach ($actualOptions as $option) {
         $expectedSelections = is_array($bundleOption[$option['option_id']]) ? $bundleOption[$option['option_id']] : [$bundleOption[$option['option_id']]];
         $this->assertEquals($expectedSelections, $option['option_selections']);
     }
 }
 /**
  * @param \Magento\Sales\Model\Order\Item $orderItem
  * @param array $response
  * @return void
  */
 protected function assertOrderItem(\Magento\Sales\Model\Order\Item $orderItem, array $response)
 {
     $expected = $orderItem->getBuyRequest()->getLinks();
     $this->assertArrayHasKey('product_option', $response);
     $this->assertArrayHasKey('extension_attributes', $response['product_option']);
     $this->assertArrayHasKey('downloadable_option', $response['product_option']['extension_attributes']);
     $actualOptions = $response['product_option']['extension_attributes']['downloadable_option'];
     $this->assertArrayHasKey('downloadable_links', $actualOptions);
     $this->assertEquals($expected, $actualOptions['downloadable_links']);
 }
 /**
  * @param \Magento\Sales\Model\Order\Item $orderItem
  * @param array $response
  * @return void
  */
 protected function assertOrderItem(\Magento\Sales\Model\Order\Item $orderItem, array $response)
 {
     $expected = $orderItem->getBuyRequest()->getSuperAttribute();
     $this->assertArrayHasKey('product_option', $response);
     $this->assertArrayHasKey('extension_attributes', $response['product_option']);
     $this->assertArrayHasKey('configurable_item_options', $response['product_option']['extension_attributes']);
     $actualOptions = $response['product_option']['extension_attributes']['configurable_item_options'];
     $this->assertTrue(is_array($actualOptions));
     $this->assertTrue(is_array($actualOptions[0]));
     $this->assertArrayHasKey('option_id', $actualOptions[0]);
     $this->assertArrayHasKey('option_value', $actualOptions[0]);
     $this->assertEquals(key($expected), $actualOptions[0]['option_id']);
     $this->assertEquals(current($expected), $actualOptions[0]['option_value']);
 }
 /**
  * @param \Magento\Sales\Model\Order\Item $orderItem
  * @param array $response
  * @return void
  */
 protected function assertOrderItem(\Magento\Sales\Model\Order\Item $orderItem, array $response)
 {
     $expected = $orderItem->getBuyRequest()->getOptions();
     $this->assertArrayHasKey('product_option', $response);
     $this->assertArrayHasKey('extension_attributes', $response['product_option']);
     $this->assertArrayHasKey('custom_options', $response['product_option']['extension_attributes']);
     $actualOptions = $response['product_option']['extension_attributes']['custom_options'];
     $expectedOptions = [];
     foreach ($expected as $optionId => $optionValue) {
         if (is_array($optionValue)) {
             $optionValue = implode(',', $optionValue);
         }
         $expectedOptions[] = ['option_id' => $optionId, 'option_value' => $optionValue];
     }
     $this->assertEquals($expectedOptions, $actualOptions);
 }
Example #5
0
 /**
  * Initialize creation data from existing order Item
  *
  * @param \Magento\Sales\Model\Order\Item $orderItem
  * @param int $qty
  * @return \Magento\Quote\Model\Quote\Item|string|$this
  */
 public function initFromOrderItem(\Magento\Sales\Model\Order\Item $orderItem, $qty = null)
 {
     if (!$orderItem->getId()) {
         return $this;
     }
     $product = $this->_objectManager->create('Magento\\Catalog\\Model\\Product')->setStoreId($this->getSession()->getStoreId())->load($orderItem->getProductId());
     if ($product->getId()) {
         $product->setSkipCheckRequiredOption(true);
         $buyRequest = $orderItem->getBuyRequest();
         if (is_numeric($qty)) {
             $buyRequest->setQty($qty);
         }
         $item = $this->getQuote()->addProduct($product, $buyRequest);
         if (is_string($item)) {
             return $item;
         }
         if ($additionalOptions = $orderItem->getProductOptionByCode('additional_options')) {
             $item->addOption(new \Magento\Framework\DataObject(['product' => $item->getProduct(), 'code' => 'additional_options', 'value' => serialize($additionalOptions)]));
         }
         $this->_eventManager->dispatch('sales_convert_order_item_to_quote_item', ['order_item' => $orderItem, 'quote_item' => $item]);
         return $item;
     }
     return $this;
 }
Example #6
0
 /**
  * Returns the name for a sales item.
  * Configurable products will have their chosen options added to their name.
  * Bundle products will have their chosen child product names added.
  * Grouped products will have their parents name prepended.
  * All others will have their own name only.
  *
  * @param Item $item the sales item model.
  *
  * @return string
  */
 protected function buildItemName(Item $item)
 {
     $name = $item->getName();
     $optNames = array();
     if ($item->getProductType() === Type::TYPE_SIMPLE) {
         $type = $item->getProduct()->getTypeInstance();
         $parentIds = $type->getParentIdsByChild($item->getProductId());
         // If the product has a configurable parent, we assume we should tag
         // the parent. If there are many parent IDs, we are safer to tag the
         // products own name alone.
         if (count($parentIds) === 1) {
             $attributes = $item->getBuyRequest()->getData('super_attribute');
             if (is_array($attributes)) {
                 foreach ($attributes as $id => $value) {
                     /** @var Attribute $attribute */
                     $attribute = $this->_objectManager->get('Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute')->load($id);
                     $label = $attribute->getSource()->getOptionText($value);
                     if (!empty($label)) {
                         $optNames[] = $label;
                     }
                 }
             }
         }
     } elseif ($item->getProductType() === Configurable::TYPE_CODE) {
         $opts = $item->getProductOptionByCode('attributes_info');
         if (is_array($opts)) {
             foreach ($opts as $opt) {
                 if (isset($opt['value']) && is_string($opt['value'])) {
                     $optNames[] = $opt['value'];
                 }
             }
         }
     } elseif ($item->getProductType() === Type::TYPE_BUNDLE) {
         $opts = $item->getProductOptionByCode('bundle_options');
         if (is_array($opts)) {
             foreach ($opts as $opt) {
                 if (isset($opt['value']) && is_array($opt['value'])) {
                     foreach ($opt['value'] as $val) {
                         $qty = '';
                         if (isset($val['qty']) && is_int($val['qty'])) {
                             $qty .= $val['qty'] . ' x ';
                         }
                         if (isset($val['title']) && is_string($val['title'])) {
                             $optNames[] = $qty . $val['title'];
                         }
                     }
                 }
             }
         }
     } elseif ($item->getProductType() === Grouped::TYPE_CODE) {
         $config = $item->getProductOptionByCode('super_product_config');
         if (isset($config['product_id'])) {
             /** @var Product $parent */
             $parent = $this->_objectManager->get('Magento\\Catalog\\Model\\Product')->load($config['product_id']);
             $parentName = $parent->getName();
             if (!empty($parentName)) {
                 $name = $parentName . ' - ' . $name;
             }
         }
     }
     if (!empty($optNames)) {
         $name .= ' (' . implode(', ', $optNames) . ')';
     }
     return $name;
 }