Skip to content

Commit 4b208c5

Browse files
Remove Redundant EAV attribute values
1 parent 3ffd3ed commit 4b208c5

File tree

3 files changed

+58
-29
lines changed

3 files changed

+58
-29
lines changed

Plugin/InsertNewProducts.php

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,25 @@
44

55
use Akeneo\Connector\Job\Product;
66
use Akeneo\Connector\Helper\Import\Entities;
7-
use Magento\Store\Model\ScopeInterface as scope;
7+
use Magento\Store\Model\ScopeInterface as Scope;
88
use Magento\Framework\App\Config\ScopeConfigInterface;
99

1010
class InsertNewProducts
1111
{
12-
protected $config;
13-
protected $entitiesHelper;
14-
15-
/**
16-
* __construct function
17-
* @param ScopeConfigInterface $config
18-
* @param Entities $entitiesHelper
19-
*/
2012
public function __construct(
21-
ScopeConfigInterface $config,
22-
Entities $entitiesHelper
13+
protected ScopeConfigInterface $config,
14+
protected Entities $entitiesHelper
2315
) {
24-
$this->config = $config;
25-
$this->entitiesHelper = $entitiesHelper;
2616
}
2717

28-
/**
29-
* afterInsertData function
30-
* @param product $subject
31-
* @param bool $result
32-
* @return bool $result
33-
*/
34-
public function afterInsertData(product $subject, $result)
18+
public function afterInsertData(Product $subject)
3519
{
36-
$extensionEnabled = $this->config->getValue('akeneo_connector/justbetter/insertnewproducts', scope::SCOPE_WEBSITE);
37-
if (!$extensionEnabled) {
38-
return $result;
20+
$extensionEnabled = $this->config->getValue('akeneo_connector/justbetter/insertnewproducts', Scope::SCOPE_WEBSITE);
21+
if ($extensionEnabled) {
22+
$connection = $this->entitiesHelper->getConnection();
23+
$tmpTableName = $this->entitiesHelper->getTableName($subject->getCode());
24+
25+
$connection->delete($tmpTableName, ['_is_new = ?' => 1]);
3926
}
40-
41-
$connection = $this->entitiesHelper->getConnection();
42-
$tmpTableName = $this->entitiesHelper->getTableName($subject->getCode());
43-
44-
$connection->delete($tmpTableName, ['_is_new = ?' => 1]);
45-
return $result;
4627
}
4728
}

Plugin/RemoveRedundantEav.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace JustBetter\AkeneoBundle\Plugin;
4+
5+
use Akeneo\Connector\Executor\JobExecutor;
6+
use Akeneo\Connector\Helper\Import\Entities;
7+
use Akeneo\Connector\Helper\Output;
8+
use Magento\Framework\App\ResourceConnection;
9+
10+
class RemoveRedundantEav
11+
{
12+
public const CATALOG_PRODUCT_ENTITY_DATA_TYPES = ['int', 'text', 'decimal', 'gallery', 'varchar', 'datetime'];
13+
14+
public function __construct(
15+
protected Entities $entities,
16+
protected ResourceConnection $resourceConnection,
17+
protected JobExecutor $jobExecutor,
18+
protected Output $outputHelper
19+
) {
20+
}
21+
22+
public function beforeRefreshIndex(): void
23+
{
24+
self::removeRedundantEAV();
25+
}
26+
27+
public function removeRedundantEAV(): void
28+
{
29+
$this->jobExecutor->displayInfo((string)$this->outputHelper->getPrefix() . __('Remove Redundant EAV attribute values'));
30+
$connection = $this->resourceConnection->getConnection();
31+
32+
foreach (self::CATALOG_PRODUCT_ENTITY_DATA_TYPES as $dataType) {
33+
$query = "
34+
DELETE cpe{$dataType}
35+
FROM catalog_product_entity_{$dataType} as cpe{$dataType}
36+
LEFT JOIN catalog_product_entity as cpe on cpe.entity_id = cpe{$dataType}.entity_id
37+
LEFT JOIN eav_attribute as ea on ea.attribute_id = cpe{$dataType}.attribute_id
38+
WHERE cpe{$dataType}.store_id = 0 AND cpe.attribute_set_id NOT IN (
39+
SELECT attribute_set_id
40+
FROM eav_entity_attribute
41+
WHERE attribute_set_id = cpe.attribute_set_id AND attribute_id = cpe{$dataType}.attribute_id
42+
)
43+
";
44+
$connection->query($query);
45+
}
46+
}
47+
}

etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<plugin name="JustBetter_SetStockStatus" type="JustBetter\AkeneoBundle\Plugin\SetStockStatus" sortOrder="2" />
1010
<plugin name="JustBetter_ImportFinished" type="JustBetter\AkeneoBundle\Plugin\ImportFinished" sortOrder="10" />
1111
<plugin name="JustBetter_CheckWebsiteAssociation" type="JustBetter\AkeneoBundle\Plugin\CheckWebsiteAssociation" />
12+
<plugin name="JustBetter_RemoveRedundantEav" type="JustBetter\AkeneoBundle\Plugin\RemoveRedundantEav" />
1213
</type>
1314

1415
<type name="Akeneo\Connector\Job\Category">

0 commit comments

Comments
 (0)