Aucune description

StatisticsWooCommercePurchases.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace MailPoet\Models;
  3. if (!defined('ABSPATH')) exit;
  4. use WC_Order;
  5. /**
  6. * @property int $newsletterId
  7. * @property int $subscriberId
  8. * @property int $queueId
  9. * @property int $clickId
  10. * @property int $orderId
  11. * @property string $orderCurrency
  12. * @property float $orderPriceTotal
  13. */
  14. class StatisticsWooCommercePurchases extends Model {
  15. public static $_table = MP_STATISTICS_WOOCOMMERCE_PURCHASES_TABLE; // phpcs:ignore PSR2.Classes.PropertyDeclaration
  16. public static function createOrUpdateByClickDataAndOrder(StatisticsClicks $click, WC_Order $order) {
  17. // search by subscriber and newsletter IDs (instead of click itself) to avoid duplicities
  18. // when a new click from the subscriber appeared since last tracking for given newsletter
  19. // (this will keep the originally tracked click - likely the click that led to the order)
  20. $statistics = self::where('order_id', $order->get_id())
  21. ->where('subscriber_id', $click->subscriberId)
  22. ->where('newsletter_id', $click->newsletterId)
  23. ->findOne();
  24. if (!$statistics instanceof self) {
  25. $statistics = self::create();
  26. $statistics->newsletterId = $click->newsletterId;
  27. $statistics->subscriberId = $click->subscriberId;
  28. $statistics->queueId = $click->queueId;
  29. $statistics->clickId = (int)$click->id;
  30. $statistics->orderId = $order->get_id();
  31. }
  32. $statistics->orderCurrency = $order->get_currency();
  33. $statistics->orderPriceTotal = (float)$order->get_total();
  34. return $statistics->save();
  35. }
  36. }