Ejemplo n.º 1
1
 /**
  * Returns array of fields
  *
  * @param string $entityType
  * @return array
  * @throws \Exception
  */
 public function getAttributes($entityType)
 {
     $metadata = $this->metadataPool->getMetadata($entityType);
     $searchResult = $this->attributeRepository->getList($metadata->getEavEntityType(), $this->searchCriteriaBuilder->addFilter('attribute_set_id', null, 'neq')->create());
     $attributes = [];
     foreach ($searchResult->getItems() as $attribute) {
         $attributes[] = $attribute->getAttributeCode();
     }
     return $attributes;
 }
Ejemplo n.º 2
0
 /**
  * @param string $entityType
  * @param object $entity
  * @return object
  * @throws CouldNotSaveException
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entityType, $entity)
 {
     /**
      * @var $entity \Magento\Catalog\Api\Data\ProductLinkInterface
      */
     $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku());
     $product = $this->productRepository->get($entity->getSku());
     $links = [];
     $extensions = $this->dataObjectProcessor->buildOutputDataArray($entity->getExtensionAttributes(), 'Magento\\Catalog\\Api\\Data\\ProductLinkExtensionInterface');
     $extensions = is_array($extensions) ? $extensions : [];
     $data = $entity->__toArray();
     foreach ($extensions as $attributeCode => $attribute) {
         $data[$attributeCode] = $attribute;
     }
     unset($data['extension_attributes']);
     $data['product_id'] = $linkedProduct->getId();
     $links[$linkedProduct->getId()] = $data;
     try {
         $linkTypesToId = $this->linkTypeProvider->getLinkTypes();
         $prodyctHydrator = $this->metadataPool->getHydrator(ProductInterface::class);
         $productData = $prodyctHydrator->extract($product);
         $this->linkResource->saveProductLinks($productData[$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()], $links, $linkTypesToId[$entity->getLinkType()]);
     } catch (\Exception $exception) {
         throw new CouldNotSaveException(__('Invalid data provided for linked products'));
     }
     return $entity;
 }
Ejemplo n.º 3
0
 /**
  * @param string $entityType
  * @param array $data
  * @return array
  */
 public function execute($entityType, $data)
 {
     $metadata = $this->metadataPool->getMetadata($entityType);
     $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
     $connection->update($metadata->getEntityTable(), $this->prepareData($metadata, $connection, $data), [$metadata->getLinkField() . ' = ?' => $data[$metadata->getLinkField()]]);
     return $data;
 }
Ejemplo n.º 4
0
 /**
  * @param string $entityType
  * @param object $entity
  * @param array $arguments
  * @return object
  * @throws \Exception
  */
 public function execute($entityType, $entity, $arguments = [])
 {
     $metadata = $this->metadataPool->getMetadata($entityType);
     $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
     $connection->beginTransaction();
     try {
         $this->eventManager->dispatch(
             'entity_manager_save_before',
             [
                 'entity_type' => $entityType,
                 'entity' => $entity
             ]
         );
         $this->eventManager->dispatchEntityEvent($entityType, 'save_before', ['entity' => $entity]);
         $entity = $this->updateMain->execute($entityType, $entity, $arguments);
         $entity = $this->updateAttributes->execute($entityType, $entity, $arguments);
         $entity = $this->updateExtensions->execute($entityType, $entity, $arguments);
         $this->eventManager->dispatchEntityEvent($entityType, 'save_after', ['entity' => $entity]);
         $this->eventManager->dispatch(
             'entity_manager_save_after',
             [
                 'entity_type' => $entityType,
                 'entity' => $entity
             ]
         );
         $connection->commit();
     } catch (\Exception $e) {
         $connection->rollBack();
         throw $e;
     }
     return $entity;
 }
Ejemplo n.º 5
0
 /**
  * @param string $entityType
  * @param object $entity
  * @param array $arguments
  * @return object
  * @throws \Exception
  */
 public function execute($entityType, $entity, $arguments = [])
 {
     $entityMetadata = $this->metadataPool->getMetadata($entityType);
     $linkField = $entityMetadata->getLinkField();
     $connection = $entityMetadata->getEntityConnection();
     $oldStores = $this->resourcePage->lookupStoreIds((int) $entity->getId());
     $newStores = (array) $entity->getStores();
     if (empty($newStores)) {
         $newStores = (array) $entity->getStoreId();
     }
     $table = $this->resourcePage->getTable('cms_page_store');
     $delete = array_diff($oldStores, $newStores);
     if ($delete) {
         $where = [$linkField . ' = ?' => (int) $entity->getData($linkField), 'store_id IN (?)' => $delete];
         $connection->delete($table, $where);
     }
     $insert = array_diff($newStores, $oldStores);
     if ($insert) {
         $data = [];
         foreach ($insert as $storeId) {
             $data[] = [$linkField => (int) $entity->getData($linkField), 'store_id' => (int) $storeId];
         }
         $connection->insertMultiple($table, $data);
     }
     return $entity;
 }
Ejemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
 {
     $installer = $setup;
     $installer->startSetup();
     $metadata = $this->metadataPool->getMetadata(CategoryInterface::class);
     $this->externalFKSetup->install($installer, $metadata->getEntityTable(), $metadata->getIdentifierField(), 'catalog_category_product', 'category_id');
     $installer->endSetup();
 }
 /**
  * {@inheritdoc}
  */
 public function build($productId)
 {
     $timestamp = $this->localeDate->scopeTimeStamp($this->storeManager->getStore());
     $currentDate = $this->dateTime->formatDate($timestamp, false);
     $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
     $productTable = $this->resource->getTableName('catalog_product_entity');
     return [$this->resource->getConnection()->select()->from(['parent' => $productTable], '')->joinInner(['link' => $this->resource->getTableName('catalog_product_relation')], "link.parent_id = parent.{$linkField}", [])->joinInner(['child' => $productTable], "child.entity_id = link.child_id", ['entity_id'])->joinInner(['t' => $this->resource->getTableName('catalogrule_product_price')], 't.product_id = child.entity_id', [])->where('parent.entity_id = ? ', $productId)->where('t.website_id = ?', $this->storeManager->getStore()->getWebsiteId())->where('t.customer_group_id = ?', $this->customerSession->getCustomerGroupId())->where('t.rule_date = ?', $currentDate)->order('t.rule_price ' . Select::SQL_ASC)->limit(1)];
 }
Ejemplo n.º 8
0
 /**
  * @param string $entityType
  * @param array $entityData
  * @param array $arguments
  * @return array
  * @throws \Exception
  */
 public function execute($entityType, $entityData, $arguments = [])
 {
     $linkField = $this->metadataPool->getMetadata($entityType)->getLinkField();
     $entityId = $entityData[$linkField];
     $entityData['customer_group_ids'] = $this->ruleResource->getCustomerGroupIds($entityId);
     $entityData['website_ids'] = $this->ruleResource->getWebsiteIds($entityId);
     return $entityData;
 }
Ejemplo n.º 9
0
 /**
  * @param string $entityType
  * @param string $identifier
  * @param array $context
  * @return array
  * @throws \Exception
  */
 public function execute($entityType, $identifier, $context = [])
 {
     $metadata = $this->metadataPool->getMetadata($entityType);
     $select = $metadata->getEntityConnection()->select()->from(['t' => $metadata->getEntityTable()])->where($metadata->getIdentifierField() . ' = ?', $identifier);
     foreach ($context as $field => $value) {
         $select->where($metadata->getEntityConnection()->quoteIdentifier($field) . ' = ?', $value);
     }
     $data = $metadata->getEntityConnection()->fetchRow($select);
     return $data ?: [];
 }
Ejemplo n.º 10
0
 /**
  * @param string $entityType
  * @param array $data
  * @return array
  */
 public function execute($entityType, $data)
 {
     $metadata = $this->metadataPool->getMetadata($entityType);
     $linkField = $metadata->getLinkField();
     $entityTable = $metadata->getEntityTable();
     $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
     $connection->insert($entityTable, $this->prepareData($metadata, $connection, $data));
     $data[$linkField] = $connection->lastInsertId($entityTable);
     return $data;
 }
