abstracts
1 month ago
admin
6 days ago
blocks
11 months ago
cli
1 month ago
customizer
4 months ago
data-stores
1 month ago
emails
5 days ago
export
1 year ago
gateways
1 month ago
import
3 months ago
integrations
2 months ago
interfaces
4 months ago
legacy
4 months ago
libraries
1 month ago
log-handlers
1 year ago
payment-tokens
5 years ago
product-usage
1 year ago
queue
4 months ago
react-admin
1 month ago
rest-api
1 month ago
shipping
3 months ago
shortcodes
1 month ago
theme-support
2 years ago
tracks
1 month ago
traits
5 years ago
walkers
5 years ago
wccom-site
1 month ago
widgets
2 months ago
class-wc-ajax.php
1 month ago
class-wc-auth.php
1 year ago
class-wc-autoloader.php
9 months ago
class-wc-background-emailer.php
2 months ago
class-wc-background-updater.php
5 years ago
class-wc-brands-brand-settings-manager.php
1 year ago
class-wc-brands-coupons.php
1 year ago
class-wc-brands.php
1 month ago
class-wc-breadcrumb.php
4 months ago
class-wc-cache-helper.php
1 month ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
3 months ago
class-wc-cart-totals.php
11 months ago
class-wc-cart.php
1 month ago
class-wc-checkout.php
1 month ago
class-wc-cli.php
10 months ago
class-wc-comments.php
4 months ago
class-wc-countries.php
2 months ago
class-wc-coupon.php
2 months ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
1 year ago
class-wc-customer.php
1 day ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
3 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
2 years ago
class-wc-deprecated-filter-hooks.php
3 months ago
class-wc-discounts.php
11 months ago
class-wc-download-handler.php
1 month ago
class-wc-emails.php
1 month ago
class-wc-embed.php
1 year ago
class-wc-form-handler.php
3 months ago
class-wc-frontend-scripts.php
2 months ago
class-wc-geo-ip.php
9 months ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
2 months ago
class-wc-https.php
3 years ago
class-wc-install.php
6 days ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
2 years ago
class-wc-logger.php
4 months ago
class-wc-meta-data.php
1 month ago
class-wc-order-factory.php
2 months ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
5 months ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
1 month ago
class-wc-order-item-shipping.php
5 months ago
class-wc-order-item-tax.php
1 month ago
class-wc-order-item.php
1 month ago
class-wc-order-query.php
1 month ago
class-wc-order-refund.php
1 year ago
class-wc-order.php
1 month ago
class-wc-payment-gateways.php
1 month ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
2 months ago
class-wc-post-types.php
1 month ago
class-wc-privacy-background-process.php
1 year ago
class-wc-privacy-erasers.php
10 months ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
1 year ago
class-wc-product-attribute.php
4 months ago
class-wc-product-download.php
4 months ago
class-wc-product-external.php
1 year ago
class-wc-product-factory.php
3 months ago
class-wc-product-grouped.php
3 months ago
class-wc-product-query.php
4 months ago
class-wc-product-simple.php
11 months ago
class-wc-product-variable.php
1 month ago
class-wc-product-variation.php
1 month ago
class-wc-query.php
1 month ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
2 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
2 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
3 months ago
class-wc-shipping-rate.php
1 year ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
7 months ago
class-wc-shipping.php
2 months ago
class-wc-shortcodes.php
1 year ago
class-wc-structured-data.php
1 month ago
class-wc-tax.php
2 months ago
class-wc-template-loader.php
7 months ago
class-wc-tracker.php
1 month ago
class-wc-validation.php
2 years ago
class-wc-webhook.php
1 month ago
class-woocommerce.php
1 day ago
wc-account-functions.php
1 month ago
wc-attribute-functions.php
1 month ago
wc-brands-functions.php
1 year ago
wc-cart-functions.php
1 month ago
wc-conditional-functions.php
11 months ago
wc-core-functions.php
2 months ago
wc-coupon-functions.php
5 months ago
wc-deprecated-functions.php
1 month ago
wc-formatting-functions.php
1 month ago
wc-interactivity-api-functions.php
2 months ago
wc-notice-functions.php
5 months ago
wc-order-functions.php
1 month ago
wc-order-item-functions.php
3 years ago
wc-order-step-logger-functions.php
4 months ago
wc-page-functions.php
1 month ago
wc-product-functions.php
2 months ago
wc-rest-functions.php
7 months ago
wc-stock-functions.php
7 months ago
wc-template-functions.php
1 month ago
wc-template-hooks.php
10 months ago
wc-term-functions.php
2 months ago
wc-update-functions.php
6 days ago
wc-user-functions.php
1 month ago
wc-webhook-functions.php
1 month ago
wc-widget-functions.php
5 years ago
class-wc-shipping-rate.php
413 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Shipping Rate |
| 4 | * |
| 5 | * Simple Class for storing rates. |
| 6 | * |
| 7 | * @package WooCommerce\Classes\Shipping |
| 8 | * @since 2.6.0 |
| 9 | */ |
| 10 | |
| 11 | declare( strict_types=1 ); |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | use Automattic\WooCommerce\Enums\ProductTaxStatus; |
| 16 | |
| 17 | /** |
| 18 | * Shipping rate class. |
| 19 | */ |
| 20 | class WC_Shipping_Rate implements JsonSerializable { |
| 21 | |
| 22 | /** |
| 23 | * Stores data for this rate. |
| 24 | * |
| 25 | * @since 9.2.0 Added description and delivery_time. |
| 26 | * @var array |
| 27 | */ |
| 28 | protected $data = array( |
| 29 | 'id' => '', |
| 30 | 'method_id' => '', |
| 31 | 'instance_id' => 0, |
| 32 | 'label' => '', |
| 33 | 'cost' => 0, |
| 34 | 'taxes' => array(), |
| 35 | 'tax_status' => ProductTaxStatus::TAXABLE, |
| 36 | 'description' => '', |
| 37 | 'delivery_time' => '', |
| 38 | ); |
| 39 | |
| 40 | /** |
| 41 | * Stores meta data for this rate. |
| 42 | * |
| 43 | * @var array |
| 44 | */ |
| 45 | protected $meta_data = array(); |
| 46 | |
| 47 | /** |
| 48 | * Constructor. |
| 49 | * |
| 50 | * @param string $id Shipping rate ID. |
| 51 | * @param string $label Shipping rate label. |
| 52 | * @param integer $cost Cost. |
| 53 | * @param array $taxes Taxes applied to shipping rate. |
| 54 | * @param string $method_id Shipping method ID. |
| 55 | * @param int $instance_id Shipping instance ID. |
| 56 | * @param string $tax_status Tax status. |
| 57 | * @param string $description Shipping rate description. |
| 58 | * @param string $delivery_time Shipping rate delivery time. |
| 59 | */ |
| 60 | public function __construct( $id = '', $label = '', $cost = 0, $taxes = array(), $method_id = '', $instance_id = 0, $tax_status = ProductTaxStatus::TAXABLE, $description = '', $delivery_time = '' ) { |
| 61 | $this->set_id( $id ); |
| 62 | $this->set_label( $label ); |
| 63 | $this->set_cost( $cost ); |
| 64 | $this->set_taxes( $taxes ); |
| 65 | $this->set_method_id( $method_id ); |
| 66 | $this->set_instance_id( $instance_id ); |
| 67 | $this->set_tax_status( $tax_status ); |
| 68 | $this->set_description( $description ); |
| 69 | $this->set_delivery_time( $delivery_time ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Magic method to support direct access to data prop. |
| 74 | * |
| 75 | * @param string $key Key. |
| 76 | * @return bool |
| 77 | */ |
| 78 | public function __isset( $key ) { |
| 79 | return isset( $this->data[ $key ] ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Magic methods to support direct access to props. |
| 84 | * |
| 85 | * @param string $key Key. |
| 86 | * @return mixed |
| 87 | */ |
| 88 | public function __get( $key ) { |
| 89 | if ( is_callable( array( $this, "get_{$key}" ) ) ) { |
| 90 | return $this->{"get_{$key}"}(); |
| 91 | } |
| 92 | |
| 93 | if ( isset( $this->data[ $key ] ) ) { |
| 94 | return $this->data[ $key ]; |
| 95 | } |
| 96 | |
| 97 | return ''; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Magic methods to support direct access to props. |
| 102 | * |
| 103 | * @since 3.2.0 |
| 104 | * @param string $key Key. |
| 105 | * @param mixed $value Value. |
| 106 | */ |
| 107 | public function __set( $key, $value ) { |
| 108 | if ( is_callable( array( $this, "set_{$key}" ) ) ) { |
| 109 | $this->{"set_{$key}"}( $value ); |
| 110 | } else { |
| 111 | $this->data[ $key ] = $value; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * When converted to JSON. |
| 117 | * |
| 118 | * @return object|array |
| 119 | */ |
| 120 | #[\ReturnTypeWillChange] |
| 121 | public function jsonSerialize() { |
| 122 | return array( |
| 123 | 'data' => $this->data, |
| 124 | 'meta_data' => $this->meta_data, |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Set ID for the rate. This is usually a combination of the method and instance IDs. |
| 130 | * |
| 131 | * @since 3.2.0 |
| 132 | * @param string $id Shipping rate ID. |
| 133 | */ |
| 134 | public function set_id( $id ) { |
| 135 | $this->data['id'] = (string) $id; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Set shipping method ID the rate belongs to. |
| 140 | * |
| 141 | * @since 3.2.0 |
| 142 | * @param string $method_id Shipping method ID. |
| 143 | */ |
| 144 | public function set_method_id( $method_id ) { |
| 145 | $this->data['method_id'] = (string) $method_id; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Set instance ID the rate belongs to. |
| 150 | * |
| 151 | * @since 3.2.0 |
| 152 | * @param int $instance_id Instance ID. |
| 153 | */ |
| 154 | public function set_instance_id( $instance_id ) { |
| 155 | $this->data['instance_id'] = absint( $instance_id ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Set rate label. |
| 160 | * |
| 161 | * @since 3.2.0 |
| 162 | * @param string $label Shipping rate label. |
| 163 | */ |
| 164 | public function set_label( $label ) { |
| 165 | $this->data['label'] = (string) $label; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Set rate cost. |
| 170 | * |
| 171 | * @todo 4.0 Prevent negative value being set. #19293 |
| 172 | * @since 3.2.0 |
| 173 | * @param string $cost Shipping rate cost. |
| 174 | */ |
| 175 | public function set_cost( $cost ) { |
| 176 | $this->data['cost'] = $cost; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Set rate taxes. |
| 181 | * |
| 182 | * @since 3.2.0 |
| 183 | * @param array $taxes List of taxes applied to shipping rate. |
| 184 | */ |
| 185 | public function set_taxes( $taxes ) { |
| 186 | $this->data['taxes'] = ! empty( $taxes ) && is_array( $taxes ) ? $taxes : array(); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Set tax status. |
| 191 | * |
| 192 | * @since 9.6.0 |
| 193 | * @param string $value Tax status. |
| 194 | */ |
| 195 | public function set_tax_status( $value ) { |
| 196 | if ( in_array( $value, array( ProductTaxStatus::TAXABLE, ProductTaxStatus::NONE ), true ) ) { |
| 197 | $this->data['tax_status'] = $value; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Set rate description. |
| 203 | * |
| 204 | * @since 9.2.0 |
| 205 | * @param string $description Shipping rate description. |
| 206 | */ |
| 207 | public function set_description( $description ) { |
| 208 | $this->data['description'] = (string) $description; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Set rate delivery time. |
| 213 | * |
| 214 | * @since 9.2.0 |
| 215 | * @param string $delivery_time Shipping rate delivery time. |
| 216 | */ |
| 217 | public function set_delivery_time( $delivery_time ) { |
| 218 | $this->data['delivery_time'] = (string) $delivery_time; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get ID for the rate. This is usually a combination of the method and instance IDs. |
| 223 | * |
| 224 | * @since 3.2.0 |
| 225 | * @return string |
| 226 | */ |
| 227 | public function get_id() { |
| 228 | /** |
| 229 | * Filter the shipping rate ID. |
| 230 | * |
| 231 | * @since 3.2.0 |
| 232 | * @param string $id The shipping rate ID. |
| 233 | * @param WC_Shipping_Rate $this The shipping rate object. |
| 234 | */ |
| 235 | return apply_filters( 'woocommerce_shipping_rate_id', $this->data['id'], $this ); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Get shipping method ID the rate belongs to. |
| 240 | * |
| 241 | * @since 3.2.0 |
| 242 | * @return string |
| 243 | */ |
| 244 | public function get_method_id() { |
| 245 | /** |
| 246 | * Filter the shipping method ID. |
| 247 | * |
| 248 | * @since 3.2.0 |
| 249 | * @param string $method_id The shipping method ID. |
| 250 | * @param WC_Shipping_Rate $this The shipping rate object. |
| 251 | */ |
| 252 | return apply_filters( 'woocommerce_shipping_rate_method_id', $this->data['method_id'], $this ); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Get instance ID the rate belongs to. |
| 257 | * |
| 258 | * @since 3.2.0 |
| 259 | * @return int |
| 260 | */ |
| 261 | public function get_instance_id() { |
| 262 | /** |
| 263 | * Filter the shipping rate instance ID. |
| 264 | * |
| 265 | * @since 3.2.0 |
| 266 | * @param int $instance_id The shipping rate instance ID. |
| 267 | * @param WC_Shipping_Rate $this The shipping rate object. |
| 268 | */ |
| 269 | return apply_filters( 'woocommerce_shipping_rate_instance_id', $this->data['instance_id'], $this ); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Get rate label. |
| 274 | * |
| 275 | * @return string |
| 276 | */ |
| 277 | public function get_label() { |
| 278 | /** |
| 279 | * Filter the shipping rate label. |
| 280 | * |
| 281 | * @since 3.2.0 |
| 282 | * @param string $label The shipping rate label. |
| 283 | * @param WC_Shipping_Rate $this The shipping rate object. |
| 284 | */ |
| 285 | return apply_filters( 'woocommerce_shipping_rate_label', $this->data['label'], $this ); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Get rate cost. |
| 290 | * |
| 291 | * @since 3.2.0 |
| 292 | * @return string |
| 293 | */ |
| 294 | public function get_cost() { |
| 295 | /** |
| 296 | * Filter the shipping rate cost. |
| 297 | * |
| 298 | * @since 3.2.0 |
| 299 | * @param string $cost The shipping rate cost. |
| 300 | * @param WC_Shipping_Rate $this The shipping rate object. |
| 301 | */ |
| 302 | return apply_filters( 'woocommerce_shipping_rate_cost', $this->data['cost'], $this ); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Get rate taxes. |
| 307 | * |
| 308 | * @since 3.2.0 |
| 309 | * @return array |
| 310 | */ |
| 311 | public function get_taxes() { |
| 312 | /** |
| 313 | * Filter the shipping rate taxes. |
| 314 | * |
| 315 | * @since 3.2.0 |
| 316 | * @param array $taxes The shipping rate taxes. |
| 317 | * @param WC_Shipping_Rate $this The shipping rate object. |
| 318 | */ |
| 319 | return apply_filters( 'woocommerce_shipping_rate_taxes', $this->data['taxes'], $this ); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Get shipping tax. |
| 324 | * |
| 325 | * @return float |
| 326 | */ |
| 327 | public function get_shipping_tax() { |
| 328 | $taxes = $this->get_taxes(); |
| 329 | |
| 330 | /** |
| 331 | * Filter the shipping rate taxes. |
| 332 | * |
| 333 | * @since 3.2.0 |
| 334 | * @param array $taxes The shipping rate taxes. |
| 335 | * @param WC_Shipping_Rate $this The shipping rate object. |
| 336 | */ |
| 337 | return apply_filters( 'woocommerce_get_shipping_tax', count( $taxes ) > 0 && ! WC()->customer->get_is_vat_exempt() ? (float) array_sum( $taxes ) : 0.0, $this ); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Get tax status. |
| 342 | * |
| 343 | * @return string |
| 344 | */ |
| 345 | public function get_tax_status() { |
| 346 | /** |
| 347 | * Filter to allow tax status to be overridden for a shipping rate. |
| 348 | * |
| 349 | * @since 9.9.0 |
| 350 | * @param string $tax_status Tax status. |
| 351 | * @param WC_Shipping_Rate $this Shipping rate object. |
| 352 | */ |
| 353 | return apply_filters( 'woocommerce_shipping_rate_tax_status', $this->data['tax_status'], $this ); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Get rate description. |
| 358 | * |
| 359 | * @since 9.2.0 |
| 360 | * @return string |
| 361 | */ |
| 362 | public function get_description() { |
| 363 | /** |
| 364 | * Filter the shipping rate description. |
| 365 | * |
| 366 | * @since 9.2.0 |
| 367 | * |
| 368 | * @param string $description The current description. |
| 369 | * @param WC_Shipping_Rate $this The shipping rate. |
| 370 | */ |
| 371 | return apply_filters( 'woocommerce_shipping_rate_description', $this->data['description'], $this ); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Get rate delivery time. |
| 376 | * |
| 377 | * @since 9.2.0 |
| 378 | * @return string |
| 379 | */ |
| 380 | public function get_delivery_time() { |
| 381 | /** |
| 382 | * Filter the shipping rate delivery time. |
| 383 | * |
| 384 | * @since 9.2.0 |
| 385 | * |
| 386 | * @param string $delivery_time The current description. |
| 387 | * @param WC_Shipping_Rate $this The shipping rate. |
| 388 | */ |
| 389 | return apply_filters( 'woocommerce_shipping_rate_delivery_time', $this->data['delivery_time'], $this ); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Add some meta data for this rate. |
| 394 | * |
| 395 | * @since 2.6.0 |
| 396 | * @param string $key Key. |
| 397 | * @param string $value Value. |
| 398 | */ |
| 399 | public function add_meta_data( $key, $value ) { |
| 400 | $this->meta_data[ wc_clean( $key ) ] = wc_clean( $value ); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Get all meta data for this rate. |
| 405 | * |
| 406 | * @since 2.6.0 |
| 407 | * @return array |
| 408 | */ |
| 409 | public function get_meta_data() { |
| 410 | return $this->meta_data; |
| 411 | } |
| 412 | } |
| 413 |