Skip to content

Commit fe90a85

Browse files
mstrzyzewskiMaksymilian Strzyżewski
andauthored
Add cron for deleting old offers mapping (#93)
Co-authored-by: Maksymilian Strzyżewski <m.strzyzewski@macopedia.pl>
1 parent 1921a30 commit fe90a85

File tree

8 files changed

+205
-3
lines changed

8 files changed

+205
-3
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Macopedia\Allegro\Console\Command;
6+
7+
use Macopedia\Allegro\Model\Api\ClientException;
8+
use Macopedia\Allegro\Model\OffersMapping;
9+
use Magento\Framework\App\Area;
10+
use Magento\Framework\App\State;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Symfony\Component\Console\Command\Command;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
16+
/**
17+
* CleanOffersMapping command class
18+
*/
19+
class CleanOffersMapping extends Command
20+
{
21+
/** @var State */
22+
protected $state;
23+
24+
/** @var OffersMapping */
25+
protected $offersMapping;
26+
27+
/**
28+
* CleanOffersMapping constructor.
29+
* @param State $state
30+
* @param OffersMapping $offersMapping
31+
* @param string|null $name
32+
*/
33+
public function __construct(
34+
State $state,
35+
OffersMapping $offersMapping,
36+
string $name = null
37+
) {
38+
parent::__construct($name);
39+
$this->state = $state;
40+
$this->offersMapping = $offersMapping;
41+
}
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
protected function configure()
47+
{
48+
$this->setName('macopedia:allegro:clean-offers-mapping');
49+
$this->setDescription("Clean old offers mapping");
50+
}
51+
52+
/**
53+
* @param InputInterface $input
54+
* @param OutputInterface $output
55+
* @return int|void
56+
* @throws LocalizedException
57+
*/
58+
protected function execute(InputInterface $input, OutputInterface $output)
59+
{
60+
try {
61+
$this->state->getAreaCode();
62+
} catch (LocalizedException $exception) {
63+
$this->state->setAreaCode(Area::AREA_GLOBAL);
64+
}
65+
66+
try {
67+
$this->offersMapping->clean();
68+
} catch (ClientException $e) {
69+
$output->writeln('Error occurred while trying to clean old offers mapping');
70+
$output->writeln($e->getMessage());
71+
}
72+
}
73+
}

Cron/CleanOffersMapping.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Macopedia\Allegro\Cron;
6+
7+
use Macopedia\Allegro\Logger\Logger;
8+
use Macopedia\Allegro\Model\Api\ClientException;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
10+
use Macopedia\Allegro\Model\OffersMapping;
11+
12+
class CleanOffersMapping
13+
{
14+
const OFFERS_MAPPING_CRON_CONFIG_KEY = 'allegro/order/offers_mapping_cron_enabled';
15+
16+
/** @var Logger */
17+
protected $logger;
18+
19+
/** @var ScopeConfigInterface */
20+
protected $scopeConfig;
21+
22+
/** @var OffersMapping */
23+
protected $offersMapping;
24+
25+
/**
26+
* @param Logger $logger
27+
* @param ScopeConfigInterface $scopeConfig
28+
* @param OffersMapping $offersMapping
29+
*/
30+
public function __construct(
31+
Logger $logger,
32+
ScopeConfigInterface $scopeConfig,
33+
OffersMapping $offersMapping
34+
) {
35+
$this->logger = $logger;
36+
$this->scopeConfig = $scopeConfig;
37+
$this->offersMapping = $offersMapping;
38+
}
39+
40+
public function execute()
41+
{
42+
if ($this->scopeConfig->getValue(self::OFFERS_MAPPING_CRON_CONFIG_KEY)) {
43+
$this->logger->addInfo("Cronjob clean offers mapping is executed.");
44+
try {
45+
$this->offersMapping->clean();
46+
} catch (ClientException $e) {
47+
$this->logger->error('Error while trying to clean old offers mapping: ' . $e->getMessage());
48+
}
49+
}
50+
}
51+
}

Model/OffersMapping.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Macopedia\Allegro\Model;
6+
7+
use Macopedia\Allegro\Logger\Logger;
8+
use Macopedia\Allegro\Model\ResourceModel\Sale\Offers;
9+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
10+
11+
class OffersMapping
12+
{
13+
/** @var Offers */
14+
protected $offers;
15+
16+
/** @var Logger */
17+
protected $logger;
18+
19+
/** @var Configuration */
20+
protected $configuration;
21+
22+
/** @var CollectionFactory */
23+
protected $productCollection;
24+
25+
/**
26+
* OffersMapping constructor.
27+
* @param Offers $offers
28+
* @param Logger $logger
29+
* @param Configuration $configuration
30+
* @param CollectionFactory $productCollection
31+
*/
32+
public function __construct(
33+
Offers $offers,
34+
Logger $logger,
35+
Configuration $configuration,
36+
CollectionFactory $productCollection
37+
) {
38+
$this->offers = $offers;
39+
$this->logger = $logger;
40+
$this->configuration = $configuration;
41+
$this->productCollection = $productCollection;
42+
}
43+
44+
/**
45+
* @throws Api\ClientException
46+
*/
47+
public function clean()
48+
{
49+
$collection = $this->productCollection->create();
50+
$collection->addAttributeToSelect('*')
51+
->addStoreFilter($this->configuration->getStoreId())
52+
->addAttributeToFilter('allegro_offer_id', ['neq' => 'NULL']);
53+
54+
/** @var Magento\Catalog\Model\Product $product */
55+
foreach ($collection->getItems() as $product) {
56+
$allegroOfferId = $product->getAllegroOfferId();
57+
try {
58+
$this->offers->get($allegroOfferId);
59+
} catch (Api\ClientResponseException $e) {
60+
if ($e->getCode() == 404) {
61+
$product->setAllegroOfferId(null);
62+
$product->save();
63+
}
64+
}
65+
}
66+
}
67+
}

Setup/Patch/Data/OrderStatuses.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace Macopedia\Allegro\Setup\Patch\Data;
55

6-
76
use Magento\Framework\Setup\ModuleDataSetupInterface;
87
use Magento\Framework\Setup\Patch\DataPatchInterface;
98

@@ -21,7 +20,8 @@ class OrderStatuses implements DataPatchInterface
2120
* OrderStatuses constructor.
2221
* @param ModuleDataSetupInterface $moduleDataSetup
2322
*/
24-
public function __construct(ModuleDataSetupInterface $moduleDataSetup) {
23+
public function __construct(ModuleDataSetupInterface $moduleDataSetup)
24+
{
2525
$this->moduleDataSetup = $moduleDataSetup;
2626
}
2727

etc/adminhtml/system.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,19 @@
7272
<label>Reservations enabled</label>
7373
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
7474
</field>
75-
<field id="reservations_cron_enabled" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
75+
<field id="reservations_cron_enabled" translate="label comment" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
7676
<depends>
7777
<field id="reservations_enabled">1</field>
7878
</depends>
7979
<label>Cron for cleaning old reservations enabled</label>
8080
<comment>Cron deletes reservations older than 10 days</comment>
8181
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
8282
</field>
83+
<field id="offers_mapping_cron_enabled" translate="label comment" type="select" sortOrder="31" showInDefault="1" showInWebsite="1" showInStore="1">
84+
<label>Cron for cleaning old offers mapping enabled</label>
85+
<comment>Cron removes Allegro offer ID from product when offer no longer exists</comment>
86+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
87+
</field>
8388
<field id="store" translate="label" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
8489
<label>Store to create new orders</label>
8590
<source_model>Magento\Store\Model\System\Store</source_model>

etc/crontab.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313
<job instance="Macopedia\Allegro\Cron\CleanReservations" method="execute" name="macopedia_allegro_clean_reservations">
1414
<schedule>0 12 * * *</schedule>
1515
</job>
16+
<job instance="Macopedia\Allegro\Cron\CleanOffersMapping" method="execute" name="macopedia_allegro_clean_offers_mapping">
17+
<schedule>0 10 * * *</schedule>
18+
</job>
1619
</group>
1720
</config>

etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<item name="OrdersImport" xsi:type="object">Macopedia\Allegro\Console\Command\ImportOrders</item>
8585
<item name="OrderImport" xsi:type="object">Macopedia\Allegro\Console\Command\ImportOrder</item>
8686
<item name="OrdersWithErrorsImport" xsi:type="object">Macopedia\Allegro\Console\Command\ImportOrdersWithErrors</item>
87+
<item name="CleanOffersMapping" xsi:type="object">Macopedia\Allegro\Console\Command\CleanOffersMapping</item>
8788
</argument>
8889
</arguments>
8990
</type>

i18n/pl_PL.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,5 @@
181181
"Cron deletes reservations older than 10 days","Cron usuwa rezerwacje starsze niż 10 dni"
182182
"Allegro underpayment","Allegro niedopłata"
183183
"Allegro overpayment","Allegro nadpłata"
184+
"Cron removes Allegro offer ID from product when offer no longer exists","Cron usuwa z produktu ID oferty na Allegro jeśli oferta już nie istnieje"
185+
"Cron for cleaning old offers mapping enabled","Cron do czyszczenia starych mapowań ofert"

0 commit comments

Comments
 (0)