Ejemplo n.º 11
0
 /**
  * @param CustomerInterface $entity
  * @param array $arguments
  * @return CustomerInterface
  * @throws \Exception
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entity, $arguments = [])
 {
     $metadata = $this->metadataPool->getMetadata(ExtensionAttributeInterface::class);
     $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
     $id = $connection->fetchOne($connection->select()->from($metadata->getEntityTable(), [$metadata->getIdentifierField()])->where('customer_id = ?', $entity->getId())->limit(1));
     $extensionAttribute = $this->extensionAttributeFactory->create();
     $extensionAttribute = $this->entityManager->load($extensionAttribute, $id);
     $customerExtension = $this->customerExtensionFactory->create(['data' => ['extension_attribute' => $extensionAttribute]]);
     $entity->setExtensionAttributes($customerExtension);
     return $entity;
 }
Ejemplo n.º 12
0
 /**
  * @param object $entity
  * @param array $arguments
  * @return bool
  * @throws \Exception
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entity, $arguments = [])
 {
     $entityType = $this->typeResolver->resolve($entity);
     $metadata = $this->metadataPool->getMetadata($entityType);
     $hydrator = $this->hydratorPool->getHydrator($entityType);
     $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
     $entityData = $hydrator->extract($entity);
     if (!isset($entityData[$metadata->getIdentifierField()])) {
         return false;
     }
     return (bool) $connection->fetchOne($connection->select()->from($metadata->getEntityTable(), [$metadata->getIdentifierField()])->where($metadata->getIdentifierField() . ' = ?', $entityData[$metadata->getIdentifierField()])->limit(1));
 }
Ejemplo n.º 13
0
 /**
  * @param string $entityType
  * @param array|null $entityData
  * @return \Magento\Framework\Model\Entity\ScopeInterface[]
  * @throws ConfigurationMismatchException
  * @throws \Exception
  */
 public function getEntityContext($entityType, $entityData = [])
 {
     $entityContext = [];
     $metadata = $this->metadataPool->getMetadata($entityType);
     foreach ($metadata->getEntityContext() as $contextProviderClass) {
         $contextProvider = $this->objectManager->get($contextProviderClass);
         if (!$contextProvider instanceof ScopeProviderInterface) {
             throw new ConfigurationMismatchException(new Phrase('Wrong configuration for type %1', [$entityType]));
         }
         $entityContext[] = $contextProvider->getContext($entityType, $entityData);
     }
     return $entityContext;
 }
Ejemplo n.º 14
0
 /**
  * Retrieve Required children ids
  * Return grouped array, ex array(
  *   group => array(ids)
  * )
  *
  * @param int $parentId
  * @param int $typeId
  * @return array
  */
 public function getChildrenIds($parentId, $typeId)
 {
     $connection = $this->getConnection();
     $childrenIds = [];
     $bind = [':product_id' => (int) $parentId, ':link_type_id' => (int) $typeId];
     $select = $connection->select()->from(['l' => $this->getMainTable()], ['linked_product_id'])->join(['cpe' => $this->getTable('catalog_product_entity')], sprintf('cpe.%s = l.product_id', $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()))->where('cpe.entity_id = :product_id')->where('link_type_id = :link_type_id');
     $select->join(['e' => $this->getTable('catalog_product_entity')], 'e.entity_id = l.linked_product_id AND e.required_options = 0', []);
     $childrenIds[$typeId] = [];
     $result = $connection->fetchAll($select, $bind);
     foreach ($result as $row) {
         $childrenIds[$typeId][$row['linked_product_id']] = $row['linked_product_id'];
     }
     return $childrenIds;
 }
 /**
  * {@inheritdoc}
  */
 public function build($productId)
 {
     $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
     $productTable = $this->resource->getTableName('catalog_product_entity');
     $priceSelect = $this->resource->getConnection()->select()->from(['parent' => $productTable], '')->joinInner(['link' => $this->resource->getTableName('catalog_product_relation')], "link.parent_id = parent.{$linkField}", [])->joinInner(['child' => $productTable], "child.entity_id = link.child_id", ['entity_id'])->joinInner(['t' => $this->resource->getTableName('catalog_product_entity_tier_price')], "t.{$linkField} = child.{$linkField}", [])->where('parent.entity_id = ? ', $productId)->where('t.all_groups = 1 OR customer_group_id = ?', $this->customerSession->getCustomerGroupId())->where('t.qty = ?', 1)->order('t.value ' . Select::SQL_ASC)->limit(1);
     $priceSelectDefault = clone $priceSelect;
     $priceSelectDefault->where('t.website_id = ?', self::DEFAULT_WEBSITE_ID);
     $select[] = $priceSelectDefault;
     if (!$this->catalogHelper->isPriceGlobal()) {
         $priceSelect->where('t.website_id = ?', $this->storeManager->getStore()->getWebsiteId());
         $select[] = $priceSelect;
     }
     return $select;
 }
Ejemplo n.º 16
0
 /**
  * Method for product filter
  *
  * @param \Magento\Catalog\Model\Product|array|int|null $product
  * @return $this
  */
 public function addProductToFilter($product)
 {
     if (empty($product)) {
         $this->addFieldToFilter('product_id', '');
     } else {
         $this->join(['cpe' => $this->getTable('catalog_product_entity')], sprintf('cpe.%s = main_table.product_id', $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()));
         if (is_array($product)) {
             $this->addFieldToFilter('cpe.entity_id', ['in' => $product]);
         } else {
             $this->addFieldToFilter('cpe.entity_id', $product);
         }
     }
     return $this;
 }
Ejemplo n.º 17
0
 /**
  * @param string $entityType
  * @param int $identifier
  * @return int
  * @throws \Exception
  */
 public function delete($entityType, $identifier)
 {
     $metadata = $this->metadataPool->getMetadata($entityType);
     $sequenceInfo = $this->sequenceRegistry->retrieve($entityType);
     if (!isset($sequenceInfo['sequenceTable'])) {
         throw new \Exception('TODO: use correct Exception class' . PHP_EOL . ' Sequence table doesnt exists');
     }
     try {
         $connection = $this->appResource->getConnectionByName($metadata->getEntityConnectionName());
         return $connection->delete($this->appResource->getTableName($sequenceInfo['sequenceTable']), ['sequence_value = ?' => $identifier]);
     } catch (\Exception $e) {
         $this->logger->critical($e->getMessage(), $e->getTrace());
         throw new \Exception('TODO: use correct Exception class' . PHP_EOL . $e->getMessage());
     }
 }
 /**
  * {@inheritdoc}
  */
 public function build($productId)
 {
     $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
     $priceAttribute = $this->eavConfig->getAttribute(Product::ENTITY, 'price');
     $productTable = $this->resource->getTableName('catalog_product_entity');
     $priceSelect = $this->resource->getConnection()->select()->from(['parent' => $productTable], '')->joinInner(['link' => $this->resource->getTableName('catalog_product_relation')], "link.parent_id = parent.{$linkField}", [])->joinInner(['child' => $productTable], "child.entity_id = link.child_id", ['entity_id'])->joinInner(['t' => $priceAttribute->getBackendTable()], "t.{$linkField} = child.{$linkField}", [])->where('parent.entity_id = ? ', $productId)->where('t.attribute_id = ?', $priceAttribute->getAttributeId())->where('t.value IS NOT NULL')->order('t.value ' . Select::SQL_ASC)->limit(1);
     $priceSelectDefault = clone $priceSelect;
     $priceSelectDefault->where('t.store_id = ?', Store::DEFAULT_STORE_ID);
     $select[] = $priceSelectDefault;
     if (!$this->catalogHelper->isPriceGlobal()) {
         $priceSelect->where('t.store_id = ?', $this->storeManager->getStore()->getId());
         $select[] = $priceSelect;
     }
     return $select;
 }
Ejemplo n.º 19
0
 /**
  * Retrieve array of related bundle product ids by selection product id(s)
  *
  * @param int|array $childId
  * @return array
  */
 public function getParentIdsByChild($childId)
 {
     $connection = $this->getConnection();
     $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
     $select = $connection->select()->distinct(true)->from($this->getMainTable(), '')->join(['e' => $this->metadataPool->getMetadata(ProductInterface::class)->getEntityTable()], 'e.' . $metadata->getLinkField() . ' = ' . $this->getMainTable() . '.parent_product_id', ['e.entity_id as parent_product_id'])->where('e.entity_id IN(?)', $childId);
     return $connection->fetchCol($select);
 }
