Skip to content

Commit

Permalink
Add option to select product attribute for offer description (#59)
Browse files Browse the repository at this point in the history
-Add validation for checking if an offer is a draft before activating
  • Loading branch information
mstrzyzewski authored May 22, 2020
1 parent 25c464d commit cdefa7b
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 43 deletions.
5 changes: 5 additions & 0 deletions Api/Data/OfferInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ public function canBePublished(): bool;
*/
public function canBeEnded(): bool;

/**
* @return bool
*/
public function isDraft(): bool;

/**
* @return bool
*/
Expand Down
4 changes: 0 additions & 4 deletions Model/Config/Source/ProductAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ public function toOptionArray()
try {
$searchCriteria = $this->searchCriteriaBuilder->create();
$attributes = $this->attributesRepository->getList(Product::ENTITY, $searchCriteria);
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage('Can\'t get list of product attributes');
$this->messageManager->addExceptionMessage($e);
return [];
} catch (\Exception $e) {
$this->messageManager->addErrorMessage('Can\'t get list of product attributes');
return [];
Expand Down
13 changes: 13 additions & 0 deletions Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Configuration
const TRACKING_NUMBER_SENDING_ENABLED_CONFIG_PATH = 'allegro/order/tracking_number_sending_enabled';
const DEBUG_MODE_ENABLED_CONFIG_PATH = 'allegro/debug_mode/debug_mode_enabled';
const EAN_ATTRIBUTE_CONFIG_PATH = 'allegro/offer_create/ean_attribute';
const DESCRIPTION_ATTRIBUTE_CONFIG_PATH = 'allegro/offer_create/description_attribute';
const STORE_ID_CONFIG_PATH = 'allegro/order/store';
const RESERVATIONS_ENABLED_CONFIG_PATH = 'allegro/order/reservations_enabled';
const LAST_EVENT_ID_FLAG_NAME = 'allegro_order_last_event_id';
Expand Down Expand Up @@ -114,6 +115,18 @@ public function getEanAttributeCode(
return $this->scopeConfig->getValue(self::EAN_ATTRIBUTE_CONFIG_PATH, $scopeType, $scopeCode);
}

/**
* @param string $scopeType
* @param string|null $scopeCode
* @return string|null
*/
public function getDescriptionAttributeCode(
string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
?string $scopeCode = null
): ?string {
return $this->scopeConfig->getValue(self::DESCRIPTION_ATTRIBUTE_CONFIG_PATH, $scopeType, $scopeCode);
}

/**
* @return int
*/
Expand Down
75 changes: 41 additions & 34 deletions Model/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
use Macopedia\Allegro\Api\Data\PublicationCommandInterfaceFactory;
use Macopedia\Allegro\Api\OfferRepositoryInterface;
use Macopedia\Allegro\Api\PublicationCommandRepositoryInterface;
use Macopedia\Allegro\Api\QuantityCommandInterface;
use Macopedia\Allegro\Logger\Logger;
use Macopedia\Allegro\Model\Api\ClientException;
use Macopedia\Allegro\Model\Api\Credentials;
use Magento\CatalogInventory\Model\Indexer\Stock\Processor;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;

/**
Expand All @@ -19,11 +22,11 @@
class Consumer implements ConsumerInterface
{
/**
* @var \Macopedia\Allegro\Api\QuantityCommandInterface
* @var QuantityCommandInterface
*/
protected $quantityCommand;
/**
* @var \Magento\CatalogInventory\Model\Indexer\Stock\Processor
* @var Processor
*/
protected $indexerProcessor;
/** @var Logger */
Expand Down Expand Up @@ -59,9 +62,9 @@ class Consumer implements ConsumerInterface
* @param OfferRepositoryInterface $offerRepository
* @param PublicationCommandRepositoryInterface $publicationCommandRepository
* @param PublicationCommandInterfaceFactory $publicationCommandFactory
* @param \Macopedia\Allegro\Api\QuantityCommandInterface $quantityCommand
* @param QuantityCommandInterface $quantityCommand
* @param Configuration $config
* @param \Magento\CatalogInventory\Model\Indexer\Stock\Processor $indexerProcessor
* @param Processor $indexerProcessor
*/
public function __construct(
Logger $logger,
Expand All @@ -71,20 +74,20 @@ public function __construct(
OfferRepositoryInterface $offerRepository,
PublicationCommandRepositoryInterface $publicationCommandRepository,
PublicationCommandInterfaceFactory $publicationCommandFactory,
\Macopedia\Allegro\Api\QuantityCommandInterface $quantityCommand,
QuantityCommandInterface $quantityCommand,
Configuration $config,
\Magento\CatalogInventory\Model\Indexer\Stock\Processor $indexerProcessor
Processor $indexerProcessor
) {
$this->logger = $logger;
$this->productRepository = $productRepository;
$this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
$this->credentials = $credentials;
$this->offerRepository = $offerRepository;
$this->logger = $logger;
$this->productRepository = $productRepository;
$this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
$this->credentials = $credentials;
$this->offerRepository = $offerRepository;
$this->publicationCommandRepository = $publicationCommandRepository;
$this->publicationCommandFactory = $publicationCommandFactory;
$this->config = $config;
$this->quantityCommand = $quantityCommand;
$this->indexerProcessor = $indexerProcessor;
$this->publicationCommandFactory = $publicationCommandFactory;
$this->config = $config;
$this->quantityCommand = $quantityCommand;
$this->indexerProcessor = $indexerProcessor;
}

/**
Expand Down Expand Up @@ -115,31 +118,36 @@ public function processMessage(MessageInterface $message)
} catch (\Exception $exception) {
$this->logger->error($exception->getMessage(), $exception->getTrace());
}

$offer = $this->offerRepository->get($allegroOfferId);
$productStock = $this->getSalableQuantityDataBySku->execute($product->getSku());
if (isset($productStock[0]) && isset($productStock[0]['qty'])) {
$qty = $productStock[0]['qty'];
if ($qty > 0) {
$this->quantityCommand->change($allegroOfferId, $qty);
$this->savePublicationCommand(
$allegroOfferId,
PublicationCommandInterface::ACTION_ACTIVATE
);
if (!isset($productStock[0]['qty'])) {
return;
}

} else {
$qty = $productStock[0]['qty'];
if ($qty > 0) {
$this->quantityCommand->change($allegroOfferId, $qty);
if (!$offer->isDraft()) {
$this->savePublicationCommand(
$allegroOfferId,
PublicationCommandInterface::ACTION_END
PublicationCommandInterface::ACTION_ACTIVATE
);

}

$this->logger->info(
sprintf(
'Quantity of offer with external id %s has been successfully updated',
$allegroOfferId
)
} else {
$this->savePublicationCommand(
$allegroOfferId,
PublicationCommandInterface::ACTION_END
);

}

$this->logger->info(
sprintf(
'Quantity of offer with external id %s has been successfully updated',
$allegroOfferId
)
);
}

} catch (\Exception $e) {
Expand All @@ -152,11 +160,10 @@ public function processMessage(MessageInterface $message)
* @param string $offerId
* @param string $action
* @throws ClientException
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws CouldNotSaveException
*/
private function savePublicationCommand(string $offerId, string $action)
{
/** @var PublicationCommandInterface $publicationCommand */
$publicationCommand = $this->publicationCommandFactory->create();
$publicationCommand->setOfferId($offerId);
$publicationCommand->setAction($action);
Expand Down
8 changes: 8 additions & 0 deletions Model/Data/Offer.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,14 @@ public function canBeEnded(): bool
return $this->getPublicationStatus() == self::PUBLICATION_STATUS_ACTIVE;
}

/**
* @return bool
*/
public function isDraft(): bool
{
return $this->getPublicationStatus() == self::PUBLICATION_STATUS_INACTIVE;
}

/**
* @return bool
*/
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ Za pomocą wtyczki możemy wystawiać produkty z Magento na Allegro. Aby to zrob
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)
3. Uzupełnić informacje o loklizacji (Sklepy->Konfiguracja->Allegro->Konfiguracja->Pochodzenie)
![origin_configuration](README/originConfiguration.png)
4. (opcjonalnie) Wybrać atrybut produktu, z którego ma być pobierany kod EAN (Sklepy->Konfiguracja->Allegro->Konfiguracja->Tworzenie oferty)
![ean_select](README/allegroEanSelect.png)
4. (opcjonalnie) Wybrać atrybuty produktów, z których ma być pobierany kod EAN oraz opis (Sklepy->Konfiguracja->Allegro->Konfiguracja->Tworzenie oferty)
![ean_select](README/allegroOfferCreateConfiguration.png)

Po wprowadzeniu wymaganych danych można zacząć wystawiać oferty z poziomu Magento.
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.
Expand Down
Binary file removed README/allegroEanSelect.png
Binary file not shown.
Binary file added README/allegroOfferCreateConfiguration.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions Ui/AllegroOffer/Form/CreateDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

class CreateDataProvider extends DataProvider
{

/** @var GetSalableQuantityDataBySku */
protected $getSalableQuantityDataBySku;

Expand All @@ -31,6 +30,8 @@ class CreateDataProvider extends DataProvider
* @param string $primaryFieldName
* @param string $requestFieldName
* @param GetSalableQuantityDataBySku $getSalableQuantityDataBySku
* @param Registry $registry
* @param Configuration $config
* @param ReportingInterface $reporting
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param RequestInterface $request
Expand Down Expand Up @@ -72,7 +73,6 @@ public function __construct(
* Get data
*
* @return array
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getData()
{
Expand All @@ -94,13 +94,16 @@ public function getData()
}

$eanAttributeCode = $this->config->getEanAttributeCode();
$descriptionAttributeCode = $this->config->getDescriptionAttributeCode();

$this->_loadedData[$product->getId()] = [
'allegro' => [
'product' => $product->getId(),
'ean' => $eanAttributeCode ? $product->getData($eanAttributeCode) : '',
'name' => $product->getName(),
'description' => $product->getDescription(),
'description' => $descriptionAttributeCode
? $product->getData($descriptionAttributeCode)
: $product->getDescription(),
'price' => $product->getPrice(),
'images' => isset($images['items']) ? $images['items'] : [],
'qty' => $stock[0]['qty']
Expand Down
4 changes: 4 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
<label>Product attribute for getting EAN</label>
<source_model>Macopedia\Allegro\Model\Config\Source\ProductAttributes</source_model>
</field>
<field id="description_attribute" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Product attribute for getting description</label>
<source_model>Macopedia\Allegro\Model\Config\Source\ProductAttributes</source_model>
</field>
</group>
<group id="delivery" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Delivery</label>
Expand Down
1 change: 1 addition & 0 deletions i18n/pl_PL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
"Maximum name length cannot exceed 50 characters, but some characters are counted as longer: &, "", <, >","Maksymalna długość nazwy nie może przekraczać 50 znaków, jednak niektóre znaki są liczone jako dłuższe: &, "", <, >"
"Offer create","Tworzenie oferty"
"Product attribute for getting EAN","Atrybut produktu do pobrania EAN"
"Product attribute for getting description","Atrybut produktu do pobrania opisu"
"Can't get list of product attributes,"Nie można pobrać listy atrybutów produktu"
"This is not a valid EAN number","To nie jest poprawny numer EAN"
"Debug mode enabled", "Tryb debugowania włączony"
Expand Down

0 comments on commit cdefa7b

Please sign in to comment.