-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add not visible individually per family (#8)
Co-authored-by: Vincent Boon <[email protected]>
- Loading branch information
1 parent
4f7a33e
commit bfe6175
Showing
8 changed files
with
173 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters