Skip to content

Commit

Permalink
Add not visible individually per family (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: Vincent Boon <[email protected]>
  • Loading branch information
VincentBean and VincentBean authored Oct 8, 2021
1 parent 4f7a33e commit bfe6175
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 4 deletions.
37 changes: 37 additions & 0 deletions Console/Command/SetNotVisible.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace JustBetter\AkeneoBundle\Console\Command;

use JustBetter\AkeneoBundle\Job\SetNotVisible as SetNotVisibleJob;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SetNotVisible extends Command
{
protected SetNotVisibleJob $job;

public function __construct(SetNotVisibleJob $job, string $name = null)
{
$this->job = $job;

parent::__construct($name);
}

protected function configure(): void
{
$this->setName('set:notvisible');
$this->setDescription('Set recently updated products to not visible');

parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output): void
{
$output->writeln('Starting');

$this->job->execute($output);

$output->writeln('Finished!');
}
}
27 changes: 27 additions & 0 deletions Data/FamilyOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace JustBetter\AkeneoBundle\Data;

use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory;
use Magento\Framework\Data\OptionSourceInterface;

class FamilyOptions implements OptionSourceInterface
{
protected CollectionFactory $collectionFactory;

public function __construct(
CollectionFactory $collectionFactory
) {
$this->collectionFactory = $collectionFactory;
}

public function toOptionArray(): array
{
return array_map(function ($set) {
return [
'value' => $set->getData('attribute_set_id'),
'label' => $set->getData('attribute_set_name')
];
}, $this->collectionFactory->create()->getItems());
}
}
64 changes: 64 additions & 0 deletions Job/SetNotVisible.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace JustBetter\AkeneoBundle\Job;

use Akeneo\Connector\Helper\Authenticator;
use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursor;
use Akeneo\Pim\ApiClient\Search\SearchBuilder;
use Magento\Catalog\Model\ResourceModel\Product\Action;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Symfony\Component\Console\Output\OutputInterface;

class SetNotVisible
{
protected const CONFIG_PREFIX = 'akeneo_connector/justbetter/';
protected const NOT_VISIBLE_CONFIG_KEY = 'notvisiblefamilies';

protected CollectionFactory $collectionFactory;
protected ScopeConfigInterface $config;
protected Action $action;

public function __construct(CollectionFactory $collectionFactory, ScopeConfigInterface $config, Action $action)
{
$this->collectionFactory = $collectionFactory;
$this->config = $config;
$this->action = $action;
}

public function execute(OutputInterface $output = null): void
{
$products = $this->collectionFactory->create()
->addFieldToFilter('attribute_set_id', ['in' => $this->getNotVisibleFamilies()])
->addFieldToFilter('visibility', ['neq' => '1'])
->getItems();

if (count($products) == 0) {
if ($output) {
$output->writeln('No updates necessary');
}
return;
}

if ($output) {
$output->writeln('Found ' . count($products) . ' products that should have visibility set to not visible individually');
}

$entityIds = array_map(function ($p) {
return $p->getEntityId();
}, $products);

$this->action->updateAttributes($entityIds, ['visibility' => '1'], 0);
}

protected function getNotVisibleFamilies(): array
{
return explode(
',',
$this->config->getValue(static::CONFIG_PREFIX . static::NOT_VISIBLE_CONFIG_KEY)
);
}
}
22 changes: 22 additions & 0 deletions Observer/SetNotVisible.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace JustBetter\AkeneoBundle\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use JustBetter\AkeneoBundle\Job\SetNotVisible as SetNotVisibleJob;

class SetNotVisible implements ObserverInterface
{
protected SetNotVisibleJob $job;

public function __construct(SetNotVisibleJob $job)
{
$this->job = $job;
}

public function execute(Observer $observer): void
{
$this->job->execute();
}
}
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ The following features are included in the JustBetter Akeneo Bundle extension:
| MailNotificationCommand | Adds the possibility to receive e-mail notifications about akeneo imports. |
| AkeneoManager | Manual adjustment of akeneo codes vs magento entity id’s connector mapping. |
| SetTierPrices | Maps specific Akeneo attribute code with a Magento Customer group. This ensures that the tier prices from Akeneo are imported into Magento customer tier prices |
| SetTaxClass | Map When you have multiple tax classes in Akeneo and want to use them in Magento. Akeneo tax class codes to Magento tax class - See confguration
| <a href="#import-finished-events">ImportFinished</a> | Fires an event for every job that is fully finished.
| <a href="#metric-units">Metric Units</a> | Sets Akeneo's metric unit in the eav_attribute
| SetTaxClass | Map When you have multiple tax classes in Akeneo and want to use them in Magento. Akeneo tax class codes to Magento tax class - See confguration |
| <a href="#import-finished-events">ImportFinished</a> | Fires an event for every job that is fully finished. |
| <a href="#metric-units">Metric Units</a> | Sets Akeneo's metric unit in the eav_attribute |
| <a href="#not-visible-individually">Family - Not Visible Individually</a> | Sets products in selected families to Not Visible Individually |

## Installation
- `composer require justbetter/magento2-akeneo-bundle`
Expand Down Expand Up @@ -50,6 +51,12 @@ You can run this from the command line using `bin/magento metric:import`

It is also automatically run after the attribute import

## Family - Not Visible Individually
If you need all products of a certain families to be set to not visible individually you can select those families.
After each import this will run and set products to not visible.

You can also run this from the command line using `bin/magento set:notvisible`

## Ideas, bugs or suggestions?
Would be awesome if you can submit an [issue](https://github.com/justbetter/magento2-akeneo-bundle/issues) or for kudos create a [pull request](https://github.com/justbetter/magento2-akeneo-bundle/pulls).

Expand Down
8 changes: 7 additions & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@
<label>Channel for metric conversions</label>
<comment>What channel to use for metric conversions</comment>
</field>
<group id="slack" translate="label" type="text" sortOrder="9" showInDefault="1" showInWebsite="0"
<field id="notvisiblefamilies" translate="label" type="multiselect" sortOrder="9" showInDefault="1"
showInWebsite="0" showInStore="0">
<label>Set families to not visible individually after importing</label>
<source_model>JustBetter\AkeneoBundle\Data\FamilyOptions</source_model>
<comment>When a family is selected here all of the products will get the status not visible individually</comment>
</field>
<group id="slack" translate="label" type="text" sortOrder="11" showInDefault="1" showInWebsite="0"
showInStore="0">
<label>Slack Akeneo import notifications</label>
<field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0"
Expand Down
3 changes: 3 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<argument name="commands" xsi:type="array">
<item name="UpdateMetricUnits" xsi:type="object">JustBetter\AkeneoBundle\Console\Command\ImportMetricUnits</item>
</argument>
<argument name="commands" xsi:type="array">
<item name="SetNotVisible" xsi:type="object">JustBetter\AkeneoBundle\Console\Command\SetNotVisible</item>
</argument>
</arguments>
</type>
</config>
3 changes: 3 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
<event name="akeneo_connector_import_finish_attribute">
<observer name="retrieve_metric_values" instance="JustBetter\AkeneoBundle\Observer\ImportMetricUnits"/>
</event>
<event name="akeneo_connector_import_finish_product">
<observer name="set_not_visible" instance="JustBetter\AkeneoBundle\Observer\SetNotVisible"/>
</event>
</config>

0 comments on commit bfe6175

Please sign in to comment.