/** * Save image information to DB. * * @param \Magento\Catalog\Model\Product $product * @param array $images * @return void */ protected function storeImage($product, $images) { $baseImage = ''; $i = 1; foreach ($images as $image) { if (empty($image)) { $this->errors[] = $product->getSku(); continue; } if (strpos($image, '_main') !== false) { $baseImage = $image; } $id = $this->galleryAttribute->insertGallery(['attribute_id' => $this->mediaAttribute->getAttributeId(), 'entity_id' => $product->getId(), 'value' => $image]); $this->galleryAttribute->insertGalleryValueInStore(['value_id' => $id, 'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, 'entity_id' => $product->getId(), 'label' => 'Image', 'position' => $i, 'disables' => 0]); $i++; } if (empty($baseImage)) { $baseImage = $images[0]; } if ($baseImage) { $imageAttribute = $product->getResource()->getAttribute('image'); $smallImageAttribute = $product->getResource()->getAttribute('small_image'); $thumbnailAttribute = $product->getResource()->getAttribute('thumbnail'); $adapter = $product->getResource()->getWriteConnection(); foreach ([$imageAttribute, $smallImageAttribute, $thumbnailAttribute] as $attribute) { $table = $imageAttribute->getBackend()->getTable(); /** @var \Magento\Framework\DB\Adapter\AdapterInterface $adapter*/ $data = [$attribute->getBackend()->getEntityIdField() => $product->getId(), 'attribute_id' => $attribute->getId(), 'value' => $baseImage]; $adapter->insertOnDuplicate($table, $data, ['value']); } } }
/** * Insert entity attribute value * * @param \Magento\Framework\Object $object * @param AbstractAttribute $attribute * @param mixed $value * @return $this */ protected function _insertAttribute($object, $attribute, $value) { /** * save required attributes in global scope every time if store id different from default */ $storeId = (int) $this->_storeManager->getStore($object->getStoreId())->getId(); if ($this->getDefaultStoreId() != $storeId) { if ($attribute->getIsRequired() || $attribute->getIsRequiredInAdminStore()) { $table = $attribute->getBackend()->getTable(); $select = $this->_getReadAdapter()->select()->from($table)->where('entity_type_id = ?', $attribute->getEntityTypeId())->where('attribute_id = ?', $attribute->getAttributeId())->where('store_id = ?', $this->getDefaultStoreId())->where('entity_id = ?', $object->getEntityId()); $row = $this->_getReadAdapter()->fetchOne($select); if (!$row) { $data = new \Magento\Framework\Object(array('entity_type_id' => $attribute->getEntityTypeId(), 'attribute_id' => $attribute->getAttributeId(), 'store_id' => $this->getDefaultStoreId(), 'entity_id' => $object->getEntityId(), 'value' => $this->_prepareValueForSave($value, $attribute))); $bind = $this->_prepareDataForTable($data, $table); $this->_getWriteAdapter()->insertOnDuplicate($table, $bind, array('value')); } } } return $this->_saveAttributeValue($object, $attribute, $value); }
/** * Insert or Update attribute data * * @param \Magento\Catalog\Model\AbstractModel $object * @param AbstractAttribute $attribute * @param mixed $value * @return $this */ protected function _saveAttributeValue($object, $attribute, $value) { $connection = $this->getConnection(); $storeId = (int) $this->_storeManager->getStore($object->getStoreId())->getId(); $table = $attribute->getBackend()->getTable(); $entityId = $this->resolveEntityId($object->getId(), $table); /** * If we work in single store mode all values should be saved just * for default store id * In this case we clear all not default values */ if ($this->_storeManager->hasSingleStore()) { $storeId = $this->getDefaultStoreId(); $connection->delete($table, ['attribute_id = ?' => $attribute->getAttributeId(), $this->getLinkField() . ' = ?' => $entityId, 'store_id <> ?' => $storeId]); } $data = new \Magento\Framework\DataObject(['attribute_id' => $attribute->getAttributeId(), 'store_id' => $storeId, $this->getLinkField() => $entityId, 'value' => $this->_prepareValueForSave($value, $attribute)]); $bind = $this->_prepareDataForTable($data, $table); if ($attribute->isScopeStore()) { /** * Update attribute value for store */ $this->_attributeValuesToSave[$table][] = $bind; } elseif ($attribute->isScopeWebsite() && $storeId != $this->getDefaultStoreId()) { /** * Update attribute value for website */ $storeIds = $this->_storeManager->getStore($storeId)->getWebsite()->getStoreIds(true); foreach ($storeIds as $storeId) { $bind['store_id'] = (int) $storeId; $this->_attributeValuesToSave[$table][] = $bind; } } else { /** * Update global attribute value */ $bind['store_id'] = $this->getDefaultStoreId(); $this->_attributeValuesToSave[$table][] = $bind; } return $this; }
/** * Load options for attribute * * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $superAttribute * @param int $productId * @return array */ public function getAttributeOptions($superAttribute, $productId) { $scope = $this->getScopeResolver()->getScope(); $select = $this->getConnection()->select()->from(['super_attribute' => $this->getTable('catalog_product_super_attribute')], ['sku' => 'entity.sku', 'product_id' => 'product_entity.entity_id', 'attribute_code' => 'attribute.attribute_code', 'value_index' => 'entity_value.value', 'option_title' => $this->getConnection()->getIfNullSql('option_value.value', 'default_option_value.value'), 'default_title' => 'default_option_value.value'])->joinInner(['product_entity' => $this->getTable('catalog_product_entity')], "product_entity.{$this->getProductEntityLinkField()} = super_attribute.product_id", [])->joinInner(['product_link' => $this->getTable('catalog_product_super_link')], 'product_link.parent_id = super_attribute.product_id', [])->joinInner(['attribute' => $this->getTable('eav_attribute')], 'attribute.attribute_id = super_attribute.attribute_id', [])->joinInner(['entity' => $this->getTable('catalog_product_entity')], 'entity.entity_id = product_link.product_id', [])->joinInner(['entity_value' => $superAttribute->getBackendTable()], implode(' AND ', ['entity_value.attribute_id = super_attribute.attribute_id', 'entity_value.store_id = 0', "entity_value.{$this->getProductEntityLinkField()} = " . "entity.{$this->getProductEntityLinkField()}"]), [])->joinLeft(['option_value' => $this->getTable('eav_attribute_option_value')], implode(' AND ', ['option_value.option_id = entity_value.value', 'option_value.store_id = ' . $scope->getId()]), [])->joinLeft(['default_option_value' => $this->getTable('eav_attribute_option_value')], implode(' AND ', ['default_option_value.option_id = entity_value.value', 'default_option_value.store_id = ' . \Magento\Store\Model\Store::DEFAULT_STORE_ID]), [])->where('super_attribute.product_id = ?', $productId)->where('attribute.attribute_id = ?', $superAttribute->getAttributeId()); return $this->getConnection()->fetchAll($select); }
/** * Insert entity attribute value * * @param \Magento\Framework\DataObject $object * @param AbstractAttribute $attribute * @param mixed $value * @return $this */ protected function _insertAttribute($object, $attribute, $value) { /** * save required attributes in global scope every time if store id different from default */ $storeId = (int) $this->_storeManager->getStore($object->getStoreId())->getId(); if ($this->getDefaultStoreId() != $storeId) { if ($attribute->getIsRequired() || $attribute->getIsRequiredInAdminStore()) { $table = $attribute->getBackend()->getTable(); $select = $this->getConnection()->select()->from($table)->where('attribute_id = ?', $attribute->getAttributeId())->where('store_id = ?', $this->getDefaultStoreId())->where($this->getLinkField() . ' = ?', $object->getData($this->getLinkField())); $row = $this->getConnection()->fetchOne($select); if (!$row) { $data = new \Magento\Framework\DataObject(['attribute_id' => $attribute->getAttributeId(), 'store_id' => $this->getDefaultStoreId(), $this->getLinkField() => $object->getData($this->getLinkField()), 'value' => $this->_prepareValueForSave($value, $attribute)]); $bind = $this->_prepareDataForTable($data, $table); $this->getConnection()->insertOnDuplicate($table, $bind, ['value']); } } } return $this->_saveAttributeValue($object, $attribute, $value); }