| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cart Item. |
| 4 |
* |
| 5 |
* @noinspection PhpMissingFieldTypeInspection |
| 6 |
* @todo move to classes/cart |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace StoreEngine\Classes; |
| 10 |
|
| 11 |
use stdClass; |
| 12 |
|
| 13 |
/** |
| 14 |
* CartItem Data representation. |
| 15 |
* |
| 16 |
* Cart item id/key... |
| 17 |
* |
| 18 |
* @property string $key |
| 19 |
* |
| 20 |
* CartItem Core Data... |
| 21 |
* |
| 22 |
* @property string $product_type; |
| 23 |
* @property ?array $bundles; |
| 24 |
* @property int $product_id; |
| 25 |
* @property int $variation_id; |
| 26 |
* @property array $variation; |
| 27 |
* @property int $price_id; |
| 28 |
* @property string $name; |
| 29 |
* @property string $price_name; |
| 30 |
* @property string $price_type; |
| 31 |
* @property float|int $price; |
| 32 |
* @property float|int $compare_price; |
| 33 |
* @property int $quantity; |
| 34 |
* @property float|int $line_subtotal; |
| 35 |
* |
| 36 |
* Price Settings... |
| 37 |
* |
| 38 |
* @property bool $setup_fee; |
| 39 |
* @property string $setup_fee_name; |
| 40 |
* @property float|int $setup_fee_price; |
| 41 |
* @property string $setup_fee_type; |
| 42 |
* @property bool $trial; |
| 43 |
* @property int $trial_days; |
| 44 |
* @property bool $expire; |
| 45 |
* @property int $expire_days; |
| 46 |
* @property int $payment_duration; |
| 47 |
* @property string $payment_duration_type; |
| 48 |
*/ |
| 49 |
#[\AllowDynamicProperties] |
| 50 |
final class CartItem { |
| 51 |
// CartItem Key. |
| 52 |
public ?string $key = null; |
| 53 |
|
| 54 |
// CartItem Core data. |
| 55 |
public ?string $product_type = null; |
| 56 |
|
| 57 |
public ?array $bundles = null; |
| 58 |
|
| 59 |
public ?int $product_id = null; |
| 60 |
|
| 61 |
public ?int $variation_id = null; |
| 62 |
|
| 63 |
public ?array $variation = null; |
| 64 |
|
| 65 |
public ?int $price_id = null; |
| 66 |
|
| 67 |
public ?string $name = null; |
| 68 |
|
| 69 |
public ?string $price_name = null; |
| 70 |
|
| 71 |
public ?string $price_type = null; |
| 72 |
|
| 73 |
/** |
| 74 |
* @var int|float|null |
| 75 |
*/ |
| 76 |
public $price = null; |
| 77 |
|
| 78 |
/** |
| 79 |
* @var int|float|null |
| 80 |
*/ |
| 81 |
public $compare_price = null; |
| 82 |
|
| 83 |
public ?int $quantity = null; |
| 84 |
|
| 85 |
public ?array $item_data = null; |
| 86 |
|
| 87 |
/** |
| 88 |
* @var int|float|null |
| 89 |
*/ |
| 90 |
public $line_subtotal = null; |
| 91 |
|
| 92 |
// Cart total. |
| 93 |
public ?string $tax_class = null; |
| 94 |
|
| 95 |
public ?bool $taxable = null; |
| 96 |
|
| 97 |
/** |
| 98 |
* @var int|float|null |
| 99 |
*/ |
| 100 |
public $line_total = null; |
| 101 |
|
| 102 |
/** |
| 103 |
* @var int|float|null |
| 104 |
*/ |
| 105 |
public $line_subtotal_tax = null; |
| 106 |
|
| 107 |
/** |
| 108 |
* @var int|float|null |
| 109 |
*/ |
| 110 |
public $line_tax = null; |
| 111 |
|
| 112 |
/** |
| 113 |
* @var array|null |
| 114 |
*/ |
| 115 |
public ?array $line_tax_data = [ |
| 116 |
'subtotal' => [], |
| 117 |
'total' => [], |
| 118 |
]; |
| 119 |
|
| 120 |
// Price Settings. |
| 121 |
public bool $setup_fee = false; |
| 122 |
|
| 123 |
public ?string $setup_fee_name = null; |
| 124 |
|
| 125 |
/** |
| 126 |
* @var int|float|null |
| 127 |
*/ |
| 128 |
public $setup_fee_price = null; |
| 129 |
|
| 130 |
public ?string $setup_fee_type = 'fee'; |
| 131 |
|
| 132 |
public bool $trial = false; |
| 133 |
|
| 134 |
public ?int $trial_days = null; |
| 135 |
|
| 136 |
public bool $expire = false; |
| 137 |
|
| 138 |
public ?int $expire_days = null; |
| 139 |
|
| 140 |
public ?int $payment_duration = 0; |
| 141 |
|
| 142 |
public ?string $payment_duration_type = null; |
| 143 |
|
| 144 |
public bool $upgradeable = false; |
| 145 |
|
| 146 |
/** |
| 147 |
* @param ?string $key |
| 148 |
* @param array|stdClass|null $data |
| 149 |
*/ |
| 150 |
public function __construct( ?string $key = null, $data = null ) { |
| 151 |
if ( $key ) { |
| 152 |
$this->key = $key; |
| 153 |
} |
| 154 |
|
| 155 |
if ( $data ) { |
| 156 |
if ( is_object( $data ) ) { |
| 157 |
$data = get_object_vars( $data ); |
| 158 |
} |
| 159 |
|
| 160 |
foreach ( $data as $key => $value ) { |
| 161 |
$this->$key = $value; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* @param array|object $data |
| 168 |
* |
| 169 |
* @return $this |
| 170 |
*/ |
| 171 |
public function set_data( $data ): CartItem { |
| 172 |
if ( is_object( $data ) ) { |
| 173 |
$data = get_object_vars( $data ); |
| 174 |
} |
| 175 |
|
| 176 |
foreach ( $data as $key => $value ) { |
| 177 |
$this->$key = $value; |
| 178 |
} |
| 179 |
|
| 180 |
return $this; |
| 181 |
} |
| 182 |
|
| 183 |
public function __set( string $name, $value ) { |
| 184 |
$this->$name = $value; |
| 185 |
} |
| 186 |
|
| 187 |
public function __get( string $name ) { |
| 188 |
return property_exists( $this, $name ) ? $this->$name : null; |
| 189 |
} |
| 190 |
|
| 191 |
public function get_price() { |
| 192 |
return apply_filters( 'storeengine/cart/raw_item_price', $this->price, $this ); |
| 193 |
} |
| 194 |
|
| 195 |
public function __isset( string $name ) { |
| 196 |
return property_exists( $this, $name ); |
| 197 |
} |
| 198 |
|
| 199 |
public function to_array(): array { |
| 200 |
return get_object_vars( $this ); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
// End of file cart-item.php |
| 205 |
|