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()); }
/** * Save Custom labels for Attribute name * * @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $attribute * @return $this */ public function saveLabel($attribute) { $adapter = $this->_getWriteAdapter(); $select = $adapter->select()->from($this->_labelTable, 'value_id')->where('product_super_attribute_id = :product_super_attribute_id')->where('store_id = :store_id'); $bind = ['product_super_attribute_id' => (int) $attribute->getId(), 'store_id' => (int) $attribute->getStoreId()]; $valueId = $adapter->fetchOne($select, $bind); if ($valueId) { $adapter->update($this->_labelTable, ['use_default' => (int) $attribute->getUseDefault(), 'value' => $attribute->getLabel()], $adapter->quoteInto('value_id = ?', (int) $valueId)); } else { $adapter->insert($this->_labelTable, ['product_super_attribute_id' => (int) $attribute->getId(), 'store_id' => (int) $attribute->getStoreId(), 'use_default' => (int) $attribute->getUseDefault(), 'value' => $attribute->getLabel()]); } return $this; }
/** * Convert configurable attribute to option data object * * @param Attribute $configurableAttribute * @return \Magento\ConfigurableProduct\Service\V1\Data\Option */ public function convertFromModel(Attribute $configurableAttribute) { $values = []; $prices = $configurableAttribute->getPrices(); if (is_array($prices)) { foreach ($prices as $price) { $values[] = $this->valueBuilder->setIndex($price['value_index'])->setPrice($price['pricing_value'])->setPercent($price['is_percent'])->create(); } } $data = [Option::ID => $configurableAttribute->getId(), Option::ATTRIBUTE_ID => $configurableAttribute->getAttributeId(), Option::LABEL => $configurableAttribute->getLabel(), Option::TYPE => $configurableAttribute->getProductAttribute()->getFrontend()->getInputType(), Option::POSITION => $configurableAttribute->getPosition(), Option::USE_DEFAULT => $configurableAttribute->getData('use_default'), Option::VALUES => $values]; return $this->optionBuilder->populateWithArray($data)->create(); }
/** * Save Custom labels for Attribute name * * @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $attribute * @return $this */ public function saveLabel($attribute) { $connection = $this->getConnection(); $select = $connection->select()->from($this->_labelTable, 'value_id')->where('product_super_attribute_id = :product_super_attribute_id')->where('store_id = :store_id'); $bind = ['product_super_attribute_id' => (int) $attribute->getId(), 'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID]; $valueId = $connection->fetchOne($select, $bind); if ($valueId) { $storeId = (int) $attribute->getStoreId() ?: $this->_storeManager->getStore()->getId(); } else { // if attribute label not exists, always store on default store (0) $storeId = Store::DEFAULT_STORE_ID; } $connection->insertOnDuplicate($this->_labelTable, ['product_super_attribute_id' => (int) $attribute->getId(), 'use_default' => (int) $attribute->getUseDefault(), 'store_id' => $storeId, 'value' => $attribute->getLabel()], ['value', 'use_default']); return $this; }