public function testGetSetTypeInstance() { // model getter $typeInstance = $this->_model->getTypeInstance(); $this->assertInstanceOf('Magento\\Catalog\\Model\\Product\\Type\\AbstractType', $typeInstance); $this->assertSame($typeInstance, $this->_model->getTypeInstance()); // singleton /** @var $otherProduct \Magento\Catalog\Model\Product */ $otherProduct = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $this->assertSame($typeInstance, $otherProduct->getTypeInstance()); // model setter $simpleTypeInstance = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product\\Type\\Simple'); $this->_model->setTypeInstance($simpleTypeInstance); $this->assertSame($simpleTypeInstance, $this->_model->getTypeInstance()); }
public function testGetSetTypeInstance() { // model getter $typeInstance = $this->_model->getTypeInstance(); $this->assertInstanceOf('Magento\\Bundle\\Model\\Product\\Type', $typeInstance); $this->assertSame($typeInstance, $this->_model->getTypeInstance()); // singleton getter $otherProduct = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $otherProduct->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE); $this->assertSame($typeInstance, $otherProduct->getTypeInstance()); // model setter $customTypeInstance = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Bundle\\Model\\Product\\Type'); $this->_model->setTypeInstance($customTypeInstance); $this->assertSame($customTypeInstance, $this->_model->getTypeInstance()); }
public function testGetTypeId() { $productType = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Type\\Virtual')->disableOriginalConstructor()->getMockForAbstractClass(); $this->productTypeInstanceMock->expects($this->exactly(2))->method('factory')->will($this->returnValue($productType)); $this->model->getTypeInstance(); $this->model->setTypeId('typeId'); $this->model->getTypeInstance(); }
public function testSaveProductRelationsNoChildren() { $childrenIds = $this->_product->getTypeInstance()->getChildrenIds(1); $this->assertNotEmpty(reset($childrenIds)); $this->_product->setAssociatedProductIds([]); $this->_model->save($this->_product); $this->_product->load(1); $this->assertEquals([[]], $this->_product->getTypeInstance()->getChildrenIds(1)); }
/** * @param Product $product * @return Option[] */ private function getOptions(Product $product) { /** @var \Magento\Bundle\Model\Product\Type $productTypeInstance */ $productTypeInstance = $product->getTypeInstance(); $productTypeInstance->setStoreFilter($product->getStoreId(), $product); $optionCollection = $productTypeInstance->getOptionsCollection($product); $selectionCollection = $productTypeInstance->getSelectionsCollection($productTypeInstance->getOptionsIds($product), $product); $options = $optionCollection->appendSelections($selectionCollection); return $options; }
/** * @param \Magento\Catalog\Model\Product $product * @return int[] */ protected function getLinkedProducts(\Magento\Catalog\Model\Product $product) { /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $typeInstance */ $typeInstance = $product->getTypeInstance(); $childrenIds = $typeInstance->getChildrenIds($product->getId()); if (isset($childrenIds[0])) { return $childrenIds[0]; } else { return []; } }
/** * @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php * @magentoAppIsolation enabled */ public function testSaveProductRelationsNoChildren() { $childrenIds = $this->product->getTypeInstance()->getChildrenIds($this->product->getId()); self::assertNotEmpty(reset($childrenIds)); $product = $this->productRepository->getById($this->product->getId(), true); $extensionAttributes = $product->getExtensionAttributes(); $extensionAttributes->setConfigurableProductLinks([]); $product->setExtensionAttributes($extensionAttributes); $this->productRepository->save($product); self::assertEquals([[]], $this->model->getChildrenIds($this->product->getId())); }
/** * Add identity of child product to identities * * @param \Magento\Catalog\Model\Product $product * @param string[] $result * @return string[] */ public function afterGetIdentities(\Magento\Catalog\Model\Product $product, $result) { /** @var Configurable $productType */ $productType = $product->getTypeInstance(); if ($productType instanceof Configurable) { foreach ($productType->getUsedProductIds($product) as $productId) { $result[] = \Magento\Catalog\Model\Product::CACHE_TAG . '_' . $productId; } } return $result; }
/** * Duplicating downloadable product data * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Catalog\Model\Product $duplicate * @return void */ public function build(\Magento\Catalog\Model\Product $product, \Magento\Catalog\Model\Product $duplicate) { if ($product->getTypeId() !== \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { //do nothing if not downloadable return; } $data = array(); /** @var \Magento\Downloadable\Model\Product\Type $type */ $type = $product->getTypeInstance(); foreach ($type->getLinks($product) as $link) { /* @var \Magento\Downloadable\Model\Link $link */ $linkData = $link->getData(); $data['link'][] = array('is_delete' => false, 'link_id' => null, 'title' => $linkData['title'], 'is_shareable' => $linkData['is_shareable'], 'sample' => array('type' => $linkData['sample_type'], 'url' => $linkData['sample_url'], 'file' => $this->encoder->jsonEncode(array(array('file' => $linkData['sample_file'], 'name' => $linkData['sample_file'], 'size' => 0, 'status' => null)))), 'file' => $this->encoder->jsonEncode(array(array('file' => $linkData['link_file'], 'name' => $linkData['link_file'], 'size' => 0, 'status' => null))), 'type' => $linkData['link_type'], 'link_url' => $linkData['link_url'], 'sort_order' => $linkData['sort_order'], 'number_of_downloads' => $linkData['number_of_downloads'], 'price' => $linkData['price']); } /** @var \Magento\Downloadable\Model\Sample $sample */ foreach ($type->getSamples($product) as $sample) { $sampleData = $sample->getData(); $data['sample'][] = array('is_delete' => false, 'sample_id' => null, 'title' => $sampleData['title'], 'type' => $sampleData['sample_type'], 'file' => $this->encoder->jsonEncode(array(array('file' => $sampleData['sample_file'], 'name' => $sampleData['sample_file'], 'size' => 0, 'status' => null))), 'sample_url' => $sampleData['sample_url'], 'sort_order' => $sampleData['sort_order']); } $duplicate->setDownloadableData($data); }
/** * Handle data received from Associated Products tab of configurable product * * @param Product $product * @return void */ public function handle(Product $product) { if ($product->getTypeId() != \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) { return; } /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $type */ $type = $product->getTypeInstance(); $originalAttributes = $type->getConfigurableAttributesAsArray($product); // Organize main information about original product attributes in assoc array form $originalAttributesMainInfo = []; if (is_array($originalAttributes)) { foreach ($originalAttributes as $originalAttribute) { $originalAttributesMainInfo[$originalAttribute['id']] = []; foreach ($originalAttribute['values'] as $value) { $originalAttributesMainInfo[$originalAttribute['id']][$value['value_index']] = ['is_percent' => $value['is_percent'], 'pricing_value' => $value['pricing_value']]; } } } $attributeData = $product->getConfigurableAttributesData(); if (is_array($attributeData)) { foreach ($attributeData as &$data) { $id = $data['attribute_id']; foreach ($data['values'] as &$value) { $valueIndex = $value['value_index']; if (isset($originalAttributesMainInfo[$id][$valueIndex])) { $value['pricing_value'] = $originalAttributesMainInfo[$id][$valueIndex]['pricing_value']; $value['is_percent'] = $originalAttributesMainInfo[$id][$valueIndex]['is_percent']; } else { $value['pricing_value'] = 0; $value['is_percent'] = 0; } } } $product->setConfigurableAttributesData($attributeData); } }
/** * Return an array of FPT attributes for a bundle product * * @param \Magento\Catalog\Model\Product $product * @return array */ public function getWeeeAttributesForBundle($product) { if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { $typeInstance = $product->getTypeInstance(); $typeInstance->setStoreFilter($product->getStoreId(), $product); $selectionCollection = $typeInstance->getSelectionsCollection( $typeInstance->getOptionsIds($product), $product ); $insertedWeeCodesArray = []; foreach ($selectionCollection as $selectionItem) { $weeAttributes = $this->getProductWeeeAttributes( $selectionItem, null, null, $product->getStore()->getWebsiteId() ); foreach ($weeAttributes as $weeAttribute) { $insertedWeeCodesArray[$selectionItem->getId()][$weeAttribute->getCode()]=$weeAttribute; } } return $insertedWeeCodesArray; } return []; }
/** * Return an array of FPT attributes for a bundle product * * @param \Magento\Catalog\Model\Product $product * @return array */ public function getWeeeAttributesForBundle($product) { if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) { $typeInstance = $product->getTypeInstance(); $typeInstance->setStoreFilter($product->getStoreId(), $product); $selectionCollection = $typeInstance->getSelectionsCollection($typeInstance->getOptionsIds($product), $product); $insertedWeeeCodesArray = []; foreach ($selectionCollection as $selectionItem) { $weeeAttributes = $this->getProductWeeeAttributes($selectionItem, null, null, $product->getStore()->getWebsiteId(), true, false); $priceTaxDisplay = $this->getTaxDisplayConfig(); $priceIncludesTax = $this->displayTotalsInclTax(); foreach ($weeeAttributes as $weeeAttribute) { if ($priceTaxDisplay == \Magento\Tax\Model\Config::DISPLAY_TYPE_INCLUDING_TAX || $priceTaxDisplay == \Magento\Tax\Model\Config::DISPLAY_TYPE_BOTH) { if ($priceIncludesTax == false) { $weeeAttribute['amount'] = $weeeAttribute['amount_excl_tax'] + $weeeAttribute['tax_amount']; } } else { if ($priceTaxDisplay == \Magento\Tax\Model\Config::DISPLAY_TYPE_EXCLUDING_TAX) { if ($priceIncludesTax == true) { $weeeAttribute['amount'] = $weeeAttribute['amount_excl_tax']; } } } $insertedWeeeCodesArray[$selectionItem->getId()][$weeeAttribute->getCode()] = $weeeAttribute; } } return $insertedWeeeCodesArray; } return []; }
/** * @param \Magento\Framework\Object $processingParams * @param \Magento\Catalog\Model\Product\Option $option * @return array * @throws LocalizedException * @throws ProductException * @throws \Exception * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Validator\Exception * @throws \Zend_File_Transfer_Exception * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function validate($processingParams, $option) { $upload = $this->httpFactory->create(); $file = $processingParams->getFilesPrefix() . 'options_' . $option->getId() . '_file'; try { $runValidation = $option->getIsRequire() || $upload->isUploaded($file); if (!$runValidation) { throw new \Magento\Framework\Validator\Exception(__('Validation failed. Required options were not filled or file was not uploaded.')); } $fileInfo = $upload->getFileInfo($file)[$file]; $fileInfo['title'] = $fileInfo['name']; } catch (\Magento\Framework\Validator\Exception $e) { throw $e; } catch (\Exception $e) { // when file exceeds the upload_max_filesize, $_FILES is empty if ($this->validateContentLength()) { $value = $this->fileSize->getMaxFileSizeInMb(); throw new LocalizedException(__('The file you uploaded is larger than %1 Megabytes allowed by server', $value)); } else { throw new ProductException(__('Option required.')); } } /** * Option Validations */ $upload = $this->buildImageValidator($upload, $option); /** * Upload process */ $this->initFilesystem(); $userValue = []; if ($upload->isUploaded($file) && $upload->isValid($file)) { $extension = pathinfo(strtolower($fileInfo['name']), PATHINFO_EXTENSION); $fileName = \Magento\MediaStorage\Model\File\Uploader::getCorrectFileName($fileInfo['name']); $dispersion = \Magento\MediaStorage\Model\File\Uploader::getDispretionPath($fileName); $filePath = $dispersion; $tmpDirectory = $this->filesystem->getDirectoryRead(DirectoryList::SYS_TMP); $fileHash = md5($tmpDirectory->readFile($tmpDirectory->getRelativePath($fileInfo['tmp_name']))); $filePath .= '/' . $fileHash . '.' . $extension; $fileFullPath = $this->mediaDirectory->getAbsolutePath($this->quotePath . $filePath); $upload->addFilter(new \Zend_Filter_File_Rename(['target' => $fileFullPath, 'overwrite' => true])); if ($this->product !== null) { $this->product->getTypeInstance()->addFileQueue(['operation' => 'receive_uploaded_file', 'src_name' => $file, 'dst_name' => $fileFullPath, 'uploader' => $upload, 'option' => $this]); } $_width = 0; $_height = 0; if ($tmpDirectory->isReadable($tmpDirectory->getRelativePath($fileInfo['tmp_name']))) { $imageSize = getimagesize($fileInfo['tmp_name']); if ($imageSize) { $_width = $imageSize[0]; $_height = $imageSize[1]; } } $uri = $this->filesystem->getUri(DirectoryList::MEDIA); $userValue = ['type' => $fileInfo['type'], 'title' => $fileInfo['name'], 'quote_path' => $uri . $this->quotePath . $filePath, 'order_path' => $uri . $this->orderPath . $filePath, 'fullpath' => $fileFullPath, 'size' => $fileInfo['size'], 'width' => $_width, 'height' => $_height, 'secret_key' => substr($fileHash, 0, 20)]; } elseif ($upload->getErrors()) { $errors = $this->getValidatorErrors($upload->getErrors(), $fileInfo, $option); if (count($errors) > 0) { throw new LocalizedException(__(implode("\n", $errors))); } } else { throw new LocalizedException(__('Please specify the product\'s required option(s).')); } return $userValue; }
/** * @param Product $product * @param int $optionId * @return array|null */ private function getProductLinks(Product $product, $optionId) { /** @var \Magento\Bundle\Model\Product\Type $productTypeInstance */ $productTypeInstance = $product->getTypeInstance(); $productTypeInstance->setStoreFilter($product->getStoreId(), $product); $selectionCollection = $productTypeInstance->getSelectionsCollection([$optionId], $product); $productLinks = []; /** @var \Magento\Catalog\Model\Product $selection */ foreach ($selectionCollection as $selection) { $productLinks[] = $this->linkConverter->createDataFromModel($selection, $product); } return $productLinks; }
/** * {@inheritdoc} */ public function getTypeInstance() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getTypeInstance'); if (!$pluginInfo) { return parent::getTypeInstance(); } else { return $this->___callPlugins('getTypeInstance', func_get_args(), $pluginInfo); } }