|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author Amasty Team |
| 4 | + * @copyright Copyright (c) Amasty (https://www.amasty.com) |
| 5 | + * @package Smart Discounts Hook |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Amasty\SmartDiscountsHook\Model\Webhook\HookField; |
| 11 | + |
| 12 | +use Magento\AdobeCommerceWebhooks\Model\Filter\Converter\FieldConverterInterface; |
| 13 | +use Magento\AdobeCommerceWebhooks\Model\Webhook\HookField; |
| 14 | +use Magento\Framework\Pricing\PriceCurrencyInterface; |
| 15 | + |
| 16 | +class TotalConverter implements FieldConverterInterface |
| 17 | +{ |
| 18 | + public const COLLECTOR_TYPE_CODE = 'discount'; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var PriceCurrencyInterface |
| 22 | + */ |
| 23 | + private PriceCurrencyInterface $priceCurrency; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + PriceCurrencyInterface $priceCurrency |
| 27 | + ) { |
| 28 | + $this->priceCurrency = $priceCurrency; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @inheritDoc |
| 33 | + */ |
| 34 | + public function toExternalFormat(mixed $value, HookField $field, array $pluginData) |
| 35 | + { |
| 36 | + return $value; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @inheritDoc |
| 41 | + */ |
| 42 | + public function fromExternalFormat(mixed $value, HookField $field, array $pluginData) |
| 43 | + { |
| 44 | + if (isset($value['amsd_base_discount']) && $value['amsd_base_discount'] !== 0) { |
| 45 | + $this->setTotalAmount($pluginData, $value); |
| 46 | + $this->setDiscountToValue($pluginData, $value); |
| 47 | + $this->updateDiscountAmountForQuoteItems($pluginData); |
| 48 | + } |
| 49 | + |
| 50 | + return $value; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param array $pluginData |
| 55 | + * @param array $value |
| 56 | + * @return void |
| 57 | + */ |
| 58 | + private function setTotalAmount(array $pluginData, $value): void |
| 59 | + { |
| 60 | + $total = $pluginData['total']; |
| 61 | + $quote = $pluginData['quote']; |
| 62 | + $discount = $this->priceCurrency->convert($value['amsd_base_discount'], $quote->getStore()); |
| 63 | + |
| 64 | + $total->addTotalAmount(self::COLLECTOR_TYPE_CODE, -$discount); |
| 65 | + $total->addBaseTotalAmount(self::COLLECTOR_TYPE_CODE, -$value['amsd_base_discount']); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param array $pluginData |
| 70 | + * @param array $value |
| 71 | + * @return void |
| 72 | + */ |
| 73 | + private function setDiscountToValue(array $pluginData, &$value): void |
| 74 | + { |
| 75 | + $total = $pluginData['total']; |
| 76 | + |
| 77 | + $value['discount_amount'] = $total->getDiscountAmount(); |
| 78 | + $value['base_discount_amount'] = $total->getBaseDiscountAmount(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * The webhook sets base discount amount for quote items. |
| 83 | + * We should convert it to discount amount with correct currency. |
| 84 | + * This logic should be in a separate converter. |
| 85 | + * But due to the limitations of applying converters on the array we have to do it here. |
| 86 | + * |
| 87 | + * @param array $pluginData |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + private function updateDiscountAmountForQuoteItems(array $pluginData): void |
| 91 | + { |
| 92 | + $quote = $pluginData['quote']; |
| 93 | + $shippingAssignment = $pluginData['shippingAssignment']; |
| 94 | + $items = $shippingAssignment->getItems(); |
| 95 | + foreach ($items as $item) { |
| 96 | + if ($item->getBaseDiscountAmount() > 0) { |
| 97 | + $discountAmount = $this->priceCurrency->convert($item->getBaseDiscountAmount(), $quote->getStore()); |
| 98 | + $item->setDiscountAmount($discountAmount); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments