Cart.php
8 months ago
CartItem.php
8 months ago
PluginData.php
7 months ago
ReachContact.php
7 months ago
Totals.php
8 months ago
Totals.php
59 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Dto; |
| 4 | |
| 5 | use WC_Order; |
| 6 | |
| 7 | class Totals { |
| 8 | |
| 9 | private float $total; |
| 10 | private float $subtotal; |
| 11 | private float $tax_total; |
| 12 | private float $shipping_total; |
| 13 | private float $discount_total; |
| 14 | private string $currency; |
| 15 | |
| 16 | public function __construct( float $total, float $subtotal, float $tax_total, float $shipping_total, float $discount_total, string $currency ) { |
| 17 | $this->total = $total; |
| 18 | $this->subtotal = $subtotal; |
| 19 | $this->tax_total = $tax_total; |
| 20 | $this->shipping_total = $shipping_total; |
| 21 | $this->discount_total = $discount_total; |
| 22 | $this->currency = $currency; |
| 23 | } |
| 24 | |
| 25 | public function to_array(): array { |
| 26 | return array( |
| 27 | 'total' => $this->total, |
| 28 | 'subtotal' => $this->subtotal, |
| 29 | 'tax_total' => $this->tax_total, |
| 30 | 'shipping_total' => $this->shipping_total, |
| 31 | 'discount_total' => $this->discount_total, |
| 32 | 'currency' => $this->currency, |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | public static function from_order( WC_Order $order ): self { |
| 37 | return new self( |
| 38 | $order->get_total(), |
| 39 | $order->get_subtotal(), |
| 40 | $order->get_total_tax(), |
| 41 | (float) $order->get_shipping_total(), |
| 42 | (float) $order->get_discount_total(), |
| 43 | $order->get_currency() |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | public static function from_cart_totals( array $cart_totals, string $currency ): self { |
| 49 | return new self( |
| 50 | (float) $cart_totals['total'], |
| 51 | (float) $cart_totals['subtotal'], |
| 52 | (float) $cart_totals['total_tax'], |
| 53 | (float) $cart_totals['shipping_total'], |
| 54 | (float) $cart_totals['discount_total'], |
| 55 | $currency |
| 56 | ); |
| 57 | } |
| 58 | } |
| 59 |