Bump.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models; |
| 4 | |
| 5 | use SureCart\Models\Traits\HasDates; |
| 6 | use SureCart\Models\Traits\HasPrice; |
| 7 | use SureCart\Support\Currency; |
| 8 | |
| 9 | /** |
| 10 | * Holds the data of the order bump. |
| 11 | */ |
| 12 | class Bump extends Model { |
| 13 | use HasDates; |
| 14 | use HasPrice; |
| 15 | |
| 16 | /** |
| 17 | * Rest API endpoint |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $endpoint = 'bumps'; |
| 22 | |
| 23 | /** |
| 24 | * Object name |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | protected $object_name = 'bump'; |
| 29 | |
| 30 | /** |
| 31 | * Get the amount off display amount attribute |
| 32 | * |
| 33 | * @return string |
| 34 | */ |
| 35 | protected function getAmountOffDisplayAmountAttribute() { |
| 36 | return Currency::format( $this->amount_off, $this->currency ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get the amount attribute |
| 41 | * |
| 42 | * @return string|null |
| 43 | */ |
| 44 | protected function getSubtotalDisplayAmountAttribute() { |
| 45 | return $this->price->display_amount ?? null; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the amount attribute |
| 50 | * |
| 51 | * @return int|null |
| 52 | */ |
| 53 | protected function getSubtotalAmountAttribute() { |
| 54 | return $this->price->amount ?? null; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get the scratch amount attribute |
| 59 | * |
| 60 | * @return int |
| 61 | */ |
| 62 | protected function getTotalAmountAttribute() { |
| 63 | $initial_amount = $this->price->amount ?? 0; |
| 64 | |
| 65 | if ( ! empty( $this->amount_off ) ) { |
| 66 | return max( 0, $initial_amount - $this->amount_off ); |
| 67 | } |
| 68 | |
| 69 | if ( ! empty( $this->percent_off ) ) { |
| 70 | $off = $initial_amount * ( $this->percent_off / 100 ); |
| 71 | return max( 0, $initial_amount - $off ); |
| 72 | } |
| 73 | |
| 74 | return $initial_amount; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the display amount attribute |
| 79 | * |
| 80 | * @return string |
| 81 | */ |
| 82 | protected function getTotalDisplayAmountAttribute() { |
| 83 | return Currency::format( $this->total_amount, $this->currency ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get the rendered bump description attribute. |
| 88 | * |
| 89 | * @return string |
| 90 | */ |
| 91 | public function getRenderedDescriptionAttribute() { |
| 92 | return ! empty( $this->metadata->description ) ? wp_kses_post( wpautop( $this->metadata->description ) ) : ''; |
| 93 | } |
| 94 | } |
| 95 |