Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Model/Write/Products/CollectionDecorator/DiscountPercentage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Tweakwise\Magento2TweakwiseExport\Model\Write\Products\CollectionDecorator;

use Magento\Bundle\Model\Product\Type as BundleType;
use Tweakwise\Magento2TweakwiseExport\Exception\InvalidArgumentException;
use Tweakwise\Magento2TweakwiseExport\Model\Write\Products\Collection;
use Tweakwise\Magento2TweakwiseExport\Model\Write\Products\ExportEntity;

class DiscountPercentage implements DecoratorInterface
{
private const ATTRIBUTE_NAME = 'discount_percentage';

/**
* Decorate items with a computed discount_percentage attribute.
* Skipped for bundle products (pricing too complex) and products with no discount.
*
* @param Collection $collection
*/
public function decorate(Collection $collection): void
{
foreach ($collection as $entity) {
if ($entity->getTypeId() === BundleType::TYPE_CODE) {
continue;
}

$discount = $this->calculateDiscount($entity);
if ($discount <= 0) {
continue;
}

$entity->addAttribute(self::ATTRIBUTE_NAME, $discount);
}
}

/**
* @param ExportEntity $entity
* @return int
*/
private function calculateDiscount(ExportEntity $entity): int
{
try {
$regularPrice = (float)$entity->getAttribute('regular_price', false);
$finalPrice = (float)$entity->getAttribute('final_price', false);
} catch (InvalidArgumentException $e) {
return 0;
}

if ($regularPrice <= 0.00001 || $finalPrice >= $regularPrice) {
return 0;
}

return (int)round(($regularPrice - $finalPrice) / $regularPrice * 100);
}
}
1 change: 1 addition & 0 deletions Model/Write/Products/CollectionDecorator/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function decorate(Collection|PriceCollection $collection): void

$row = $this->applyCombinedPrices($row, $product, $store);
$row = $this->applyPriceFields($row, $priceFields);
$row['regular_price'] = (float) $row['price'];
$row['price'] = $this->getPriceValue($row, $priceFields);

$collection->get($entityId)->setFromArray($row);
Expand Down
2 changes: 2 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<item name="stock_data" xsi:type="object">Tweakwise\Magento2TweakwiseExport\Model\Write\Products\CollectionDecorator\StockData</item>
<item name="children_attributes" xsi:type="object">Tweakwise\Magento2TweakwiseExport\Model\Write\Products\CollectionDecorator\ChildrenAttributes</item>
<item name="price" xsi:type="object">Tweakwise\Magento2TweakwiseExport\Model\Write\Products\CollectionDecorator\Price</item>
<item name="discount_percentage" xsi:type="object">Tweakwise\Magento2TweakwiseExport\Model\Write\Products\CollectionDecorator\DiscountPercentage</item>
<item name="website_link" xsi:type="object">Tweakwise\Magento2TweakwiseExport\Model\Write\Products\CollectionDecorator\WebsiteLink</item>
<item name="review" xsi:type="object">Tweakwise\Magento2TweakwiseExport\Model\Write\Products\CollectionDecorator\Review</item>
</argument>
Expand All @@ -104,6 +105,7 @@
<argument name="collectionDecorators" xsi:type="array">
<item name="children" xsi:type="object">Tweakwise\Magento2TweakwiseExport\Model\Write\Products\CollectionDecorator\Children</item>
<item name="price" xsi:type="object">Tweakwise\Magento2TweakwiseExport\Model\Write\Products\CollectionDecorator\Price</item>
<item name="discount_percentage" xsi:type="object">Tweakwise\Magento2TweakwiseExport\Model\Write\Products\CollectionDecorator\DiscountPercentage</item>
<item name="website_link" xsi:type="object">Tweakwise\Magento2TweakwiseExport\Model\Write\Products\CollectionDecorator\WebsiteLink</item>
</argument>
</arguments>
Expand Down
Loading