Skip to content

Commit

Permalink
Revert "Remove website accociation plugin (#34)" (#35)
Browse files Browse the repository at this point in the history
This reverts commit 9e06647.

Co-authored-by: Robin Mulder <[email protected]>
  • Loading branch information
rbnmulder and Robin Mulder authored Jan 10, 2024
1 parent 9e06647 commit 5ba331a
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
135 changes: 135 additions & 0 deletions Plugin/CheckWebsiteAssociation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace JustBetter\AkeneoBundle\Plugin;

use Akeneo\Pim\ApiClient\Search\SearchBuilderFactory;
use Magento\Framework\DB\Select;
use Magento\Framework\DB\Statement\Pdo\Mysql;
use Magento\Framework\Serialize\SerializerInterface;
use Zend_Db_Expr as expression;
use Akeneo\Connector\Job\Product;
use Magento\Store\Model\ScopeInterface as scope;
use Akeneo\Connector\Helper\Store as StoreHelper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Akeneo\Connector\Helper\Import\Product as ProductImportHelper;
use Akeneo\Connector\Helper\Config as ConfigHelper;
use Akeneo\Connector\Helper\Authenticator;

class CheckWebsiteAssociation
{
protected $entitiesHelper;
protected $storeHelper;
protected $config;
protected $serializer;

/**
* construct function
* @param ProductImportHelper $entitiesHelper
*/
public function __construct(
ProductImportHelper $entitiesHelper,
StoreHelper $storeHelper,
ScopeConfigInterface $config,
ConfigHelper $configHelper,
Authenticator $authenticator,
SearchBuilderFactory $searchBuilderFactory,
SerializerInterface $serializer
) {
$this->entitiesHelper = $entitiesHelper;
$this->storeHelper = $storeHelper;
$this->config = $config;
$this->configHelper = $configHelper;
$this->authenticator = $authenticator;
$this->searchBuilderFactory = $searchBuilderFactory;
$this->serializer = $serializer;
}

public function beforeSetWebsites(product $subject)
{
$connection = $this->entitiesHelper->getConnection();
/** @var string $tmpTable */
$tmpTable = $this->entitiesHelper->getTableName($subject->getCode());
$websiteAttribute = $this->configHelper->getWebsiteAttribute();
$websites = $this->storeHelper->getStores('website_code');
$websiteAssociation = $this->config->getValue('akeneo_connector/product/website_attribute');

$requiredAttributes = $this->getRequiredAttributes();

if ($connection->tableColumnExists($tmpTable, $websiteAttribute)) {
/** @var Select $select */
$select = $connection->select()->from(
$tmpTable
);
/** @var Mysql $query */
$query = $connection->query($select);
/** @var array $row */
while (($row = $query->fetch())) {

if(!isset($row[$websiteAssociation])) {
continue;
}

$websites = explode(',', $row[$websiteAssociation]);
$mapping = $this->getMappedWebsiteChannels();

foreach ($websites as $key => $website) {
$channel = $mapping[$website] ?? '';
if (empty($channel)) {
continue;
}

$locales = $this->storeHelper->getChannelStoreLangs($channel);
foreach ($requiredAttributes as $attribute) {
if (isset($attribute['localizable']) && $attribute['localizable'] === true) {
foreach ($locales as $locale) {
if (empty($row[$attribute['akeneo_attribute'] . '-' . $locale . '-' . $channel])) {
unset($websites[$key]);
break(2);
}
}
} else {
if (empty($row[$attribute])) {
unset($websites[$key]);
break(2);
}
}
}
}

$connection->update(
$tmpTable,
[
$websiteAssociation => implode(',', $websites),
],
['identifier = ?' => $row['identifier']]
);
}
}
return [$subject];
}

public function getMappedWebsiteChannels()
{
/** @var mixed[] $mapping */
$mapping = $this->configHelper->getWebsiteMapping();
/** @var string[] $channels */
$channels = array_column($mapping, 'channel', 'website');

return $channels;
}

public function getRequiredAttributes()
{
if (!($requiredAttributes = $this->config->getValue('akeneo_connector/product/required_attribute_mapping'))) {
return [];
}
$requiredAttributes = $this->serializer->unserialize($requiredAttributes);

foreach ($requiredAttributes as $key => &$requiredAttribute) {
$akeneoAttribute = $this->authenticator->getAkeneoApiClient()->getAttributeApi()->get($requiredAttribute['akeneo_attribute']);
$requiredAttribute['localizable'] = $akeneoAttribute['localizable'];
}

return $requiredAttributes;
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ These features can be enabled / disabled via an extra configuration section call
| <a href="#metric-units">Enable retrieving metric units</a> | Sets Akeneo's metric unit in the eav_attribute - See configuration |
| Channel for metric conversions | What channel to use for metric conversions |
| <a href="#not-visible-individually">Set families to not visible individually after importing</a> | Sets products in selected families to `Not Visible Individually` |
| Unset Website when empty Product Attribute Mapping | When enabled this will unset the website from the product when a required attribute has no specific value. For example when the Name attribute in Akeneo is empty for the associated website |
| Akeneo Import e-mail notifications | Setup e-mail notifications of Akeneo imports |
| Slack Akeneo import notifications | Setup Slack notifications of Akeneo imports |
| *Akeneo import log cleaner* | Cleans the Akeneo logs that are older then the configured number of days *Deprecated - the Akeneo Connector has this functionality built in since* [v102.2.0](https://github.com/akeneo/magento2-connector-community/releases/tag/v102.2.0) |
Expand Down
6 changes: 6 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
<field id="akeneo_connector/justbetter/settaxclass">1</field>
</depends>
</field>
<field id="required_attribute_mapping" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Unset Website when empty Product Attribute Mapping</label>
<frontend_model>Akeneo\Connector\Block\Adminhtml\System\Config\Form\Field\Attribute</frontend_model>
<backend_model>Magento\Config\Model\Config\Backend\Serialized\ArraySerialized</backend_model>
<comment>Each of the required product attributes will be checked. If one of the attributes is empty the Website Association is removed. For Example when a productname in Akeneo is empty, there may be errors in Magento.</comment>
</field>
</group>
<resource>JustBetter_AkeneoBundle::akeneobundle_config</resource>
<group id="justbetter" translate="label" type="text" sortOrder="2000" showInDefault="1" showInWebsite="0"
Expand Down
1 change: 1 addition & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<plugin name="JustBetter_EnableManageStock" type="JustBetter\AkeneoBundle\Plugin\EnableManageStock" sortOrder="1" />
<plugin name="JustBetter_SetStockStatus" type="JustBetter\AkeneoBundle\Plugin\SetStockStatus" sortOrder="2" />
<plugin name="JustBetter_ImportFinished" type="JustBetter\AkeneoBundle\Plugin\ImportFinished" sortOrder="10" />
<plugin name="JustBetter_CheckWebsiteAssociation" type="JustBetter\AkeneoBundle\Plugin\CheckWebsiteAssociation" />
</type>

<type name="Akeneo\Connector\Job\Category">
Expand Down

0 comments on commit 5ba331a

Please sign in to comment.