/** * @param Attribute $attribute * @param array $config * @return array */ protected function getAttributeOptionsData($attribute, $config) { $attributeOptionsData = []; foreach ($attribute->getOptions() as $attributeOption) { $optionId = $attributeOption['value_index']; $attributeOptionsData[] = ['id' => $optionId, 'label' => $attributeOption['label'], 'products' => isset($config[$attribute->getAttributeId()][$optionId]) ? $config[$attribute->getAttributeId()][$optionId] : []]; } return $attributeOptionsData; }
public function testRemove() { $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($this->attributeMock)); $this->attributeMock->expects($this->once())->method('delete'); $this->assertTrue($this->writeService->remove($productSku, $optionId)); }
public function testGetLabel() { $this->assertEmpty($this->_model->getLabel()); $this->_model->setProductAttribute(new \Magento\Framework\Object(['store_label' => 'Store Label'])); $this->assertEquals('Store Label', $this->_model->getLabel()); $this->_model->setUseDefault(1)->setProductAttribute(new \Magento\Framework\Object(['store_label' => 'Other Label'])); $this->assertEquals('Other Label', $this->_model->getLabel()); }
/** * @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); }
public function testGetModelFromData() { $data = ['data']; $id = 33; $this->configurableAttribute->expects($this->any())->method('getData')->will($this->returnValue($data)); $this->configurableAttribute->expects($this->once())->method('setData')->with($this->equalTo($data)); $this->configurableAttribute->expects($this->once())->method('addData')->with($this->equalTo($data)); $this->configurableAttribute->expects($this->any())->method('getId')->will($this->returnValue($id)); $this->configurableAttribute->expects($this->once())->method('setId')->with($this->equalTo($id)); $this->configurableAttribute->expects($this->any())->method('getAttributeId')->will($this->returnValue($id)); $this->configurableAttribute->expects($this->once())->method('setAttributeId')->with($this->equalTo($id)); $this->configurableAttribute->expects($this->any())->method('getValues')->will($this->returnValue($data)); $this->configurableAttribute->expects($this->any())->method('setValues')->with($this->equalTo($data)); $this->option->expects($this->any())->method('getValues')->will($this->returnValue([$this->value])); $this->option->expects($this->any())->method('__toArray')->will($this->returnValue($data)); $this->valueConverter->expects($this->any())->method('convertArrayFromData')->with($this->equalTo($this->value))->will($this->returnValue($data[0])); $result = $this->converter->getModelFromData($this->option, $this->configurableAttribute); $this->assertEquals($this->configurableAttribute, $result); }
/** * Load prices for configurable attribute * * @param ConfigurableAttribute $object * @return $this */ protected function loadPrices(ConfigurableAttribute $object) { $websiteId = $this->_catalogData->isPriceGlobal() ? 0 : (int) $this->_storeManager->getStore()->getWebsiteId(); $select = $this->_getReadAdapter()->select()->from($this->_priceTable)->where('product_super_attribute_id = ?', $object->getId())->where('website_id = ?', $websiteId); foreach ($select->query() as $row) { $data = ['value_index' => $row['value_index'], 'is_percent' => $row['is_percent'], 'pricing_value' => $row['pricing_value']]; $object->addPrice($data); } return $this; }
/** * Save Options prices (Depends from price save scope) * * @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $attribute * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function savePrices($attribute) { $write = $this->_getWriteAdapter(); // define website id scope if ($this->getCatalogHelper()->isPriceGlobal()) { $websiteId = 0; } else { $websiteId = (int) $this->_storeManager->getStore($attribute->getStoreId())->getWebsite()->getId(); } $values = $attribute->getValues(); if (!is_array($values)) { $values = array(); } $new = array(); $old = array(); // retrieve old values $select = $write->select()->from($this->_priceTable)->where('product_super_attribute_id = :product_super_attribute_id')->where('website_id = :website_id'); $bind = array('product_super_attribute_id' => (int) $attribute->getId(), 'website_id' => $websiteId); $rowSet = $write->fetchAll($select, $bind); foreach ($rowSet as $row) { $key = implode('-', array($row['website_id'], $row['value_index'])); if (!isset($old[$key])) { $old[$key] = $row; } else { // delete invalid (duplicate row) $where = $write->quoteInto('value_id = ?', $row['value_id']); $write->delete($this->_priceTable, $where); } } // prepare new values foreach ($values as $v) { if (empty($v['value_index'])) { continue; } $key = implode('-', array($websiteId, $v['value_index'])); $new[$key] = array('value_index' => $v['value_index'], 'pricing_value' => $v['pricing_value'], 'is_percent' => $v['is_percent'], 'website_id' => $websiteId, 'use_default' => !empty($v['use_default_value']) ? true : false); } $insert = array(); $update = array(); $delete = array(); foreach ($old as $k => $v) { if (!isset($new[$k])) { $delete[] = $v['value_id']; } } foreach ($new as $k => $v) { $needInsert = false; $needUpdate = false; $needDelete = false; $isGlobal = true; if (!$this->getCatalogHelper()->isPriceGlobal() && $websiteId != 0) { $isGlobal = false; } $hasValue = $isGlobal && !empty($v['pricing_value']) || !$isGlobal && !$v['use_default']; if (isset($old[$k])) { // data changed $dataChanged = $old[$k]['is_percent'] != $v['is_percent'] || $old[$k]['pricing_value'] != $v['pricing_value']; if (!$hasValue) { $needDelete = true; } else { if ($dataChanged) { $needUpdate = true; } } } else { if ($hasValue) { $needInsert = true; } } if (!$isGlobal && empty($v['pricing_value'])) { $v['pricing_value'] = 0; $v['is_percent'] = 0; } if ($needInsert) { $insert[] = array('product_super_attribute_id' => $attribute->getId(), 'value_index' => $v['value_index'], 'is_percent' => $v['is_percent'], 'pricing_value' => $v['pricing_value'], 'website_id' => $websiteId); } if ($needUpdate) { $update[$old[$k]['value_id']] = array('is_percent' => $v['is_percent'], 'pricing_value' => $v['pricing_value']); } if ($needDelete) { $delete[] = $old[$k]['value_id']; } } if (!empty($delete)) { $where = $write->quoteInto('value_id IN(?)', $delete); $write->delete($this->_priceTable, $where); } if (!empty($update)) { foreach ($update as $valueId => $bind) { $where = $write->quoteInto('value_id=?', $valueId); $write->update($this->_priceTable, $bind, $where); } } if (!empty($insert)) { $write->insertMultiple($this->_priceTable, $insert); } return $this; }
/** * Load label for configurable attribute * * @param ConfigurableAttribute $object * @return $this */ protected function loadLabel(ConfigurableAttribute $object) { $storeId = (int) $this->_storeManager->getStore()->getId(); $connection = $this->_getReadAdapter(); $useDefaultCheck = $connection->getCheckSql('store.use_default IS NULL', 'def.use_default', 'store.use_default'); $labelCheck = $connection->getCheckSql('store.value IS NULL', 'def.value', 'store.value'); $select = $connection->select()->from(['def' => $this->_labelTable])->joinLeft(['store' => $this->_labelTable], $connection->quoteInto('store.product_super_attribute_id = def.product_super_attribute_id AND store.store_id = ?', $storeId), ['use_default' => $useDefaultCheck, 'label' => $labelCheck])->where('def.product_super_attribute_id = ?', $object->getId())->where('def.store_id = ?', 0); $data = $connection->fetchRow($select); $object->setLabel($data['label']); $object->setUseDefault($data['use_default']); return $this; }
/** * @param Option $option * @param Attribute $configurableAttribute * @return Attribute */ public function getModelFromData(Option $option, Attribute $configurableAttribute) { /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $returnConfigurableAttribute */ $returnConfigurableAttribute = $this->attributeFactory->create(); $returnConfigurableAttribute->setData($configurableAttribute->getData()); $returnConfigurableAttribute->addData($option->__toArray()); $returnConfigurableAttribute->setId($configurableAttribute->getId()); $returnConfigurableAttribute->setAttributeId($configurableAttribute->getAttributeId()); $returnConfigurableAttribute->setValues($configurableAttribute->getPrices()); $values = $option->getValues(); if (!is_null($values)) { $prices = []; foreach ($values as $value) { $prices[] = $this->valueConverter->convertArrayFromData($value); } $returnConfigurableAttribute->setValues($prices); } return $returnConfigurableAttribute; }