コード例 #1
0
 /**
  * {@inheritdoc}
  */
 public function removeChild($sku, $optionId, $childSku)
 {
     $product = $this->productRepository->get($sku, true);
     if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
         throw new InputException(__('Product with specified sku: %1 is not a bundle product', $sku));
     }
     $excludeSelectionIds = [];
     $usedProductIds = [];
     $removeSelectionIds = [];
     foreach ($this->getOptions($product) as $option) {
         /** @var \Magento\Bundle\Model\Selection $selection */
         foreach ($option->getSelections() as $selection) {
             if (strcasecmp($selection->getSku(), $childSku) == 0 && $selection->getOptionId() == $optionId) {
                 $removeSelectionIds[] = $selection->getSelectionId();
                 $usedProductIds[] = $selection->getProductId();
                 continue;
             }
             $excludeSelectionIds[] = $selection->getSelectionId();
             $usedProductIds[] = $selection->getProductId();
         }
     }
     if (empty($removeSelectionIds)) {
         throw new \Magento\Framework\Exception\NoSuchEntityException(__('Requested bundle option product doesn\'t exist'));
     }
     $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
     /* @var $resource \Magento\Bundle\Model\ResourceModel\Bundle */
     $resource = $this->bundleFactory->create();
     $resource->dropAllUnneededSelections($product->getData($linkField), $excludeSelectionIds);
     $resource->removeProductRelations($product->getData($linkField), array_unique($usedProductIds));
     return true;
 }
コード例 #2
0
ファイル: TypeTest.php プロジェクト: pradeep-wagento/magento2
 public function testSave()
 {
     $options = ['some_option' => ['option_id' => '', 'delete' => false]];
     $selections = ['some_option' => [123 => ['selection_id' => '', 'delete' => false]]];
     $resource = $this->getMockBuilder('Magento\\Bundle\\Model\\ResourceModel\\Bundle')->disableOriginalConstructor()->getMock();
     $this->bundleFactory->expects($this->once())->method('create')->willReturn($resource);
     $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->setMethods(['getStoreId', 'getOrigData', 'getData', 'getBundleOptionsData', 'getBundleSelectionsData'])->disableOriginalConstructor()->getMock();
     $product->expects($this->once())->method('getBundleOptionsData')->willReturn($options);
     $product->expects($this->once())->method('getBundleSelectionsData')->willReturn($selections);
     $option = $this->getMockBuilder('Magento\\Bundle\\Model\\ResourceModel\\Option\\Collection')->setMethods(['setData', 'setParentId', 'setStoreId', 'isDeleted', 'save', 'getOptionId'])->disableOriginalConstructor()->getMock();
     $option->expects($this->once())->method('setData')->willReturnSelf();
     $option->expects($this->once())->method('setParentId')->willReturnSelf();
     $option->expects($this->once())->method('setStoreId')->willReturnSelf();
     $this->bundleOptionFactory->expects($this->once())->method('create')->will($this->returnValue($option));
     $selection = $this->getMockBuilder('Magento\\Bundle\\Model\\Selection')->setMethods(['setData', 'setOptionId', 'setParentProductId', 'setWebsiteId', 'save'])->disableOriginalConstructor()->getMock();
     $selection->expects($this->once())->method('setData')->willReturnSelf();
     $selection->expects($this->once())->method('setOptionId')->willReturnSelf();
     $selection->expects($this->once())->method('setParentProductId')->willReturnSelf();
     $selection->expects($this->once())->method('setWebsiteId')->willReturnSelf();
     $selection->expects($this->once())->method('setParentProductId')->willReturnSelf();
     $this->bundleModelSelection->expects($this->once())->method('create')->willReturn($selection);
     $store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->setMethods(['getWebsiteId', '__wakeup'])->disableOriginalConstructor()->getMock();
     $this->storeManager->expects($this->once())->method('getStore')->will($this->returnValue($store));
     $store->expects($this->once())->method('getWebsiteId')->will($this->returnValue(10));
     $this->model->save($product);
 }
コード例 #3
0
ファイル: Type.php プロジェクト: whoople/magento2-testing
 /**
  * Save type related data
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function save($product)
 {
     parent::save($product);
     /* @var $resource \Magento\Bundle\Model\ResourceModel\Bundle */
     $resource = $this->_bundleFactory->create();
     $options = $product->getBundleOptionsData();
     if ($options) {
         $product->setIsRelationsChanged(true);
         foreach ($options as $key => $option) {
             if (isset($option['option_id']) && $option['option_id'] == '') {
                 unset($option['option_id']);
             }
             $optionModel = $this->_bundleOption->create()->setData($option)->setParentId($product->getId())->setStoreId($product->getStoreId());
             $optionModel->isDeleted((bool) $option['delete']);
             $optionModel->save();
             $options[$key]['option_id'] = $optionModel->getOptionId();
         }
         $usedProductIds = [];
         $excludeSelectionIds = [];
         $selections = $product->getBundleSelectionsData();
         if ($selections) {
             foreach ($selections as $index => $group) {
                 foreach ($group as $selection) {
                     if (isset($selection['selection_id']) && $selection['selection_id'] == '') {
                         unset($selection['selection_id']);
                     }
                     if (!isset($selection['is_default'])) {
                         $selection['is_default'] = 0;
                     }
                     $selectionModel = $this->_bundleModelSelection->create()->setData($selection)->setOptionId($options[$index]['option_id'])->setWebsiteId($this->_storeManager->getStore($product->getStoreId())->getWebsiteId())->setParentProductId($product->getId());
                     $selectionModel->isDeleted((bool) $selection['delete']);
                     $selectionModel->save();
                     $selection['selection_id'] = $selectionModel->getSelectionId();
                     if ($selectionModel->getSelectionId()) {
                         $excludeSelectionIds[] = $selectionModel->getSelectionId();
                         $usedProductIds[] = $selectionModel->getProductId();
                     }
                 }
             }
             $resource->dropAllUnneededSelections($product->getId(), $excludeSelectionIds);
             $resource->saveProductRelations($product->getId(), array_unique($usedProductIds));
         }
         if ($product->getData('price_type') != $product->getOrigData('price_type')) {
             $resource->dropAllQuoteChildItems($product->getId());
         }
     }
     return $this;
 }