/** * @param $parentsIds * @param $validationResult * @param $runValidateAmount * @param $result * @dataProvider dataProviderForValidateWithValidConfigurableProduct * @return void */ public function testAroundValidateWithValidConfigurableProduct($parentsIds, $validationResult, $runValidateAmount, $result) { $closureMock = function () { return false; }; $this->productMock->expects($this->once())->method('getId')->willReturn('product_id'); $this->configurableMock->expects($this->once())->method('getParentIdsByChild')->with('product_id')->willReturn($parentsIds); $this->ruleMock->expects($this->exactly($runValidateAmount))->method('getConditions')->willReturn($this->ruleConditionsMock); $this->ruleConditionsMock->expects($this->exactly($runValidateAmount))->method('validateByEntityId')->willReturnMap($validationResult); $this->assertEquals($result, $this->validation->aroundValidate($this->ruleMock, $closureMock, $this->productMock)); }
/** * @param array $configurableIds * @param \Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject * * @return array */ private function reindexSubProducts(array $configurableIds, \Magento\CatalogRule\Model\Indexer\Product\ProductRuleIndexer $subject) { $subProducts = []; if ($configurableIds) { $subProducts = array_values($this->configurable->getChildrenIds($configurableIds)[0]); if ($subProducts) { $subject->executeList($subProducts); } } return $subProducts; }
/** * @param \Magento\Framework\Pricing\SaleableInterface|\Magento\Catalog\Model\Product $product * @return float * @throws \Magento\Framework\Exception\LocalizedException */ public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product) { $price = null; foreach ($this->configurable->getUsedProducts($product) as $subProduct) { $productPrice = $this->priceResolver->resolvePrice($subProduct); $price = $price ? min($price, $productPrice) : $productPrice; } if (!$price) { throw new \Magento\Framework\Exception\LocalizedException(__('Configurable product "%1" do not have sub-products', $product->getName())); } return (double) $price; }
/** * {@inheritdoc} */ public function getProducts(ProductInterface $product) { if (!isset($this->products[$product->getId()])) { if ($this->requestSafety->isSafeMethod()) { $productIds = $this->resource->getConnection()->fetchCol('(' . implode(') UNION (', $this->linkedProductSelectBuilder->build($product->getId())) . ')'); $this->products[$product->getId()] = $this->collectionFactory->create()->addAttributeToSelect(['price', 'special_price'])->addIdFilter($productIds); } else { $this->products[$product->getId()] = $this->configurable->getUsedProducts($product); } } return $this->products[$product->getId()]; }
/** * Preparing layout * * @return \Magento\ConfigurableProduct\Block\Product\Configurable\AssociatedSelector\Backend\Grid\ColumnSet */ protected function _prepareLayout() { parent::_prepareLayout(); $product = $this->getProduct(); $attributes = $this->_productType->getUsedProductAttributes($product); foreach ($attributes as $attribute) { /** @var $attribute \Magento\Catalog\Model\Entity\Attribute */ /** @var $block \Magento\Backend\Block\Widget\Grid\Column */ $block = $this->addChild($attribute->getAttributeCode(), 'Magento\\Backend\\Block\\Widget\\Grid\\Column', array('header' => $attribute->getStoreLabel(), 'index' => $attribute->getAttributeCode(), 'type' => 'options', 'options' => $this->getOptions($attribute->getSource()), 'sortable' => false)); $block->setId($attribute->getAttributeCode())->setGrid($this); } return $this; }
/** * Prepare attribute set comprising all selected configurable attributes * * @param \Magento\Catalog\Model\Product $product * * @return void */ protected function prepareAttributeSetToBeBaseForNewVariations(\Magento\Catalog\Model\Product $product) { $attributes = $this->configurableProduct->getUsedProductAttributes($product); $attributeSetId = $product->getNewVariationsAttributeSetId(); /** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */ $attributeSet = $this->attributeSetFactory->create()->load($attributeSetId); $attributeSet->addSetInfo($this->entityFactory->create()->setType(\Magento\Catalog\Model\Product::ENTITY)->getTypeId(), $attributes); foreach ($attributes as $attribute) { /* @var $attribute \Magento\Catalog\Model\Entity\Attribute */ if (!$attribute->isInSet($attributeSetId)) { $attribute->setAttributeSetId($attributeSetId)->setAttributeGroupId($attributeSet->getDefaultGroupId($attributeSetId))->save(); } } }
/** * Create configurable product options * * @param array $attributesData * @return OptionInterface[] * @throws \InvalidArgumentException */ public function create(array $attributesData) { $options = []; foreach ($attributesData as $item) { $attribute = $this->attributeFactory->create(); $eavAttribute = $this->productAttributeRepository->get($item[Attribute::KEY_ATTRIBUTE_ID]); if (!$this->productType->canUseAttribute($eavAttribute)) { throw new \InvalidArgumentException('Provided attribute can not be used with configurable product.'); } $this->updateAttributeData($attribute, $item); $options[] = $attribute; } return $options; }
/** * @param \Magento\CatalogRule\Model\Rule $rule * @param \Closure $proceed * @param \Magento\Framework\DataObject|\Magento\Catalog\Model\Product $product * @return bool */ public function aroundValidate(\Magento\CatalogRule\Model\Rule $rule, \Closure $proceed, \Magento\Framework\DataObject $product) { $validateResult = $proceed($product); if (!$validateResult && ($configurableProducts = $this->configurable->getParentIdsByChild($product->getId()))) { foreach ($configurableProducts as $configurableProductId) { $validateResult = $rule->getConditions()->validateByEntityId($configurableProductId); // If any of configurable product is valid for current rule, then their sub-product must be valid too if ($validateResult) { break; } } } return $validateResult; }
/** * @inheritdoc */ protected function prepareProduct($product, $data) { if ($this->attributeSet !== $data['attribute_set']) { $this->attributeSet = $data['attribute_set']; $this->eavConfig->clear(); } if (empty($data['associated_product_ids'])) { $simpleIds = $this->configurableProductType->generateSimpleProducts($product, $data['variations_matrix']); } else { $simpleIds = $data['associated_product_ids']; } $product->setAssociatedProductIds($simpleIds); $product->setCanSaveConfigurableAttributes(true); return $this; }
/** * @param \Magento\Framework\Pricing\SaleableInterface $product * @return float */ public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product) { $selectedConfigurableOption = $product->getSelectedConfigurableOption(); if ($selectedConfigurableOption) { $price = $this->priceResolver->resolvePrice($selectedConfigurableOption); } else { $price = null; foreach ($this->configurable->getUsedProducts($product) as $subProduct) { $productPrice = $this->priceResolver->resolvePrice($subProduct); $price = $price ? min($price, $productPrice) : $productPrice; } } $priceInCurrentCurrency = $this->priceCurrency->convertAndRound($price); return $priceInCurrentCurrency ? (double) $priceInCurrentCurrency : false; }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGenerateSimpleProducts() { $productsData = [6 => ['image' => 'image.jpg', 'name' => 'config-red', 'configurable_attribute' => '{"new_attr":"6"}', 'sku' => 'config-red', 'quantity_and_stock_status' => ['qty' => ''], 'weight' => '333']]; $stockData = ['manage_stock' => '0', 'use_config_enable_qty_increments' => '1', 'use_config_qty_increments' => '1', 'use_config_manage_stock' => 0, 'is_decimal_divided' => 0]; $parentProductMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['__wakeup', 'getNewVariationsAttributeSetId', 'getStockData', 'getQuantityAndStockStatus', 'getWebsiteIds'])->disableOriginalConstructor()->getMock(); $newSimpleProductMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['__wakeup', 'save', 'getId', 'setStoreId', 'setTypeId', 'setAttributeSetId', 'getTypeInstance', 'getStoreId', 'addData', 'setWebsiteIds', 'setStatus', 'setVisibility'])->disableOriginalConstructor()->getMock(); $attributeMock = $this->getMockBuilder('\\Magento\\Eav\\Model\\Entity\\Attribute')->setMethods(['isInSet', 'setAttributeSetId', 'setAttributeGroupId', 'save'])->disableOriginalConstructor()->getMock(); $attributeSetMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Set')->setMethods(['load', 'addSetInfo', 'getDefaultGroupId'])->disableOriginalConstructor()->getMock(); $eavEntityMock = $this->getMockBuilder('\\Magento\\Eav\\Model\\Entity')->setMethods(['setType', 'getTypeId'])->disableOriginalConstructor()->getMock(); $productTypeMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Type')->setMethods(['getSetAttributes'])->disableOriginalConstructor()->getMock(); $editableAttributeMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute')->setMethods(['getIsUnique', 'getAttributeCode', 'getFrontend', 'getIsVisible'])->disableOriginalConstructor()->getMock(); $frontendAttributeMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Frontend')->setMethods(['getInputType'])->disableOriginalConstructor()->getMock(); $this->configurableProduct->expects($this->once())->method('getUsedProductAttributes')->willReturn([$attributeMock]); $parentProductMock->expects($this->any())->method('getNewVariationsAttributeSetId')->willReturn('new_attr_set_id'); $this->_attributeSetFactory->expects($this->once())->method('create')->willReturn($attributeSetMock); $attributeSetMock->expects($this->once())->method('load')->with('new_attr_set_id')->willReturnSelf(); $this->_entityFactoryMock->expects($this->once())->method('create')->willReturn($eavEntityMock); $eavEntityMock->expects($this->once())->method('setType')->with('catalog_product')->willReturnSelf(); $eavEntityMock->expects($this->once())->method('getTypeId')->willReturn('type_id'); $attributeSetMock->expects($this->once())->method('addSetInfo')->with('type_id', [$attributeMock]); $attributeMock->expects($this->once())->method('isInSet')->with('new_attr_set_id')->willReturn(false); $attributeMock->expects($this->once())->method('setAttributeSetId')->with('new_attr_set_id')->willReturnSelf(); $attributeSetMock->expects($this->once())->method('getDefaultGroupId')->with('new_attr_set_id')->willReturn('default_group_id'); $attributeMock->expects($this->once())->method('setAttributeGroupId')->with('default_group_id')->willReturnSelf(); $attributeMock->expects($this->once())->method('save')->willReturnSelf(); $this->_productFactoryMock->expects($this->once())->method('create')->willReturn($newSimpleProductMock); $newSimpleProductMock->expects($this->once())->method('setStoreId')->with(0)->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setTypeId')->with('simple')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setAttributeSetId')->with('new_attr_set_id')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('getTypeInstance')->willReturn($productTypeMock); $productTypeMock->expects($this->once())->method('getSetAttributes')->with($newSimpleProductMock)->willReturn([$editableAttributeMock]); $editableAttributeMock->expects($this->once())->method('getIsUnique')->willReturn(false); $editableAttributeMock->expects($this->once())->method('getAttributeCode')->willReturn('some_code'); $editableAttributeMock->expects($this->any())->method('getFrontend')->willReturn($frontendAttributeMock); $frontendAttributeMock->expects($this->any())->method('getInputType')->willReturn('input_type'); $editableAttributeMock->expects($this->any())->method('getIsVisible')->willReturn(false); $parentProductMock->expects($this->once())->method('getStockData')->willReturn($stockData); $parentProductMock->expects($this->once())->method('getQuantityAndStockStatus')->willReturn(['is_in_stock' => 1]); $newSimpleProductMock->expects($this->once())->method('getStoreId')->willReturn('store_id'); $this->_stockConfiguration->expects($this->once())->method('getManageStock')->with('store_id')->willReturn(1); $newSimpleProductMock->expects($this->once())->method('addData')->willReturnSelf(); $parentProductMock->expects($this->once())->method('getWebsiteIds')->willReturn('website_id'); $newSimpleProductMock->expects($this->once())->method('setWebsiteIds')->with('website_id')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setVisibility')->with(1)->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('save')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('getId')->willReturn('product_id'); $this->assertEquals(['product_id'], $this->_model->generateSimpleProducts($parentProductMock, $productsData)); }
private function validateConfigurable($product, $attributeSetId, $storeId) { $type = $product->getTypeInstance(); //var_dump(\Magento\Store\Model\ScopeInterface::SCOPE_STORE);die(); $auto = $this->scopeConfig->getValue('catalog/configurable_value/auto_create', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); if (!$type instanceof Configurable) { return true; } $attributeSet = $this->attributeSetFactory->create()->load($attributeSetId); $attributes = $this->configurableProduct->getUsedProductAttributes($product); $attributeSet->addSetInfo($this->entityFactory->create()->setType(\Magento\Catalog\Model\Product::ENTITY)->getTypeId(), $attributes); foreach ($type->getConfigurableAttributes($product) as $configAattribute) { $attribute = $configAattribute->getProductAttribute(); if (!is_null($attribute)) { //var_dump($attribute->isInSet($attributeSetId));die(); if (!$attribute->isInSet($attributeSetId)) { if ($auto) { $attribute->setAttributeSetId($attributeSetId)->setAttributeGroupId($attributeSet->getDefaultGroupId($attributeSetId))->save(); return true; } else { $this->messageManager->addError(__('The configurable attribute ' . $attribute->getAttributeCode() . ' is not available in the targeted attribute set. Please create it first! Or allow create it from global config Catalog->Change Attribte Set')); return false; } } else { return true; } } } }
protected function getAttributesFromConfigurable() { $this->productMock->expects($this->atLeastOnce())->method('getTypeInstance')->willReturn($this->configurableMock); $confAttribute = $this->getMock('\\Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable\\Attribute', [], [], '', false); $this->configurableMock->expects($this->any())->method('getConfigurableAttributes')->with($this->productMock)->willReturn([$confAttribute, $confAttribute]); $confAttribute->expects($this->any())->method('__call')->with('getProductAttribute')->willReturn($this->attributeMock); }
/** * {@inheritdoc} */ public function getUsedProductCollection($product = null) { $collection = parent::getUsedProductCollection($product); if (!is_null($this->getStoreFilter($product))) { $collection->setStoreId($this->getStoreFilter($product)); } return $collection; }
/** * Select one of the options and "prepare for cart" with a proper buy request */ protected function _prepareForCart() { $attributes = $this->_model->getConfigurableAttributesAsArray($this->_product); $attribute = reset($attributes); $optionValueId = $attribute['values'][0]['value_index']; $buyRequest = new \Magento\Framework\Object(['qty' => 5, 'super_attribute' => [$attribute['attribute_id'] => $optionValueId]]); $this->_model->prepareForCart($buyRequest, $this->_product); }
/** * @covers \Magento\ConfigurableProduct\Helper\Product\Options\Loader::load */ public function testLoad() { $option = ['value_index' => 23]; $this->product->expects(static::once())->method('getTypeInstance')->willReturn($this->configurable); $attribute = $this->getMockBuilder(Attribute::class)->disableOriginalConstructor()->setMethods(['getOptions', 'setValues'])->getMock(); $attributes = [$attribute]; $iterator = $this->getMockBuilder(Collection::class)->disableOriginalConstructor()->getMock(); $iterator->expects($this->once())->method('getIterator')->willReturn(new \ArrayIterator($attributes)); $this->configurable->expects(static::once())->method('getConfigurableAttributeCollection')->with($this->product)->willReturn($iterator); $attribute->expects(static::once())->method('getOptions')->willReturn([$option]); $optionValue = $this->getMockForAbstractClass(OptionValueInterface::class); $this->optionValueFactory->expects(static::once())->method('create')->willReturn($optionValue); $optionValue->expects(static::once())->method('setValueIndex')->with($option['value_index']); $attribute->expects(static::once())->method('setValues')->with([$optionValue]); $options = $this->loader->load($this->product); static::assertSame([$attribute], $options); }
/** * Prepare form before rendering HTML * * @return $this */ protected function _prepareForm() { /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(); $fieldset = $form->addFieldset('settings', ['legend' => __('Select Configurable Attributes')]); $fieldset->addField('configurable-attribute-selector', 'text', ['label' => 'Select Attribute', 'title' => 'Select Attribute']); $product = $this->getProduct(); $usedAttributes = $product->getTypeId() == Configurable::TYPE_CODE ? $this->_configurableType->getUsedProductAttributes($product) : []; foreach ($usedAttributes as $attribute) { /** @var $attribute \Magento\Catalog\Model\Resource\Eav\Attribute */ if ($this->_configurableType->canUseAttribute($attribute, $product)) { $fieldset->addField('attribute_' . $attribute->getAttributeId(), 'checkbox', ['label' => $attribute->getFrontendLabel(), 'title' => $attribute->getFrontendLabel(), 'name' => 'attributes[]', 'class' => 'configurable-attribute-checkbox', 'value' => $attribute->getAttributeId(), 'checked' => true]); } } $fieldset->addField('continue_button', 'note', ['text' => $this->getChildHtml('continue_button')]); $this->setForm($form); return parent::_prepareForm(); }
/** * Initialize data for configurable product * * @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject * @param \Magento\Catalog\Model\Product $product * * @return \Magento\Catalog\Model\Product * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterInitialize(\Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject, \Magento\Catalog\Model\Product $product) { $attributes = $this->request->getParam('attributes'); if ($product->getTypeId() == ConfigurableProduct::TYPE_CODE && !empty($attributes)) { $setId = $this->request->getPost('new-variations-attribute-set-id'); $product->setAttributeSetId($setId); $this->productType->setUsedProductAttributeIds($attributes, $product); $product->setNewVariationsAttributeSetId($setId); $associatedProductIds = $this->request->getPost('associated_product_ids', []); $variationsMatrix = $this->request->getParam('variations-matrix', []); if (!empty($variationsMatrix)) { $generatedProductIds = $this->variationHandler->generateSimpleProducts($product, $variationsMatrix); $associatedProductIds = array_merge($associatedProductIds, $generatedProductIds); } $product->setAssociatedProductIds(array_filter($associatedProductIds)); $product->setCanSaveConfigurableAttributes((bool) $this->request->getPost('affect_configurable_product_attributes')); } return $product; }
public function testCanUseAttribute() { $attribute = $this->getMock('Magento\\Catalog\\Model\\Resource\\Eav\\Attribute', array('getIsGlobal', 'getIsVisible', 'getIsConfigurable', 'usesSource', 'getIsUserDefined', '__wakeup', '__sleep'), array(), '', false); $attribute->expects($this->once())->method('getIsGlobal')->will($this->returnValue(1)); $attribute->expects($this->once())->method('getIsVisible')->will($this->returnValue(1)); $attribute->expects($this->once())->method('getIsConfigurable')->will($this->returnValue(1)); $attribute->expects($this->once())->method('usesSource')->will($this->returnValue(1)); $attribute->expects($this->once())->method('getIsUserDefined')->will($this->returnValue(1)); $this->assertTrue($this->_model->canUseAttribute($attribute)); }
/** * @expectedException \Magento\Framework\Exception\NoSuchEntityException */ public function testRemoveNoSuchEntityException() { $productSku = 'productSku'; $optionId = 3; $this->productRepositoryMock->expects($this->once())->method('get')->with($this->equalTo($productSku))->will($this->returnValue($this->productMock)); $this->productMock->expects($this->once())->method('getTypeId')->will($this->returnValue(ConfigurableType::TYPE_CODE)); $this->productTypeMock->expects($this->once())->method('getConfigurableAttributeCollection')->with($this->equalTo($this->productMock))->will($this->returnValue($this->attributeCollectionMock)); $this->attributeCollectionMock->expects($this->once())->method('getItemById')->with($this->equalTo($optionId))->will($this->returnValue(null)); $this->writeService->remove($productSku, $optionId); }
/** * Select one of the options and "prepare for cart" with a proper buy request * * @return ProductInterface */ protected function _prepareForCart() { $product = $this->productRepository->getById(1, true); $attributes = $this->model->getConfigurableAttributesAsArray($product); $attribute = reset($attributes); $optionValueId = $attribute['values'][0]['value_index']; $buyRequest = new \Magento\Framework\DataObject(['qty' => 5, 'super_attribute' => [$attribute['attribute_id'] => $optionValueId]]); $this->model->prepareForCart($buyRequest, $product); return $product; }
/** * {@inheritdoc} */ public function deleteById($sku, $id) { $product = $this->getProduct($sku); $attributeCollection = $this->configurableType->getConfigurableAttributeCollection($product); /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $option */ $option = $attributeCollection->getItemById($id); if ($option === null) { throw new NoSuchEntityException(__('Requested option doesn\'t exist')); } return $this->delete($option); }
/** * {@inheritdoc} */ public function remove($productSku, $optionId) { $product = $this->getProduct($productSku); $attributeCollection = $this->productType->getConfigurableAttributeCollection($product); /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $option */ $option = $attributeCollection->getItemById($optionId); if ($option === null) { throw new NoSuchEntityException('Requested option doesn\'t exist'); } $option->delete(); return true; }
/** * @expectedException \Magento\Framework\Exception\NoSuchEntityException */ public function testRemoveChildInvalidChildSku() { $productSku = 'configurable'; $childSku = 'simple_10'; $this->product->expects($this->any())->method('getTypeId')->will($this->returnValue(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE)); $this->productRepository->expects($this->any())->method('get')->will($this->returnValue($this->product)); $option = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['getSku', 'getId', '__wakeup'])->disableOriginalConstructor()->getMock(); $option->expects($this->any())->method('getSku')->will($this->returnValue($childSku . '_invalid')); $option->expects($this->any())->method('getId')->will($this->returnValue(10)); $this->productType->expects($this->once())->method('getUsedProducts')->will($this->returnValue([$option])); $this->service->removeChild($productSku, $childSku); }
public function testSetImageFromChildProduct() { $productMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['__wakeup', 'hasData', 'getData', 'setImage'])->disableOriginalConstructor()->getMock(); $childProductMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['__wakeup', 'getData'])->disableOriginalConstructor()->getMock(); $productMock->expects($this->at(0))->method('getData')->with('image')->willReturn('no_selection'); $productMock->expects($this->at(1))->method('getData')->with('image')->willReturn('no_selection'); $productMock->expects($this->once())->method('hasData')->with('_cache_instance_products')->willReturn(true); $productMock->expects($this->at(3))->method('getData')->with('_cache_instance_products')->willReturn([$childProductMock]); $childProductMock->expects($this->any())->method('getData')->with('image')->willReturn('image_data'); $productMock->expects($this->once())->method('setImage')->with('image_data')->willReturnSelf(); $this->_model->setImageFromChildProduct($productMock); }
public function testPrepareAttributeSet() { $productMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['getNewVariationsAttributeSetId'])->disableOriginalConstructor()->getMock(); $attributeMock = $this->getMockBuilder('\\Magento\\Eav\\Model\\Entity\\Attribute')->setMethods(['isInSet', 'setAttributeSetId', 'setAttributeGroupId', 'save'])->disableOriginalConstructor()->getMock(); $attributeSetMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Set')->setMethods(['load', 'addSetInfo', 'getDefaultGroupId'])->disableOriginalConstructor()->getMock(); $eavEntityMock = $this->getMockBuilder('\\Magento\\Eav\\Model\\Entity')->setMethods(['setType', 'getTypeId'])->disableOriginalConstructor()->getMock(); $productMock->expects($this->once())->method('getNewVariationsAttributeSetId')->willReturn('new_attr_set_id'); $this->configurableProduct->expects($this->once())->method('getUsedProductAttributes')->with($productMock)->willReturn([$attributeMock]); $this->attributeSetFactory->expects($this->once())->method('create')->willReturn($attributeSetMock); $attributeSetMock->expects($this->once())->method('load')->with('new_attr_set_id')->willReturnSelf(); $this->entityFactoryMock->expects($this->once())->method('create')->willReturn($eavEntityMock); $eavEntityMock->expects($this->once())->method('setType')->with('catalog_product')->willReturnSelf(); $eavEntityMock->expects($this->once())->method('getTypeId')->willReturn('type_id'); $attributeSetMock->expects($this->once())->method('addSetInfo')->with('type_id', [$attributeMock]); $attributeMock->expects($this->once())->method('isInSet')->with('new_attr_set_id')->willReturn(false); $attributeMock->expects($this->once())->method('setAttributeSetId')->with('new_attr_set_id')->willReturnSelf(); $attributeSetMock->expects($this->once())->method('getDefaultGroupId')->with('new_attr_set_id')->willReturn('default_group_id'); $attributeMock->expects($this->once())->method('setAttributeGroupId')->with('default_group_id')->willReturnSelf(); $attributeMock->expects($this->once())->method('save')->willReturnSelf(); $this->model->prepareAttributeSet($productMock); }
/** * @expectedException \Magento\Framework\Exception\NoSuchEntityException * @expectedExceptionMessage Requested option doesn't exist: 3 */ public function testGetNoSuchEntityException() { $productSku = 'oneSku'; $optionId = 3; $this->productRepository->expects($this->once())->method('get')->with($this->equalTo($productSku))->will($this->returnValue($this->product)); $this->product->expects($this->once())->method('getTypeId')->will($this->returnValue(ConfigurableType::TYPE_CODE)); $this->productType->expects($this->once())->method('getConfigurableAttributeCollection')->with($this->equalTo($this->product))->will($this->returnValue($this->configurableAttributeCollection)); $this->configurableAttributeCollection->expects($this->once())->method('addFieldToFilter')->with(self::ATTRIBUTE_ID_FIELD_NAME, $optionId)->will($this->returnSelf()); $this->configurableAttributeCollection->expects($this->once())->method('getFirstItem')->will($this->returnValue($this->option)); $this->option->expects($this->once())->method('getId')->will($this->returnValue(null)); $this->attributeResource->expects($this->once())->method('getIdFieldName')->will($this->returnValue(self::ATTRIBUTE_ID_FIELD_NAME)); $this->configurableAttributeCollection->expects($this->once())->method('getResource')->will($this->returnValue($this->attributeResource)); $this->model->get($productSku, $optionId); }
/** * @param \Magento\Catalog\Controller\Adminhtml\Product\Builder $subject * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request * * @return \Magento\Catalog\Model\Product * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function aroundBuild(\Magento\Catalog\Controller\Adminhtml\Product\Builder $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request) { $product = $proceed($request); if ($request->has('attributes')) { $attributes = $request->getParam('attributes'); if (!empty($attributes)) { $product->setTypeId(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE); $this->configurableType->setUsedProductAttributeIds($attributes, $product); } else { $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE); } } // Required attributes of simple product for configurable creation if ($request->getParam('popup') && ($requiredAttributes = $request->getParam('required'))) { $requiredAttributes = explode(",", $requiredAttributes); foreach ($product->getAttributes() as $attribute) { if (in_array($attribute->getId(), $requiredAttributes)) { $attribute->setIsRequired(1); } } } if ($request->getParam('popup') && $request->getParam('product') && !is_array($request->getParam('product')) && $request->getParam('id', false) === false) { $configProduct = $this->productFactory->create(); $configProduct->setStoreId(0)->load($request->getParam('product'))->setTypeId($request->getParam('type')); $data = []; foreach ($configProduct->getTypeInstance()->getEditableAttributes($configProduct) as $attribute) { /* @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ if (!$attribute->getIsUnique() && $attribute->getFrontend()->getInputType() != 'gallery' && $attribute->getAttributeCode() != 'required_options' && $attribute->getAttributeCode() != 'has_options' && $attribute->getAttributeCode() != $configProduct->getIdFieldName()) { $data[$attribute->getAttributeCode()] = $configProduct->getData($attribute->getAttributeCode()); } } $product->addData($data); $product->setWebsiteIds($configProduct->getWebsiteIds()); } return $product; }
/** * Retrieve actual list of associated products (i.e. if product contains variations matrix form data * - previously saved in database relations are not considered) * * @return Product[] */ protected function _getAssociatedProducts() { $product = $this->getProduct(); $ids = $this->getProduct()->getAssociatedProductIds(); if ($ids === null) { // form data overrides any relations stored in database return $this->_configurableType->getUsedProducts($product); } $products = []; foreach ($ids as $productId) { try { $products[] = $this->productRepository->getById($productId); } catch (NoSuchEntityException $e) { continue; } } return $products; }
/** * Retrieve actual list of associated products (i.e. if product contains variations matrix form data * - previously saved in database relations are not considered) * * @return Product[] */ protected function _getAssociatedProducts() { $product = $this->getProduct(); $ids = $this->getProduct()->getAssociatedProductIds(); if ($ids === null) { // form data overrides any relations stored in database return $this->_configurableType->getUsedProducts($product); } $products = array(); foreach ($ids as $productId) { /** @var $product Product */ $product = $this->_productFactory->create()->load($productId); if ($product->getId()) { $products[] = $product; } } return $products; }