Skip to content

Commit

Permalink
fix(analytics): resolve PHP 8 type-safety issues in price and shipping (
Browse files Browse the repository at this point in the history
#68)

* fix(analytics): resolve PHP 8 type-safety issues in price and shipping calculations

* fix(analytics): enforce float type returns for price and shipping methods

---------

Co-authored-by: enes <eneskirca@gmail.com>
  • Loading branch information
eneskirca and enes authored Nov 4, 2024
1 parent 9f9c63c commit b51a30d
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions Model/SendPurchaseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,16 @@ protected function collectProducts(Order $order): array

/**
* Get the actual price the customer also saw in it's cart.
* @phpstan-ignore-next-line
* @return float
*/
private function getPaidProductPrice(Item $orderItem): float|string
private function getPaidProductPrice(Item $orderItem): float
{
return $this->moduleConfiguration
/** @phpstan-ignore-next-line */
$price = $this->moduleConfiguration
->getTaxDisplayType($orderItem->getOrder()?->getStoreId()) === Config::DISPLAY_TYPE_EXCLUDING_TAX
? $orderItem->getBasePrice()
: $orderItem->getBasePriceInclTax();

return (float)$price;
}

public function getTransactionDataObject(Order $order, $elgentosSalesOrder): DataObject
Expand All @@ -199,14 +200,16 @@ public function getTransactionDataObject(Order $order, $elgentosSalesOrder): Dat
$order->getGlobalCurrencyCode() :
$order->getBaseCurrencyCode();

$shippingCosts = $this->getPaidShippingCosts($order);

$transactionDataObject = new DataObject(
[
'transaction_id' => $order->getIncrementId(),
'affiliation' => $order->getStoreName(),
'currency' => $currency,
'value' => $order->getBaseGrandTotal(),
'tax' => $order->getBaseTaxAmount(),
'shipping' => ($this->getPaidShippingCosts($order) ?? 0),
'value' => (float)$order->getBaseGrandTotal(),
'tax' => (float)$order->getBaseTaxAmount(),
'shipping' => $shippingCosts ?? 0.0, // Use 0.0 if null
'coupon_code' => $order->getCouponCode(),
'session_id' => $elgentosSalesOrder->getGaSessionId()
]
Expand All @@ -221,13 +224,20 @@ public function getTransactionDataObject(Order $order, $elgentosSalesOrder): Dat
}

/**
* @phpstan-ignore-next-line
* Get shipping costs
* @return float|null
*/
private function getPaidShippingCosts(Order $order): null|float|string
private function getPaidShippingCosts(Order $order): ?float
{
return $this->moduleConfiguration->getTaxDisplayType($order->getStoreId()) == Config::DISPLAY_TYPE_EXCLUDING_TAX
$shippingAmount = $this->moduleConfiguration->getTaxDisplayType($order->getStoreId()) == Config::DISPLAY_TYPE_EXCLUDING_TAX
? $order->getBaseShippingAmount()
: $order->getBaseShippingInclTax();

if ($shippingAmount === null) {
return null;
}

return (float)$shippingAmount;
}

public function sendPurchaseEvent(
Expand Down

0 comments on commit b51a30d

Please sign in to comment.