public function testGetAttributes() { // fixture required $this->_model->load(1); $attributes = $this->_model->getAttributes(); $this->assertArrayHasKey('name', $attributes); $this->assertArrayHasKey('sku', $attributes); $this->assertInstanceOf('Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute', $attributes['sku']); }
/** * Return Product attribute by attribute's ID * * @param \Magento\Catalog\Model\Product $product * @param int $attributeId * @return null|\Magento\Catalog\Model\Entity\Attribute Product's attribute */ public function getProductAttribute(\Magento\Catalog\Model\Product $product, $attributeId) { if (!isset($this->_productAttributes[$product->getId()])) { $attributes = $product->getAttributes(); foreach ($attributes as $attribute) { $this->_productAttributes[$product->getId()][$attribute->getAttributeId()] = $attribute; } } return isset($this->_productAttributes[$product->getId()][$attributeId]) ? $this->_productAttributes[$product->getId()][$attributeId] : null; }
public function testGetAttributes() { $productType = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Type\\AbstractType')->setMethods(['getEditableAttributes'])->disableOriginalConstructor()->getMockForAbstractClass(); $this->productTypeInstanceMock->expects($this->any())->method('factory')->will($this->returnValue($productType)); $attribute = $this->getMockBuilder('\\Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute')->setMethods(['__wakeup', 'isInGroup'])->disableOriginalConstructor()->getMockForAbstractClass(); $attribute->expects($this->any())->method('isInGroup')->will($this->returnValue(true)); $productType->expects($this->any())->method('getEditableAttributes')->will($this->returnValue([$attribute])); $expect = [$attribute]; $this->assertEquals($expect, $this->model->getAttributes(5)); $this->assertEquals($expect, $this->model->getAttributes()); }
/** * Loads the values from a product model * * @param \Magento\Catalog\Model\Product $productModel * @return void */ protected function populateBuilderWithAttributes(\Magento\Catalog\Model\Product $productModel) { $attributes = array(); foreach ($productModel->getAttributes() as $attribute) { $attrCode = $attribute->getAttributeCode(); $value = $productModel->getDataUsingMethod($attrCode) ?: $productModel->getData($attrCode); if (null !== $value) { if ($attrCode != 'entity_id') { $attributes[$attrCode] = $value; } } } $attributes[ProductDataObject::STORE_ID] = $productModel->getStoreId(); $this->productBuilder->populateWithArray($attributes); return; }
/** * Initialize product from data * * @param \Magento\Catalog\Model\Product $product * @param array $productData * @return \Magento\Catalog\Model\Product * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function initializeFromData(\Magento\Catalog\Model\Product $product, array $productData) { unset($productData['custom_attributes']); unset($productData['extension_attributes']); if ($productData) { $stockData = isset($productData['stock_data']) ? $productData['stock_data'] : []; $productData['stock_data'] = $this->stockFilter->filter($stockData); } $productData = $this->normalize($productData); if (!empty($productData['is_downloadable'])) { $productData['product_has_weight'] = 0; } foreach (['category_ids', 'website_ids'] as $field) { if (!isset($productData[$field])) { $productData[$field] = []; } } foreach ($productData['website_ids'] as $websiteId => $checkboxValue) { if (!$checkboxValue) { unset($productData['website_ids'][$websiteId]); } } $wasLockedMedia = false; if ($product->isLockedAttribute('media')) { $product->unlockAttribute('media'); $wasLockedMedia = true; } $dateFieldFilters = []; $attributes = $product->getAttributes(); foreach ($attributes as $attrKey => $attribute) { if ($attribute->getBackend()->getType() == 'datetime') { if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') { $dateFieldFilters[$attrKey] = $this->getDateTimeFilter(); } } } $inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $productData); $productData = $inputFilter->getUnescaped(); if (isset($productData['options'])) { $productOptions = $productData['options']; unset($productData['options']); } else { $productOptions = []; } $product->addData($productData); if ($wasLockedMedia) { $product->lockAttribute('media'); } if ($this->storeManager->hasSingleStore() && empty($product->getWebsiteIds())) { $product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]); } /** * Check "Use Default Value" checkboxes values */ $useDefaults = (array) $this->request->getPost('use_default', []); foreach ($useDefaults as $attributeCode => $useDefaultState) { if ($useDefaultState) { $product->setData($attributeCode, null); } } $product = $this->setProductLinks($product); /** * Initialize product options */ if ($productOptions && !$product->getOptionsReadonly()) { // mark custom options that should to fall back to default value $options = $this->mergeProductOptions($productOptions, $this->request->getPost('options_use_default')); $customOptions = []; foreach ($options as $customOptionData) { if (empty($customOptionData['is_delete'])) { if (isset($customOptionData['values'])) { $customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) { return empty($valueData['is_delete']); }); } $customOption = $this->getCustomOptionFactory()->create(['data' => $customOptionData]); $customOption->setProductSku($product->getSku()); $customOption->setOptionId(null); $customOptions[] = $customOption; } } $product->setOptions($customOptions); } $product->setCanSaveCustomOptions(!empty($productData['affect_product_custom_options']) && !$product->getOptionsReadonly()); return $product; }
/** * @param Product $product * @return array */ protected function getRequiredDataFromProduct(Product $product) { $parentProductData = []; foreach ($product->getAttributes() as $attribute) { if ($attribute->getIsUserDefined() && $attribute->getIsRequired()) { $parentProductData[$attribute->getAttributeCode()] = $product->getData($attribute->getAttributeCode()); } } return $parentProductData; }
/** * Initialize product before saving * * @param \Magento\Catalog\Model\Product $product * @return \Magento\Catalog\Model\Product * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function initialize(\Magento\Catalog\Model\Product $product) { $productData = $this->request->getPost('product'); unset($productData['custom_attributes']); unset($productData['extension_attributes']); if ($productData) { $stockData = isset($productData['stock_data']) ? $productData['stock_data'] : []; $productData['stock_data'] = $this->stockFilter->filter($stockData); } foreach (['category_ids', 'website_ids'] as $field) { if (!isset($productData[$field])) { $productData[$field] = []; } } $wasLockedMedia = false; if ($product->isLockedAttribute('media')) { $product->unlockAttribute('media'); $wasLockedMedia = true; } $dateFieldFilters = []; $attributes = $product->getAttributes(); foreach ($attributes as $attrKey => $attribute) { if ($attribute->getBackend()->getType() == 'datetime') { if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') { $dateFieldFilters[$attrKey] = $this->dateFilter; } } } $inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $productData); $productData = $inputFilter->getUnescaped(); $product->addData($productData); if ($wasLockedMedia) { $product->lockAttribute('media'); } if ($this->storeManager->hasSingleStore()) { $product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]); } /** * Check "Use Default Value" checkboxes values */ $useDefaults = $this->request->getPost('use_default'); if ($useDefaults) { foreach ($useDefaults as $attributeCode) { $product->setData($attributeCode, false); } } $links = $this->request->getPost('links'); $links = is_array($links) ? $links : []; $linkTypes = ['related', 'upsell', 'crosssell']; foreach ($linkTypes as $type) { if (isset($links[$type])) { $links[$type] = $this->jsHelper->decodeGridSerializedInput($links[$type]); } } $product = $this->productLinks->initializeLinks($product, $links); $productLinks = $product->getProductLinks(); $linkTypes = ['related' => $product->getRelatedReadonly(), 'upsell' => $product->getUpsellReadonly(), 'crosssell' => $product->getCrosssellReadonly()]; foreach ($linkTypes as $linkType => $readonly) { if (isset($links[$linkType]) && !$readonly) { foreach ($links[$linkType] as $linkId => $linkData) { $linkProduct = $this->productRepository->getById($linkId); $link = $this->productLinkFactory->create(); $link->setSku($product->getSku())->setLinkedProductSku($linkProduct->getSku())->setLinkType($linkType)->setPosition(isset($linkData['position']) ? (int) $linkData['position'] : 0); $productLinks[] = $link; } } } $product->setProductLinks($productLinks); /** * Initialize product options */ if (isset($productData['options']) && !$product->getOptionsReadonly()) { // mark custom options that should to fall back to default value $options = $this->mergeProductOptions($productData['options'], $this->request->getPost('options_use_default')); $customOptions = []; foreach ($options as $customOptionData) { if (!(bool) $customOptionData['is_delete']) { $customOption = $this->customOptionFactory->create(['data' => $customOptionData]); $customOption->setProductSku($product->getSku()); $customOption->setOptionId(null); $customOptions[] = $customOption; } } $product->setOptions($customOptions); } $product->setCanSaveCustomOptions((bool) $this->request->getPost('affect_product_custom_options') && !$product->getOptionsReadonly()); return $product; }
/** * {@inheritdoc} */ public function getAttributes($groupId = null, $skipSuper = false) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getAttributes'); if (!$pluginInfo) { return parent::getAttributes($groupId, $skipSuper); } else { return $this->___callPlugins('getAttributes', func_get_args(), $pluginInfo); } }
/** * @param Product $product * @return array */ protected function buildTags(Product $product) { $tags = []; foreach ($product->getAttributes() as $attr) { if ($attr->getIsVisibleOnFront() && $product->hasData($attr->getAttributeCode())) { $label = $attr->getStoreLabel(); $value = $attr->getFrontend()->getValue($product); if (is_string($label) && strlen($label) && is_string($value) && strlen($value)) { $tags[] = "{$label}: {$value}"; } } } if (!$product->canConfigure()) { $tags[] = \NostoProduct::PRODUCT_ADD_TO_CART; } return $tags; }