feat(upsell): apply real cart discounts and track performance

This commit is contained in:
2026-05-08 19:16:01 +03:30
parent 8345e94a1b
commit fd9d29a0ee
19 changed files with 747 additions and 206 deletions

View File

@@ -43,8 +43,18 @@ class UpsellService {
}
$price = floatval($product->get_price());
return $this->calculateDiscountedPrice($price, $upsell);
}
public function calculateDiscountedPrice($price, $upsell) {
$price = max(0, floatval($price));
if (!$upsell) {
return $price;
}
if ($upsell->discount_type === 'percentage') {
return max(0, $price * (1 - floatval($upsell->discount_value) / 100));
$percent = max(0, min(100, floatval($upsell->discount_value)));
return max(0, $price * (1 - $percent / 100));
}
if ($upsell->discount_type === 'fixed') {
@@ -54,6 +64,31 @@ class UpsellService {
return $price;
}
public function getById($id) {
return $this->upsellRepository->getById((int) $id);
}
public function isValidForCart($upsell, $cart) {
return $cart && !$cart->is_empty() && $this->cartMatchesTrigger($upsell, $cart);
}
public function canApplyToProduct($upsell, $productId, $variationId = 0) {
if (!$upsell || !$upsell->isActive()) {
return false;
}
$targetProductId = (int) $upsell->target_product_id;
return $targetProductId > 0 && ($targetProductId === (int) $productId || $targetProductId === (int) $variationId);
}
public function incrementImpression($upsellId) {
return $this->upsellRepository->incrementImpression((int) $upsellId);
}
public function incrementConversion($upsellId) {
return $this->upsellRepository->incrementConversion((int) $upsellId);
}
public function getTriggerLabel($upsell) {
switch ($upsell->trigger_type) {
case 'product':