Ejemplo n.º 20
0
 /**
  * @param string $entityType
  * @param array $entityData
  * @param array $arguments
  * @return array
  * @throws \Exception
  * @throws \Magento\Framework\Exception\ConfigurationMismatchException
  * @throws \Magento\Framework\Exception\LocalizedException
  * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  */
 public function execute($entityType, $entityData, $arguments = [])
 {
     $metadata = $this->metadataPool->getMetadata($entityType);
     if (!$metadata->getEavEntityType()) {
         //todo hasCustomAttributes
         return $entityData;
     }
     $context = $this->scopeResolver->getEntityContext($entityType, $entityData);
     $connection = $metadata->getEntityConnection();
     /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
     $attributeTables = [];
     $attributesMap = [];
     $selects = [];
     foreach ($this->getAttributes($entityType) as $attribute) {
         if (!$attribute->isStatic()) {
             $attributeTables[$attribute->getBackend()->getTable()][] = $attribute->getAttributeId();
             $attributesMap[$attribute->getAttributeId()] = $attribute->getAttributeCode();
         }
     }
     foreach ($attributeTables as $attributeTable => $attributeCodes) {
         $select = $connection->select()->from(['t' => $attributeTable], ['value' => 't.value', 'attribute_id' => 't.attribute_id'])->where($metadata->getLinkField() . ' = ?', $entityData[$metadata->getLinkField()]);
         foreach ($context as $scope) {
             //TODO: if (in table exists context field)
             $select->where($metadata->getEntityConnection()->quoteIdentifier($scope->getIdentifier()) . ' IN (?)', $this->getContextVariables($scope))->order('t.' . $scope->getIdentifier() . ' DESC');
         }
         $selects[] = $select;
     }
     $unionSelect = new \Magento\Framework\DB\Sql\UnionExpression($selects, \Magento\Framework\DB\Select::SQL_UNION_ALL);
     foreach ($connection->fetchAll($unionSelect) as $attributeValue) {
         $entityData[$attributesMap[$attributeValue['attribute_id']]] = $attributeValue['value'];
     }
     return $entityData;
 }
Ejemplo n.º 21
0
 /**
  * Retrieve links searchable data
  *
  * @param int $productId
  * @param int $storeId
  * @return array
  */
 public function getSearchableData($productId, $storeId)
 {
     $connection = $this->getConnection();
     $ifNullDefaultTitle = $connection->getIfNullSql('st.title', 'd.title');
     $select = $connection->select()->from(['m' => $this->getMainTable()], null)->join(['d' => $this->getTable('downloadable_sample_title')], 'd.sample_id=m.sample_id AND d.store_id=0', [])->join(['cpe' => $this->getTable('catalog_product_entity')], sprintf('cpe.entity_id = m.product_id', $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()), [])->joinLeft(['st' => $this->getTable('downloadable_sample_title')], 'st.sample_id=m.sample_id AND st.store_id=:store_id', ['title' => $ifNullDefaultTitle])->where('cpe.entity_id=:product_id', $productId);
     $bind = [':store_id' => (int) $storeId, ':product_id' => $productId];
     return $connection->fetchCol($select, $bind);
 }
Ejemplo n.º 22
0
 /**
  * @param object $entity
  * @param string $identifier
  * @param array $arguments
  * @return object
  * @throws \Exception
  */
 public function execute($entity, $identifier, $arguments = [])
 {
     $entityType = $this->typeResolver->resolve($entity);
     $metadata = $this->metadataPool->getMetadata($entityType);
     $hydrator = $this->hydratorPool->getHydrator($entityType);
     $this->eventManager->dispatch('entity_manager_load_before', ['entity_type' => $entityType, 'identifier' => $identifier, 'arguments' => $arguments]);
     $this->eventManager->dispatchEntityEvent($entityType, 'load_before', ['identifier' => $identifier, 'arguments' => $arguments]);
     $entity = $this->readMain->execute($entity, $identifier);
     $entityData = array_merge($hydrator->extract($entity), $arguments);
     if (isset($entityData[$metadata->getLinkField()])) {
         $entity = $this->readAttributes->execute($entity, $arguments);
         $entity = $this->readExtensions->execute($entity, $arguments);
     }
     $this->eventManager->dispatchEntityEvent($entityType, 'load_after', ['entity' => $entity, 'arguments' => $arguments]);
     $this->eventManager->dispatch('entity_manager_load_after', ['entity_type' => $entityType, 'entity' => $entity, 'arguments' => $arguments]);
     return $entity;
 }
Ejemplo n.º 23
0
 /**
  * Get store ids to which specified item is assigned
  *
  * @param int $id
  * @return array
  */
 public function lookupStoreIds($id)
 {
     $connection = $this->getConnection();
     $entityMetadata = $this->metadataPool->getMetadata(BlockInterface::class);
     $linkField = $entityMetadata->getLinkField();
     $select = $connection->select()->from(['cbs' => $this->getTable('cms_block_store')], 'store_id')->join(['cb' => $this->getMainTable()], 'cbs.' . $linkField . ' = cb.' . $linkField, [])->where('cb.' . $entityMetadata->getIdentifierField() . ' = :block_id');
     return $connection->fetchCol($select, ['block_id' => (int) $id]);
 }
