Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Observer/RemoveRedundantEav.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace JustBetter\AkeneoBundle\Observer;

use Akeneo\Connector\Executor\JobExecutor;
use Akeneo\Connector\Helper\Import\Entities;
use Akeneo\Connector\Helper\Output;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

class RemoveRedundantEav implements ObserverInterface
{
public const CATALOG_PRODUCT_ENTITY_DATA_TYPES = ['int', 'text', 'decimal', 'gallery', 'varchar', 'datetime'];

public function __construct(
protected Entities $entities,
protected ResourceConnection $resourceConnection,
protected Output $outputHelper,
protected JobExecutor $jobExecutor,
protected ScopeConfigInterface $scopeConfig
) {
}

public function execute(Observer $observer): void
{
if (!$this->scopeConfig->isSetFlag('akeneo_connector/justbetter/remove_redundant_eav')) {
return;
}

$this->jobExecutor->displayInfo((string)$this->outputHelper->getPrefix() . __('Remove Redundant EAV attribute values'));
$connection = $this->resourceConnection->getConnection();

foreach (self::CATALOG_PRODUCT_ENTITY_DATA_TYPES as $dataType) {
$query = "
DELETE cpe{$dataType}
FROM catalog_product_entity_{$dataType} as cpe{$dataType}
LEFT JOIN catalog_product_entity as cpe on cpe.entity_id = cpe{$dataType}.entity_id
LEFT JOIN eav_attribute as ea on ea.attribute_id = cpe{$dataType}.attribute_id
WHERE cpe{$dataType}.store_id = 0 AND cpe.attribute_set_id NOT IN (
SELECT attribute_set_id
FROM eav_entity_attribute
WHERE attribute_set_id = cpe.attribute_set_id AND attribute_id = cpe{$dataType}.attribute_id
)
";
$connection->query($query);
}
}
}
39 changes: 10 additions & 29 deletions Plugin/InsertNewProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,25 @@

use Akeneo\Connector\Job\Product;
use Akeneo\Connector\Helper\Import\Entities;
use Magento\Store\Model\ScopeInterface as scope;
use Magento\Store\Model\ScopeInterface as Scope;
use Magento\Framework\App\Config\ScopeConfigInterface;

class InsertNewProducts
{
protected $config;
protected $entitiesHelper;

/**
* __construct function
* @param ScopeConfigInterface $config
* @param Entities $entitiesHelper
*/
public function __construct(
ScopeConfigInterface $config,
Entities $entitiesHelper
protected ScopeConfigInterface $config,
protected Entities $entitiesHelper
) {
$this->config = $config;
$this->entitiesHelper = $entitiesHelper;
}

/**
* afterInsertData function
* @param product $subject
* @param bool $result
* @return bool $result
*/
public function afterInsertData(product $subject, $result)
public function afterInsertData(Product $subject)
{
$extensionEnabled = $this->config->getValue('akeneo_connector/justbetter/insertnewproducts', scope::SCOPE_WEBSITE);
if (!$extensionEnabled) {
return $result;
$extensionEnabled = $this->config->getValue('akeneo_connector/justbetter/insertnewproducts', Scope::SCOPE_WEBSITE);
if ($extensionEnabled) {
$connection = $this->entitiesHelper->getConnection();
$tmpTableName = $this->entitiesHelper->getTableName($subject->getCode());

$connection->delete($tmpTableName, ['_is_new = ?' => 1]);
}

$connection = $this->entitiesHelper->getConnection();
$tmpTableName = $this->entitiesHelper->getTableName($subject->getCode());

$connection->delete($tmpTableName, ['_is_new = ?' => 1]);
return $result;
}
}
13 changes: 13 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@
<field id="akeneo_connector/justbetter/defaultstorevalues">1</field>
</depends>
</field>
<field id="remove_redundant_eav" translate="label" type="select" sortOrder="160" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Remove Redundant EAV</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>
<![CDATA[
Enable or disable the removal of redundant EAV attribute values. (Default: no)
<br/><br/>
When a product's family (attribute set) changes in Akeneo, attributes no longer in the new set may still have values stored in the database.
<br/><br/>
Enabling this option removes these redundant values during import, keeping the database clean and consistent.
]]>
</comment>
</field>
<group id="slack" translate="label" type="text" sortOrder="160" showInDefault="1" showInWebsite="0"
showInStore="0">
<label>Slack Akeneo import notifications</label>
Expand Down
1 change: 1 addition & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
</event>
<event name="akeneo_connector_import_finish_product">
<observer name="set_not_visible" instance="JustBetter\AkeneoBundle\Observer\SetNotVisible"/>
<observer name="remove_redundant_eav" instance="JustBetter\AkeneoBundle\Observer\RemoveRedundantEav" />
</event>
</config>