Skip to content

Commit cdefa7b

Browse files
authored
Add option to select product attribute for offer description (#59)
-Add validation for checking if an offer is a draft before activating
1 parent 25c464d commit cdefa7b

File tree

11 files changed

+80
-43
lines changed

11 files changed

+80
-43
lines changed

Api/Data/OfferInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ public function canBePublished(): bool;
199199
*/
200200
public function canBeEnded(): bool;
201201

202+
/**
203+
* @return bool
204+
*/
205+
public function isDraft(): bool;
206+
202207
/**
203208
* @return bool
204209
*/

Model/Config/Source/ProductAttributes.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ public function toOptionArray()
4747
try {
4848
$searchCriteria = $this->searchCriteriaBuilder->create();
4949
$attributes = $this->attributesRepository->getList(Product::ENTITY, $searchCriteria);
50-
} catch (LocalizedException $e) {
51-
$this->messageManager->addErrorMessage('Can\'t get list of product attributes');
52-
$this->messageManager->addExceptionMessage($e);
53-
return [];
5450
} catch (\Exception $e) {
5551
$this->messageManager->addErrorMessage('Can\'t get list of product attributes');
5652
return [];

Model/Configuration.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Configuration
1313
const TRACKING_NUMBER_SENDING_ENABLED_CONFIG_PATH = 'allegro/order/tracking_number_sending_enabled';
1414
const DEBUG_MODE_ENABLED_CONFIG_PATH = 'allegro/debug_mode/debug_mode_enabled';
1515
const EAN_ATTRIBUTE_CONFIG_PATH = 'allegro/offer_create/ean_attribute';
16+
const DESCRIPTION_ATTRIBUTE_CONFIG_PATH = 'allegro/offer_create/description_attribute';
1617
const STORE_ID_CONFIG_PATH = 'allegro/order/store';
1718
const RESERVATIONS_ENABLED_CONFIG_PATH = 'allegro/order/reservations_enabled';
1819
const LAST_EVENT_ID_FLAG_NAME = 'allegro_order_last_event_id';
@@ -114,6 +115,18 @@ public function getEanAttributeCode(
114115
return $this->scopeConfig->getValue(self::EAN_ATTRIBUTE_CONFIG_PATH, $scopeType, $scopeCode);
115116
}
116117

118+
/**
119+
* @param string $scopeType
120+
* @param string|null $scopeCode
121+
* @return string|null
122+
*/
123+
public function getDescriptionAttributeCode(
124+
string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
125+
?string $scopeCode = null
126+
): ?string {
127+
return $this->scopeConfig->getValue(self::DESCRIPTION_ATTRIBUTE_CONFIG_PATH, $scopeType, $scopeCode);
128+
}
129+
117130
/**
118131
* @return int
119132
*/

Model/Consumer.php

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
use Macopedia\Allegro\Api\Data\PublicationCommandInterfaceFactory;
99
use Macopedia\Allegro\Api\OfferRepositoryInterface;
1010
use Macopedia\Allegro\Api\PublicationCommandRepositoryInterface;
11+
use Macopedia\Allegro\Api\QuantityCommandInterface;
1112
use Macopedia\Allegro\Logger\Logger;
1213
use Macopedia\Allegro\Model\Api\ClientException;
1314
use Macopedia\Allegro\Model\Api\Credentials;
15+
use Magento\CatalogInventory\Model\Indexer\Stock\Processor;
16+
use Magento\Framework\Exception\CouldNotSaveException;
1417
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;
1518

1619
/**
@@ -19,11 +22,11 @@
1922
class Consumer implements ConsumerInterface
2023
{
2124
/**
22-
* @var \Macopedia\Allegro\Api\QuantityCommandInterface
25+
* @var QuantityCommandInterface
2326
*/
2427
protected $quantityCommand;
2528
/**
26-
* @var \Magento\CatalogInventory\Model\Indexer\Stock\Processor
29+
* @var Processor
2730
*/
2831
protected $indexerProcessor;
2932
/** @var Logger */
@@ -59,9 +62,9 @@ class Consumer implements ConsumerInterface
5962
* @param OfferRepositoryInterface $offerRepository
6063
* @param PublicationCommandRepositoryInterface $publicationCommandRepository
6164
* @param PublicationCommandInterfaceFactory $publicationCommandFactory
62-
* @param \Macopedia\Allegro\Api\QuantityCommandInterface $quantityCommand
65+
* @param QuantityCommandInterface $quantityCommand
6366
* @param Configuration $config
64-
* @param \Magento\CatalogInventory\Model\Indexer\Stock\Processor $indexerProcessor
67+
* @param Processor $indexerProcessor
6568
*/
6669
public function __construct(
6770
Logger $logger,
@@ -71,20 +74,20 @@ public function __construct(
7174
OfferRepositoryInterface $offerRepository,
7275
PublicationCommandRepositoryInterface $publicationCommandRepository,
7376
PublicationCommandInterfaceFactory $publicationCommandFactory,
74-
\Macopedia\Allegro\Api\QuantityCommandInterface $quantityCommand,
77+
QuantityCommandInterface $quantityCommand,
7578
Configuration $config,
76-
\Magento\CatalogInventory\Model\Indexer\Stock\Processor $indexerProcessor
79+
Processor $indexerProcessor
7780
) {
78-
$this->logger = $logger;
79-
$this->productRepository = $productRepository;
80-
$this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
81-
$this->credentials = $credentials;
82-
$this->offerRepository = $offerRepository;
81+
$this->logger = $logger;
82+
$this->productRepository = $productRepository;
83+
$this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
84+
$this->credentials = $credentials;
85+
$this->offerRepository = $offerRepository;
8386
$this->publicationCommandRepository = $publicationCommandRepository;
84-
$this->publicationCommandFactory = $publicationCommandFactory;
85-
$this->config = $config;
86-
$this->quantityCommand = $quantityCommand;
87-
$this->indexerProcessor = $indexerProcessor;
87+
$this->publicationCommandFactory = $publicationCommandFactory;
88+
$this->config = $config;
89+
$this->quantityCommand = $quantityCommand;
90+
$this->indexerProcessor = $indexerProcessor;
8891
}
8992

9093
/**
@@ -115,31 +118,36 @@ public function processMessage(MessageInterface $message)
115118
} catch (\Exception $exception) {
116119
$this->logger->error($exception->getMessage(), $exception->getTrace());
117120
}
121+
122+
$offer = $this->offerRepository->get($allegroOfferId);
118123
$productStock = $this->getSalableQuantityDataBySku->execute($product->getSku());
119-
if (isset($productStock[0]) && isset($productStock[0]['qty'])) {
120-
$qty = $productStock[0]['qty'];
121-
if ($qty > 0) {
122-
$this->quantityCommand->change($allegroOfferId, $qty);
123-
$this->savePublicationCommand(
124-
$allegroOfferId,
125-
PublicationCommandInterface::ACTION_ACTIVATE
126-
);
124+
if (!isset($productStock[0]['qty'])) {
125+
return;
126+
}
127127

128-
} else {
128+
$qty = $productStock[0]['qty'];
129+
if ($qty > 0) {
130+
$this->quantityCommand->change($allegroOfferId, $qty);
131+
if (!$offer->isDraft()) {
129132
$this->savePublicationCommand(
130133
$allegroOfferId,
131-
PublicationCommandInterface::ACTION_END
134+
PublicationCommandInterface::ACTION_ACTIVATE
132135
);
133-
134136
}
135-
136-
$this->logger->info(
137-
sprintf(
138-
'Quantity of offer with external id %s has been successfully updated',
139-
$allegroOfferId
140-
)
137+
} else {
138+
$this->savePublicationCommand(
139+
$allegroOfferId,
140+
PublicationCommandInterface::ACTION_END
141141
);
142+
142143
}
144+
145+
$this->logger->info(
146+
sprintf(
147+
'Quantity of offer with external id %s has been successfully updated',
148+
$allegroOfferId
149+
)
150+
);
143151
}
144152

145153
} catch (\Exception $e) {
@@ -152,11 +160,10 @@ public function processMessage(MessageInterface $message)
152160
* @param string $offerId
153161
* @param string $action
154162
* @throws ClientException
155-
* @throws \Magento\Framework\Exception\CouldNotSaveException
163+
* @throws CouldNotSaveException
156164
*/
157165
private function savePublicationCommand(string $offerId, string $action)
158166
{
159-
/** @var PublicationCommandInterface $publicationCommand */
160167
$publicationCommand = $this->publicationCommandFactory->create();
161168
$publicationCommand->setOfferId($offerId);
162169
$publicationCommand->setAction($action);

Model/Data/Offer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,14 @@ public function canBeEnded(): bool
353353
return $this->getPublicationStatus() == self::PUBLICATION_STATUS_ACTIVE;
354354
}
355355

356+
/**
357+
* @return bool
358+
*/
359+
public function isDraft(): bool
360+
{
361+
return $this->getPublicationStatus() == self::PUBLICATION_STATUS_INACTIVE;
362+
}
363+
356364
/**
357365
* @return bool
358366
*/

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ Za pomocą wtyczki możemy wystawiać produkty z Magento na Allegro. Aby to zrob
146146
2. Dodać informacje o [zwrotach](https://allegro.pl/dla-sprzedajacych/warunki-oferty-zwroty-a124GwdXZFA), [reklamacji](https://allegro.pl/dla-sprzedajacych/warunki-oferty-reklamacje-vKgeWL5GnHA) oraz [gwarancji](https://allegro.pl/dla-sprzedajacych/warunki-oferty-gwarancje-9dXYn0VeXHM) na Allegro (wymagane tylko dla konta firmowego)
147147
3. Uzupełnić informacje o loklizacji (Sklepy->Konfiguracja->Allegro->Konfiguracja->Pochodzenie)
148148
![origin_configuration](README/originConfiguration.png)
149-
4. (opcjonalnie) Wybrać atrybut produktu, z którego ma być pobierany kod EAN (Sklepy->Konfiguracja->Allegro->Konfiguracja->Tworzenie oferty)
150-
![ean_select](README/allegroEanSelect.png)
149+
4. (opcjonalnie) Wybrać atrybuty produktów, z których ma być pobierany kod EAN oraz opis (Sklepy->Konfiguracja->Allegro->Konfiguracja->Tworzenie oferty)
150+
![ean_select](README/allegroOfferCreateConfiguration.png)
151151

152152
Po wprowadzeniu wymaganych danych można zacząć wystawiać oferty z poziomu Magento.
153153
Należy wybrać produkt, który chcemy wstawić, wejść na jego stronę i wybrać zdjęcie do oferty Allegro. Żeby, to zrobić wystarczy kliknąć zdjęcie, zaznaczyć rolę 'Allegro', a następnie zapisać produkt.

README/allegroEanSelect.png

-8.84 KB
Binary file not shown.
16.2 KB
Loading

Ui/AllegroOffer/Form/CreateDataProvider.php

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

1616
class CreateDataProvider extends DataProvider
1717
{
18-
1918
/** @var GetSalableQuantityDataBySku */
2019
protected $getSalableQuantityDataBySku;
2120

@@ -31,6 +30,8 @@ class CreateDataProvider extends DataProvider
3130
* @param string $primaryFieldName
3231
* @param string $requestFieldName
3332
* @param GetSalableQuantityDataBySku $getSalableQuantityDataBySku
33+
* @param Registry $registry
34+
* @param Configuration $config
3435
* @param ReportingInterface $reporting
3536
* @param SearchCriteriaBuilder $searchCriteriaBuilder
3637
* @param RequestInterface $request
@@ -72,7 +73,6 @@ public function __construct(
7273
* Get data
7374
*
7475
* @return array
75-
* @throws \Magento\Framework\Exception\NoSuchEntityException
7676
*/
7777
public function getData()
7878
{
@@ -94,13 +94,16 @@ public function getData()
9494
}
9595

9696
$eanAttributeCode = $this->config->getEanAttributeCode();
97+
$descriptionAttributeCode = $this->config->getDescriptionAttributeCode();
9798

9899
$this->_loadedData[$product->getId()] = [
99100
'allegro' => [
100101
'product' => $product->getId(),
101102
'ean' => $eanAttributeCode ? $product->getData($eanAttributeCode) : '',
102103
'name' => $product->getName(),
103-
'description' => $product->getDescription(),
104+
'description' => $descriptionAttributeCode
105+
? $product->getData($descriptionAttributeCode)
106+
: $product->getDescription(),
104107
'price' => $product->getPrice(),
105108
'images' => isset($images['items']) ? $images['items'] : [],
106109
'qty' => $stock[0]['qty']

etc/adminhtml/system.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686
<label>Product attribute for getting EAN</label>
8787
<source_model>Macopedia\Allegro\Model\Config\Source\ProductAttributes</source_model>
8888
</field>
89+
<field id="description_attribute" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
90+
<label>Product attribute for getting description</label>
91+
<source_model>Macopedia\Allegro\Model\Config\Source\ProductAttributes</source_model>
92+
</field>
8993
</group>
9094
<group id="delivery" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
9195
<label>Delivery</label>

0 commit comments

Comments
 (0)