Ejemplo n.º 24
0
 /**
  * @param string $entityType
  * @param object $entity
  * @return object
  * @throws CouldNotDeleteException
  * @throws NoSuchEntityException
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entityType, $entity)
 {
     $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku());
     $product = $this->productRepository->get($entity->getSku());
     $linkTypesToId = $this->linkTypeProvider->getLinkTypes();
     $prodyctHydrator = $this->metadataPool->getHydrator(ProductInterface::class);
     $productData = $prodyctHydrator->extract($product);
     $linkId = $this->linkResource->getProductLinkId($productData[$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()], $linkedProduct->getId(), $linkTypesToId[$entity->getLinkType()]);
     if (!$linkId) {
         throw new NoSuchEntityException(__('Product with SKU %1 is not linked to product with SKU %2', $entity->getLinkedProductSku(), $entity->getSku()));
     }
     try {
         $this->linkResource->deleteProductLink($linkId);
     } catch (\Exception $exception) {
         throw new CouldNotDeleteException(__('Invalid data provided for linked products'));
     }
 }
 /**
  * @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $productAttributeRepository
  * @param \Magento\Catalog\Model\ResourceModel\Attribute $attributeResource
  * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
  * @param \Magento\Framework\App\State $appState
  * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
  */
 public function __construct(\Magento\Catalog\Api\ProductAttributeRepositoryInterface $productAttributeRepository, \Magento\Catalog\Model\ResourceModel\Attribute $attributeResource, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\Framework\App\State $appState, \Magento\Framework\EntityManager\MetadataPool $metadataPool)
 {
     $this->productAttributeRepository = $productAttributeRepository;
     $this->searchCriteriaBuilder = $searchCriteriaBuilder;
     $this->attributeResource = $attributeResource;
     $this->appState = $appState;
     $this->metadata = $metadataPool->getMetadata(ProductInterface::class);
     parent::__construct();
 }
Ejemplo n.º 26
0
 /**
  * @param string $entityType
  * @param array $entityData
  * @param array $arguments
  * @return array
  * @throws \Exception
  */
 public function execute($entityType, $entityData, $arguments = [])
 {
     $linkField = $this->metadataPool->getMetadata($entityType)->getLinkField();
     if (isset($entityData['website_ids'])) {
         $websiteIds = $entityData['website_ids'];
         if (!is_array($websiteIds)) {
             $websiteIds = explode(',', (string) $websiteIds);
         }
         $this->ruleResource->bindRuleToEntity($entityData[$linkField], $websiteIds, 'website');
     }
     if (isset($entityData['customer_group_ids'])) {
         $customerGroupIds = $entityData['customer_group_ids'];
         if (!is_array($customerGroupIds)) {
             $customerGroupIds = explode(',', (string) $customerGroupIds);
         }
         $this->ruleResource->bindRuleToEntity($entityData[$linkField], $customerGroupIds, 'customer_group');
     }
     return $entityData;
 }
Ejemplo n.º 27
0
 /**
  * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
  * @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
  * @param \Magento\Catalog\Model\ResourceModel\Product\Gallery $resourceModel
  * @param \Magento\Framework\Json\Helper\Data $jsonHelper
  * @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
  * @param \Magento\Framework\Filesystem $filesystem
  * @param \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb
  */
 public function __construct(\Magento\Framework\EntityManager\MetadataPool $metadataPool, \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository, \Magento\Catalog\Model\ResourceModel\Product\Gallery $resourceModel, \Magento\Framework\Json\Helper\Data $jsonHelper, \Magento\Catalog\Model\Product\Media\Config $mediaConfig, \Magento\Framework\Filesystem $filesystem, \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb)
 {
     $this->metadata = $metadataPool->getMetadata('Magento\\Catalog\\Api\\Data\\ProductInterface');
     $this->attributeRepository = $attributeRepository;
     $this->resourceModel = $resourceModel;
     $this->jsonHelper = $jsonHelper;
     $this->mediaConfig = $mediaConfig;
     $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
     $this->fileStorageDb = $fileStorageDb;
 }
Ejemplo n.º 28
0
 /**
  * Check is current entity has changes, by comparing current object state with stored snapshot
  *
  * @param string $entityType
  * @param object $entity
  * @return bool
  */
 public function isModified($entityType, $entity)
 {
     $metadata = $this->metadataPool->getMetadata($entityType);
     $hydrator = $this->metadataPool->getHydrator($entityType);
     $entityData = $hydrator->extract($entity);
     if (empty($entityData[$metadata->getIdentifierField()])) {
         return true;
     }
     $identifier = $entityData[$metadata->getIdentifierField()];
     if (!isset($this->snapshotData[$entityType][$identifier])) {
         return true;
     }
     foreach ($this->snapshotData[$entityType][$identifier] as $field => $value) {
         if (isset($entityData[$field]) && $entityData[$field] != $value) {
             return true;
         }
     }
     return false;
 }
 /**
  * {@inheritdoc}
  */
 public function build($productId)
 {
     $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
     $connection = $this->resource->getConnection();
     $specialPriceAttribute = $this->eavConfig->getAttribute(Product::ENTITY, 'special_price');
     $specialPriceFromDate = $this->eavConfig->getAttribute(Product::ENTITY, 'special_from_date');
     $specialPriceToDate = $this->eavConfig->getAttribute(Product::ENTITY, 'special_to_date');
     $timestamp = $this->localeDate->scopeTimeStamp($this->storeManager->getStore());
     $currentDate = $this->dateTime->formatDate($timestamp, false);
     $productTable = $this->resource->getTableName('catalog_product_entity');
     $specialPrice = $this->resource->getConnection()->select()->from(['parent' => $productTable], '')->joinInner(['link' => $this->resource->getTableName('catalog_product_relation')], "link.parent_id = parent.{$linkField}", [])->joinInner(['child' => $productTable], "child.entity_id = link.child_id", ['entity_id'])->joinInner(['t' => $specialPriceAttribute->getBackendTable()], "t.{$linkField} = child.{$linkField}", [])->joinLeft(['special_from' => $specialPriceFromDate->getBackendTable()], $connection->quoteInto("t.{$linkField} = special_from.{$linkField} AND special_from.attribute_id = ?", $specialPriceFromDate->getAttributeId()), '')->joinLeft(['special_to' => $specialPriceToDate->getBackendTable()], $connection->quoteInto("t.{$linkField} = special_to.{$linkField} AND special_to.attribute_id = ?", $specialPriceToDate->getAttributeId()), '')->where('parent.entity_id = ? ', $productId)->where('t.attribute_id = ?', $specialPriceAttribute->getAttributeId())->where('t.value IS NOT NULL')->where('special_from.value IS NULL OR ' . $connection->getDatePartSql('special_from.value') . ' <= ?', $currentDate)->where('special_to.value IS NULL OR ' . $connection->getDatePartSql('special_to.value') . ' >= ?', $currentDate)->order('t.value ' . Select::SQL_ASC)->limit(1);
     $specialPriceDefault = clone $specialPrice;
     $specialPriceDefault->where('t.store_id = ?', Store::DEFAULT_STORE_ID);
     $select[] = $specialPriceDefault;
     if (!$this->catalogHelper->isPriceGlobal()) {
         $specialPrice->where('t.store_id = ?', $this->storeManager->getStore()->getId());
         $select[] = $specialPrice;
     }
     return $select;
 }
Ejemplo n.º 30
0
 /**
  * @param string $entityType
  * @param array $entityData
  * @param array $arguments
  * @return array
  * @throws \Exception
  * @throws \Magento\Framework\Exception\ConfigurationMismatchException
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entityType, $entityData, $arguments = [])
 {
     $metadata = $this->metadataPool->getMetadata($entityType);
     if ($metadata->getEavEntityType()) {
         $processed = [];
         /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */
         foreach ($this->getAttributes($entityType) as $attribute) {
             if ($attribute->isStatic()) {
                 continue;
             }
             if (isset($entityData[$attribute->getAttributeCode()]) && !is_array($entityData[$attribute->getAttributeCode()]) && !$attribute->isValueEmpty($entityData[$attribute->getAttributeCode()])) {
                 $entityLinkField = $metadata->getLinkField();
                 $this->attributePersistor->registerInsert($entityType, $entityData[$entityLinkField], $attribute->getAttributeCode(), $entityData[$attribute->getAttributeCode()]);
                 $processed[$attribute->getAttributeCode()] = $entityData[$attribute->getAttributeCode()];
             }
         }
         $context = $this->scopeResolver->getEntityContext($entityType, $entityData);
         $this->attributePersistor->flush($entityType, $context);
     }
     return $entityData;
 }