| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Classes\Order; |
| 4 |
|
| 5 |
use StoreEngine; |
| 6 |
use StoreEngine\Classes\enums\ProductTaxStatus; |
| 7 |
use StoreEngine\Utils\Formatting; |
| 8 |
|
| 9 |
/** |
| 10 |
* Represents a single shipping rate. |
| 11 |
*/ |
| 12 |
class ShippingRate { |
| 13 |
|
| 14 |
/** |
| 15 |
* Stores data for this rate. |
| 16 |
* |
| 17 |
* @var array |
| 18 |
*/ |
| 19 |
protected array $data = [ |
| 20 |
'id' => '', |
| 21 |
'method_id' => '', |
| 22 |
'instance_id' => 0, |
| 23 |
'label' => '', |
| 24 |
'cost' => 0, |
| 25 |
'taxes' => [], |
| 26 |
'tax_status' => ProductTaxStatus::TAXABLE, |
| 27 |
'description' => '', |
| 28 |
'delivery_time' => '', |
| 29 |
]; |
| 30 |
|
| 31 |
/** |
| 32 |
* Stores meta data for this rate. |
| 33 |
* |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
protected array $meta_data = []; |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor. |
| 40 |
* |
| 41 |
* @param string $id Shipping rate ID. |
| 42 |
* @param string $label Shipping rate label. |
| 43 |
* @param float|int $cost Cost. |
| 44 |
* @param array $taxes Taxes applied to shipping rate. |
| 45 |
* @param string $method_id Shipping method ID. |
| 46 |
* @param int|string $instance_id Shipping instance ID. |
| 47 |
* @param string $tax_status Tax status. |
| 48 |
* @param string $description Shipping rate description. |
| 49 |
* @param string $delivery_time Shipping rate delivery time. |
| 50 |
*/ |
| 51 |
public function __construct( string $id = '', string $label = '', $cost = 0, array $taxes = [], string $method_id = '', $instance_id = 0, string $tax_status = ProductTaxStatus::TAXABLE, string $description = '', string $delivery_time = '' ) { |
| 52 |
$this->set_id( $id ); |
| 53 |
$this->set_label( $label ); |
| 54 |
$this->set_cost( $cost ); |
| 55 |
$this->set_taxes( $taxes ); |
| 56 |
$this->set_method_id( $method_id ); |
| 57 |
$this->set_instance_id( $instance_id ); |
| 58 |
$this->set_tax_status( $tax_status ); |
| 59 |
$this->set_description( $description ); |
| 60 |
$this->set_delivery_time( $delivery_time ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Magic methods to support direct access to props. |
| 65 |
* |
| 66 |
* @param string $key Key. |
| 67 |
* |
| 68 |
* @return bool |
| 69 |
*/ |
| 70 |
public function __isset( string $key ) { |
| 71 |
return isset( $this->data[ $key ] ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Magic methods to support direct access to props. |
| 76 |
* |
| 77 |
* @param string $key Key. |
| 78 |
* |
| 79 |
* @return mixed |
| 80 |
*/ |
| 81 |
public function __get( string $key ) { |
| 82 |
if ( is_callable( [ $this, "get_$key" ] ) ) { |
| 83 |
return $this->{"get_$key"}(); |
| 84 |
} elseif ( isset( $this->data[ $key ] ) ) { |
| 85 |
return $this->data[ $key ]; |
| 86 |
} else { |
| 87 |
return ''; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Magic methods to support direct access to props. |
| 93 |
* |
| 94 |
* @param string $key Key. |
| 95 |
* @param mixed $value Value. |
| 96 |
*/ |
| 97 |
public function __set( string $key, $value ) { |
| 98 |
if ( is_callable( array( $this, "set_$key" ) ) ) { |
| 99 |
$this->{"set_$key"}( $value ); |
| 100 |
} else { |
| 101 |
$this->data[ $key ] = $value; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Set ID for the rate. This is usually a combination of the method and instance IDs. |
| 107 |
* |
| 108 |
* @param string $id Shipping rate ID. |
| 109 |
*/ |
| 110 |
public function set_id( string $id ) { |
| 111 |
$this->data['id'] = $id; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Set shipping method ID the rate belongs to. |
| 116 |
* |
| 117 |
* @param string $method_id Shipping method ID. |
| 118 |
*/ |
| 119 |
public function set_method_id( string $method_id ) { |
| 120 |
$this->data['method_id'] = $method_id; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Set instance ID the rate belongs to. |
| 125 |
* |
| 126 |
* @param int|string $instance_id Instance ID. |
| 127 |
*/ |
| 128 |
public function set_instance_id( $instance_id ) { |
| 129 |
$this->data['instance_id'] = absint( $instance_id ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Set rate label. |
| 134 |
* |
| 135 |
* @param string $label Shipping rate label. |
| 136 |
*/ |
| 137 |
public function set_label( string $label ) { |
| 138 |
$this->data['label'] = $label; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Set rate cost. |
| 143 |
* |
| 144 |
* @param int|float $cost Shipping rate cost. |
| 145 |
*/ |
| 146 |
public function set_cost( $cost ) { |
| 147 |
$this->data['cost'] = abs( floatval( $cost ) ); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Set rate taxes. |
| 152 |
* |
| 153 |
* @param array $taxes List of taxes applied to shipping rate. |
| 154 |
*/ |
| 155 |
public function set_taxes( array $taxes ) { |
| 156 |
$this->data['taxes'] = ! empty( $taxes ) ? $taxes : []; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Set tax status. |
| 161 |
* |
| 162 |
* @param string $value Tax status. |
| 163 |
*/ |
| 164 |
public function set_tax_status( string $value ) { |
| 165 |
if ( in_array( $value, [ ProductTaxStatus::TAXABLE, ProductTaxStatus::NONE ], true ) ) { |
| 166 |
$this->data['tax_status'] = $value; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Set rate description. |
| 172 |
* |
| 173 |
* @param string $description Shipping rate description. |
| 174 |
*/ |
| 175 |
public function set_description( string $description ) { |
| 176 |
$this->data['description'] = $description; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Set rate delivery time. |
| 181 |
* |
| 182 |
* @param string $delivery_time Shipping rate delivery time. |
| 183 |
*/ |
| 184 |
public function set_delivery_time( string $delivery_time ) { |
| 185 |
$this->data['delivery_time'] = $delivery_time; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Get ID for the rate. This is usually a combination of the method and instance IDs. |
| 190 |
* |
| 191 |
* @return string |
| 192 |
*/ |
| 193 |
public function get_id(): string { |
| 194 |
return apply_filters( 'storeengine/shipping_rate_id', $this->data['id'], $this ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get shipping method ID the rate belongs to. |
| 199 |
* |
| 200 |
* @return string |
| 201 |
*/ |
| 202 |
public function get_method_id(): string { |
| 203 |
return apply_filters( 'storeengine/shipping_rate_method_id', $this->data['method_id'], $this ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Get instance ID the rate belongs to. |
| 208 |
* |
| 209 |
* @return int |
| 210 |
*/ |
| 211 |
public function get_instance_id(): int { |
| 212 |
return apply_filters( 'storeengine/shipping_rate_instance_id', $this->data['instance_id'], $this ); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Get rate label. |
| 217 |
* |
| 218 |
* @return string |
| 219 |
*/ |
| 220 |
public function get_label(): string { |
| 221 |
return apply_filters( 'storeengine/shipping_rate_label', $this->data['label'], $this ); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Get rate cost. |
| 226 |
* |
| 227 |
* @return float |
| 228 |
*/ |
| 229 |
public function get_cost(): float { |
| 230 |
return (float) apply_filters( 'storeengine/shipping_rate_cost', $this->data['cost'], $this ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Get rate taxes. |
| 235 |
* |
| 236 |
* @return array |
| 237 |
*/ |
| 238 |
public function get_taxes(): array { |
| 239 |
return apply_filters( 'storeengine/shipping_rate_taxes', $this->data['taxes'], $this ); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Get shipping tax. |
| 244 |
* |
| 245 |
* @return float |
| 246 |
*/ |
| 247 |
public function get_shipping_tax(): float { |
| 248 |
return apply_filters( 'storeengine/get_shipping_tax', count( $this->get_taxes() ) > 0 && ! StoreEngine::init()->customer->get_is_vat_exempt() ? (float) array_sum( $this->get_taxes() ) : 0.0, $this ); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
/** |
| 253 |
* Get tax status. |
| 254 |
* |
| 255 |
* @return string |
| 256 |
*/ |
| 257 |
public function get_tax_status(): string { |
| 258 |
/** |
| 259 |
* Filter to allow tax status to be overridden for a shipping rate. |
| 260 |
* |
| 261 |
* @param string $tax_status Tax status. |
| 262 |
* @param ShippingRate $this Shipping rate object. |
| 263 |
*/ |
| 264 |
return apply_filters( 'storeengine/shipping_rate_tax_status', $this->data['tax_status'], $this ); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Get rate description. |
| 269 |
* |
| 270 |
* @return string |
| 271 |
*/ |
| 272 |
public function get_description(): string { |
| 273 |
/** |
| 274 |
* Filter the shipping rate description. |
| 275 |
* |
| 276 |
* @param string $description The current description. |
| 277 |
* @param ShippingRate $this The shipping rate. |
| 278 |
*/ |
| 279 |
return apply_filters( 'storeengine/shipping_rate_description', $this->data['description'], $this ); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Get rate delivery time. |
| 284 |
* |
| 285 |
* @return string |
| 286 |
*/ |
| 287 |
public function get_delivery_time(): string { |
| 288 |
/** |
| 289 |
* Filter the shipping rate delivery time. |
| 290 |
* |
| 291 |
* @param string $delivery_time The current description. |
| 292 |
* @param ShippingRate $this The shipping rate. |
| 293 |
*/ |
| 294 |
return apply_filters( 'storeengine/shipping_rate_delivery_time', $this->data['delivery_time'], $this ); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Add some meta data for this rate. |
| 299 |
* |
| 300 |
* @param string $key Key. |
| 301 |
* @param mixed $value Value. |
| 302 |
*/ |
| 303 |
public function add_meta_data( string $key, $value ) { |
| 304 |
$this->meta_data[ Formatting::clean( $key ) ] = Formatting::clean( $value ); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Get all meta data for this rate. |
| 309 |
* |
| 310 |
* @return array |
| 311 |
*/ |
| 312 |
public function get_meta_data(): array { |
| 313 |
return $this->meta_data; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
// End of file shipping-rate.php |
| 319 |
|