-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- update Allegro offer price after Magento product save - allow increasing prices on Allegro by given percent
- Loading branch information
1 parent
f3aaa2e
commit 85dfd3c
Showing
12 changed files
with
323 additions
and
12 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,13 @@ | ||
<?php | ||
|
||
namespace Macopedia\Allegro\Api; | ||
|
||
interface PriceCommandInterface | ||
{ | ||
/** | ||
* @param string $offerId | ||
* @param float $price | ||
* @return array | ||
*/ | ||
public function change(string $offerId, float $price); | ||
} |
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,107 @@ | ||
<?php | ||
|
||
|
||
namespace Macopedia\Allegro\Model; | ||
|
||
|
||
use Magento\Catalog\Api\Data\ProductInterface; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Eav\Model\Config; | ||
use Magento\Framework\App\ResourceConnection; | ||
|
||
class AllegroPrice | ||
{ | ||
/** @var Configuration */ | ||
protected $config; | ||
|
||
/** @var Config */ | ||
protected $eavConfig; | ||
|
||
/** @var ResourceConnection */ | ||
protected $resource; | ||
|
||
/** | ||
* AllegroPrice constructor. | ||
* @param Configuration $config | ||
* @param Config $eavConfig | ||
* @param ResourceConnection $resource | ||
*/ | ||
public function __construct( | ||
Configuration $config, | ||
Config $eavConfig, | ||
ResourceConnection $resource | ||
) { | ||
$this->config = $config; | ||
$this->eavConfig = $eavConfig; | ||
$this->resource = $resource; | ||
} | ||
|
||
/** | ||
* @param ProductInterface $product | ||
* @return float|int|mixed|null | ||
*/ | ||
public function get(ProductInterface $product) | ||
{ | ||
$price = (float)$product->getData($this->getPriceAttributeCode()); | ||
return $this->calculateNewPrice($price); | ||
} | ||
|
||
/** | ||
* @param string $productId | ||
* @param int $storeId | ||
* @return float|int | ||
* @throws AllegroPriceGettingException | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
public function getByProductId(string $productId, int $storeId = 0) | ||
{ | ||
$priceAttribute = $this->eavConfig->getAttribute(Product::ENTITY, $this->getPriceAttributeCode()); | ||
|
||
$connection = $this->resource->getConnection(); | ||
|
||
$select = $connection->select() | ||
->from($priceAttribute->getBackendTable(), ['value']) | ||
->where('entity_id = :productId') | ||
->where('store_id = :storeId') | ||
->where('attribute_id = :attributeId') | ||
->where('value IS NOT NULL'); | ||
|
||
$bind = [ | ||
':productId' => (int)$productId, | ||
':storeId' => (int)$storeId, | ||
':attributeId' => (int)$priceAttribute->getId() | ||
]; | ||
|
||
$price = $connection->fetchOne($select, $bind); | ||
if (is_null($price)) { | ||
throw new AllegroPriceGettingException( | ||
"Error while trying to get Allegro price for product with id {$productId}", | ||
1603885321 | ||
); | ||
} | ||
|
||
return $this->calculateNewPrice((float)$price); | ||
} | ||
|
||
/** | ||
* @return string|null | ||
*/ | ||
protected function getPriceAttributeCode() | ||
{ | ||
return $this->config->getPriceAttributeCode();; | ||
} | ||
|
||
/** | ||
* @param float $price | ||
* @return float|int | ||
*/ | ||
protected function calculateNewPrice(float $price) | ||
{ | ||
if ($this->config->isPricePolicyEnabled()) { | ||
$percentIncrease = $this->config->getPricePercentIncrease(); | ||
$price = $price + $price * $percentIncrease / 100; | ||
} | ||
|
||
return $price; | ||
} | ||
} |
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,10 @@ | ||
<?php | ||
|
||
|
||
namespace Macopedia\Allegro\Model; | ||
|
||
|
||
class AllegroPriceGettingException extends \Exception | ||
{ | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Macopedia\Allegro\Model; | ||
|
||
use Macopedia\Allegro\Api\PriceCommandInterface; | ||
use Macopedia\Allegro\Model\Api\ClientResponseException; | ||
use Macopedia\Allegro\Model\ResourceModel\Sale\Offers; | ||
use Macopedia\Allegro\Model\Api\ClientException; | ||
|
||
class PriceCommand implements PriceCommandInterface | ||
{ | ||
|
||
/** @var Offers */ | ||
private $offers; | ||
|
||
/** | ||
* PriceCommand constructor. | ||
* @param Offers $offers | ||
*/ | ||
public function __construct(Offers $offers) | ||
{ | ||
$this->offers = $offers; | ||
} | ||
|
||
/** | ||
* @param string $offerId | ||
* @param float $price | ||
* @return array | ||
* @throws Api\ClientResponseErrorException | ||
* @throws ClientException | ||
* @throws ClientResponseException | ||
*/ | ||
public function change(string $offerId, float $price) | ||
{ | ||
return $this->offers->changePrice($offerId, $price); | ||
} | ||
} |
Oops, something went wrong.