Skip to content

Commit 5ebcf25

Browse files
committed
Merge branch 'mstrzyzewski-product_prices'
2 parents 4bb58d8 + c2134ee commit 5ebcf25

File tree

12 files changed

+323
-12
lines changed

12 files changed

+323
-12
lines changed

Api/PriceCommandInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Macopedia\Allegro\Api;
4+
5+
interface PriceCommandInterface
6+
{
7+
/**
8+
* @param string $offerId
9+
* @param float $price
10+
* @return array
11+
*/
12+
public function change(string $offerId, float $price);
13+
}

Model/AllegroPrice.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
4+
namespace Macopedia\Allegro\Model;
5+
6+
7+
use Magento\Catalog\Api\Data\ProductInterface;
8+
use Magento\Catalog\Model\Product;
9+
use Magento\Eav\Model\Config;
10+
use Magento\Framework\App\ResourceConnection;
11+
12+
class AllegroPrice
13+
{
14+
/** @var Configuration */
15+
protected $config;
16+
17+
/** @var Config */
18+
protected $eavConfig;
19+
20+
/** @var ResourceConnection */
21+
protected $resource;
22+
23+
/**
24+
* AllegroPrice constructor.
25+
* @param Configuration $config
26+
* @param Config $eavConfig
27+
* @param ResourceConnection $resource
28+
*/
29+
public function __construct(
30+
Configuration $config,
31+
Config $eavConfig,
32+
ResourceConnection $resource
33+
) {
34+
$this->config = $config;
35+
$this->eavConfig = $eavConfig;
36+
$this->resource = $resource;
37+
}
38+
39+
/**
40+
* @param ProductInterface $product
41+
* @return float|int|mixed|null
42+
*/
43+
public function get(ProductInterface $product)
44+
{
45+
$price = (float)$product->getData($this->getPriceAttributeCode());
46+
return $this->calculateNewPrice($price);
47+
}
48+
49+
/**
50+
* @param string $productId
51+
* @param int $storeId
52+
* @return float|int
53+
* @throws AllegroPriceGettingException
54+
* @throws \Magento\Framework\Exception\LocalizedException
55+
*/
56+
public function getByProductId(string $productId, int $storeId = 0)
57+
{
58+
$priceAttribute = $this->eavConfig->getAttribute(Product::ENTITY, $this->getPriceAttributeCode());
59+
60+
$connection = $this->resource->getConnection();
61+
62+
$select = $connection->select()
63+
->from($priceAttribute->getBackendTable(), ['value'])
64+
->where('entity_id = :productId')
65+
->where('store_id = :storeId')
66+
->where('attribute_id = :attributeId')
67+
->where('value IS NOT NULL');
68+
69+
$bind = [
70+
':productId' => (int)$productId,
71+
':storeId' => (int)$storeId,
72+
':attributeId' => (int)$priceAttribute->getId()
73+
];
74+
75+
$price = $connection->fetchOne($select, $bind);
76+
if (is_null($price)) {
77+
throw new AllegroPriceGettingException(
78+
"Error while trying to get Allegro price for product with id {$productId}",
79+
1603885321
80+
);
81+
}
82+
83+
return $this->calculateNewPrice((float)$price);
84+
}
85+
86+
/**
87+
* @return string|null
88+
*/
89+
protected function getPriceAttributeCode()
90+
{
91+
return $this->config->getPriceAttributeCode();;
92+
}
93+
94+
/**
95+
* @param float $price
96+
* @return float|int
97+
*/
98+
protected function calculateNewPrice(float $price)
99+
{
100+
if ($this->config->isPricePolicyEnabled()) {
101+
$percentIncrease = $this->config->getPricePercentIncrease();
102+
$price = $price + $price * $percentIncrease / 100;
103+
}
104+
105+
return $price;
106+
}
107+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
4+
namespace Macopedia\Allegro\Model;
5+
6+
7+
class AllegroPriceGettingException extends \Exception
8+
{
9+
10+
}

Model/Configuration.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ class Configuration
1414
const DEBUG_MODE_ENABLED_CONFIG_PATH = 'allegro/debug_mode/debug_mode_enabled';
1515
const EAN_ATTRIBUTE_CONFIG_PATH = 'allegro/offer_create/ean_attribute';
1616
const DESCRIPTION_ATTRIBUTE_CONFIG_PATH = 'allegro/offer_create/description_attribute';
17+
const PRICE_ATTRIBUTE_CONFIG_PATH = 'allegro/offer_create/price_attribute';
1718
const STORE_ID_CONFIG_PATH = 'allegro/order/store';
1819
const RESERVATIONS_ENABLED_CONFIG_PATH = 'allegro/order/reservations_enabled';
20+
const PRICE_POLICY_ENABLED_CONFIG_PATH = 'allegro/price_policy/price_policy_enabled';
21+
const PERCENT_INCREASE_CONFIG_PATH = 'allegro/price_policy/percent_increase';
1922
const LAST_EVENT_ID_FLAG_NAME = 'allegro_order_last_event_id';
2023
const LAST_USER_ID_FLAG_NAME = 'allegro_credentials_last_user_id';
2124
const INITIALIZATION_TIME_FLAG_NAME = 'allegro_initialization_time';
@@ -87,6 +90,30 @@ public function areReservationsEnabled(
8790
return $this->scopeConfig->isSetFlag(self::RESERVATIONS_ENABLED_CONFIG_PATH, $scopeType, $scopeCode);
8891
}
8992

93+
/**
94+
* @param string $scopeType
95+
* @param string|null $scopeCode
96+
* @return bool
97+
*/
98+
public function isPricePolicyEnabled(
99+
string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
100+
?string $scopeCode = null
101+
): bool {
102+
return $this->scopeConfig->isSetFlag(self::PRICE_POLICY_ENABLED_CONFIG_PATH, $scopeType, $scopeCode);
103+
}
104+
105+
/**
106+
* @param string $scopeType
107+
* @param string|null $scopeCode
108+
* @return string|null
109+
*/
110+
public function getPricePercentIncrease(
111+
string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
112+
?string $scopeCode = null
113+
): ?string {
114+
return $this->scopeConfig->getValue(self::PERCENT_INCREASE_CONFIG_PATH, $scopeType, $scopeCode);
115+
}
116+
90117
/**
91118
* @return string|null
92119
*/
@@ -127,6 +154,18 @@ public function getDescriptionAttributeCode(
127154
return $this->scopeConfig->getValue(self::DESCRIPTION_ATTRIBUTE_CONFIG_PATH, $scopeType, $scopeCode);
128155
}
129156

157+
/**
158+
* @param string $scopeType
159+
* @param string|null $scopeCode
160+
* @return string|null
161+
*/
162+
public function getPriceAttributeCode(
163+
string $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
164+
?string $scopeCode = null
165+
): ?string {
166+
return $this->scopeConfig->getValue(self::PRICE_ATTRIBUTE_CONFIG_PATH, $scopeType, $scopeCode);
167+
}
168+
130169
/**
131170
* @return int
132171
*/

Model/Consumer.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use Macopedia\Allegro\Api\Data\PublicationCommandInterface;
88
use Macopedia\Allegro\Api\Data\PublicationCommandInterfaceFactory;
99
use Macopedia\Allegro\Api\OfferRepositoryInterface;
10+
use Macopedia\Allegro\Api\PriceCommandInterface;
1011
use Macopedia\Allegro\Api\PublicationCommandRepositoryInterface;
1112
use Macopedia\Allegro\Api\QuantityCommandInterface;
1213
use Macopedia\Allegro\Logger\Logger;
1314
use Macopedia\Allegro\Model\Api\ClientException;
1415
use Macopedia\Allegro\Model\Api\Credentials;
16+
use Magento\Catalog\Api\Data\ProductInterface;
1517
use Magento\CatalogInventory\Model\Indexer\Stock\Processor;
1618
use Magento\Framework\Exception\CouldNotSaveException;
1719
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;
@@ -53,6 +55,12 @@ class Consumer implements ConsumerInterface
5355
/** @var Configuration */
5456
private $config;
5557

58+
/** @var PriceCommandInterface */
59+
private $priceCommand;
60+
61+
/** @var AllegroPrice */
62+
protected $allegroPrice;
63+
5664
/**
5765
* Consumer constructor.
5866
* @param Logger $logger
@@ -65,6 +73,8 @@ class Consumer implements ConsumerInterface
6573
* @param QuantityCommandInterface $quantityCommand
6674
* @param Configuration $config
6775
* @param Processor $indexerProcessor
76+
* @param PriceCommandInterface $priceCommand
77+
* @param AllegroPrice $allegroPrice
6878
*/
6979
public function __construct(
7080
Logger $logger,
@@ -76,7 +86,9 @@ public function __construct(
7686
PublicationCommandInterfaceFactory $publicationCommandFactory,
7787
QuantityCommandInterface $quantityCommand,
7888
Configuration $config,
79-
Processor $indexerProcessor
89+
Processor $indexerProcessor,
90+
PriceCommandInterface $priceCommand,
91+
AllegroPrice $allegroPrice
8092
) {
8193
$this->logger = $logger;
8294
$this->productRepository = $productRepository;
@@ -88,6 +100,8 @@ public function __construct(
88100
$this->config = $config;
89101
$this->quantityCommand = $quantityCommand;
90102
$this->indexerProcessor = $indexerProcessor;
103+
$this->priceCommand = $priceCommand;
104+
$this->allegroPrice = $allegroPrice;
91105
}
92106

93107
/**
@@ -119,6 +133,8 @@ public function processMessage(MessageInterface $message)
119133
$this->logger->error($exception->getMessage(), $exception->getTrace());
120134
}
121135

136+
$this->updateOfferPrice($product, $allegroOfferId);
137+
122138
$offer = $this->offerRepository->get($allegroOfferId);
123139
$productStock = $this->getSalableQuantityDataBySku->execute($product->getSku());
124140
if (!isset($productStock[0]['qty'])) {
@@ -144,7 +160,7 @@ public function processMessage(MessageInterface $message)
144160

145161
$this->logger->info(
146162
sprintf(
147-
'Quantity of offer with external id %s has been successfully updated',
163+
'Quantity and price of offer with external id %s have been successfully updated',
148164
$allegroOfferId
149165
)
150166
);
@@ -170,4 +186,23 @@ private function savePublicationCommand(string $offerId, string $action)
170186

171187
$this->publicationCommandRepository->save($publicationCommand);
172188
}
189+
190+
/**
191+
* @param ProductInterface $product
192+
* @param string $offerId
193+
* @throws AllegroPriceGettingException
194+
* @throws \Magento\Framework\Exception\LocalizedException
195+
*/
196+
private function updateOfferPrice(ProductInterface $product, string $offerId)
197+
{
198+
if (!$this->config->isPricePolicyEnabled()) {
199+
return;
200+
}
201+
202+
$price = $this->allegroPrice->getByProductId($product->getId());
203+
204+
if ($price > 0) {
205+
$this->priceCommand->change($offerId, $price);
206+
}
207+
}
173208
}

Model/PriceCommand.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Macopedia\Allegro\Model;
4+
5+
use Macopedia\Allegro\Api\PriceCommandInterface;
6+
use Macopedia\Allegro\Model\Api\ClientResponseException;
7+
use Macopedia\Allegro\Model\ResourceModel\Sale\Offers;
8+
use Macopedia\Allegro\Model\Api\ClientException;
9+
10+
class PriceCommand implements PriceCommandInterface
11+
{
12+
13+
/** @var Offers */
14+
private $offers;
15+
16+
/**
17+
* PriceCommand constructor.
18+
* @param Offers $offers
19+
*/
20+
public function __construct(Offers $offers)
21+
{
22+
$this->offers = $offers;
23+
}
24+
25+
/**
26+
* @param string $offerId
27+
* @param float $price
28+
* @return array
29+
* @throws Api\ClientResponseErrorException
30+
* @throws ClientException
31+
* @throws ClientResponseException
32+
*/
33+
public function change(string $offerId, float $price)
34+
{
35+
return $this->offers->changePrice($offerId, $price);
36+
}
37+
}

0 commit comments

Comments
 (0)