| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Products\Discount; |
| 4 |
|
| 5 |
use SyncBasalam\Utilities\ProductMetaKey; |
| 6 |
|
| 7 |
defined('ABSPATH') || exit; |
| 8 |
|
| 9 |
class SimpleProductDiscount implements DiscountInterface |
| 10 |
{ |
| 11 |
private $discountService; |
| 12 |
|
| 13 |
public function __construct(DiscountManager $discountService) |
| 14 |
{ |
| 15 |
$this->discountService = $discountService; |
| 16 |
} |
| 17 |
|
| 18 |
public function apply($product): void |
| 19 |
{ |
| 20 |
$basalam_id = get_post_meta($product->get_id(), ProductMetaKey::basalamProductId(), true); |
| 21 |
if (!$basalam_id) return; |
| 22 |
|
| 23 |
$regular = $product->get_regular_price(); |
| 24 |
$sale = $product->get_sale_price(); |
| 25 |
|
| 26 |
if (!$regular || !$sale) return; |
| 27 |
|
| 28 |
$discount = $this->discountService->calculateDiscountPercent($regular, $sale); |
| 29 |
$this->discountService->apply($discount, [$basalam_id], null, null); |
| 30 |
} |
| 31 |
|
| 32 |
public function remove($product): void |
| 33 |
{ |
| 34 |
$basalam_id = get_post_meta($product->get_id(), ProductMetaKey::basalamProductId(), true); |
| 35 |
if ($basalam_id) { |
| 36 |
$this->discountService->remove([$basalam_id], null); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|