class-wc-shipping-rate.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Shipping rate class.
  13. */
  14. class WC_Shipping_Rate {
  15. /**
  16. * Stores data for this rate.
  17. *
  18. * @since 3.2.0
  19. * @var array
  20. */
  21. protected $data = array(
  22. 'id' => '',
  23. 'method_id' => '',
  24. 'instance_id' => 0,
  25. 'label' => '',
  26. 'cost' => 0,
  27. 'taxes' => array(),
  28. );
  29. /**
  30. * Stores meta data for this rate.
  31. *
  32. * @since 2.6.0
  33. * @var array
  34. */
  35. protected $meta_data = array();
  36. /**
  37. * Constructor.
  38. *
  39. * @param string $id Shipping rate ID.
  40. * @param string $label Shipping rate label.
  41. * @param integer $cost Cost.
  42. * @param array $taxes Taxes applied to shipping rate.
  43. * @param string $method_id Shipping method ID.
  44. * @param int $instance_id Shipping instance ID.
  45. */
  46. public function __construct( $id = '', $label = '', $cost = 0, $taxes = array(), $method_id = '', $instance_id = 0 ) {
  47. $this->set_id( $id );
  48. $this->set_label( $label );
  49. $this->set_cost( $cost );
  50. $this->set_taxes( $taxes );
  51. $this->set_method_id( $method_id );
  52. $this->set_instance_id( $instance_id );
  53. }
  54. /**
  55. * Magic methods to support direct access to props.
  56. *
  57. * @since 3.2.0
  58. * @param string $key Key.
  59. * @return bool
  60. */
  61. public function __isset( $key ) {
  62. return isset( $this->data[ $key ] );
  63. }
  64. /**
  65. * Magic methods to support direct access to props.
  66. *
  67. * @since 3.2.0
  68. * @param string $key Key.
  69. * @return mixed
  70. */
  71. public function __get( $key ) {
  72. if ( is_callable( array( $this, "get_{$key}" ) ) ) {
  73. return $this->{"get_{$key}"}();
  74. } elseif ( isset( $this->data[ $key ] ) ) {
  75. return $this->data[ $key ];
  76. } else {
  77. return '';
  78. }
  79. }
  80. /**
  81. * Magic methods to support direct access to props.
  82. *
  83. * @since 3.2.0
  84. * @param string $key Key.
  85. * @param mixed $value Value.
  86. */
  87. public function __set( $key, $value ) {
  88. if ( is_callable( array( $this, "set_{$key}" ) ) ) {
  89. $this->{"set_{$key}"}( $value );
  90. } else {
  91. $this->data[ $key ] = $value;
  92. }
  93. }
  94. /**
  95. * Set ID for the rate. This is usually a combination of the method and instance IDs.
  96. *
  97. * @since 3.2.0
  98. * @param string $id Shipping rate ID.
  99. */
  100. public function set_id( $id ) {
  101. $this->data['id'] = (string) $id;
  102. }
  103. /**
  104. * Set shipping method ID the rate belongs to.
  105. *
  106. * @since 3.2.0
  107. * @param string $method_id Shipping method ID.
  108. */
  109. public function set_method_id( $method_id ) {
  110. $this->data['method_id'] = (string) $method_id;
  111. }
  112. /**
  113. * Set instance ID the rate belongs to.
  114. *
  115. * @since 3.2.0
  116. * @param int $instance_id Instance ID.
  117. */
  118. public function set_instance_id( $instance_id ) {
  119. $this->data['instance_id'] = absint( $instance_id );
  120. }
  121. /**
  122. * Set rate label.
  123. *
  124. * @since 3.2.0
  125. * @param string $label Shipping rate label.
  126. */
  127. public function set_label( $label ) {
  128. $this->data['label'] = (string) $label;
  129. }
  130. /**
  131. * Set rate cost.
  132. *
  133. * @todo 4.0 Prevent negative value being set. #19293
  134. * @since 3.2.0
  135. * @param string $cost Shipping rate cost.
  136. */
  137. public function set_cost( $cost ) {
  138. $this->data['cost'] = $cost;
  139. }
  140. /**
  141. * Set rate taxes.
  142. *
  143. * @since 3.2.0
  144. * @param array $taxes List of taxes applied to shipping rate.
  145. */
  146. public function set_taxes( $taxes ) {
  147. $this->data['taxes'] = ! empty( $taxes ) && is_array( $taxes ) ? $taxes : array();
  148. }
  149. /**
  150. * Set ID for the rate. This is usually a combination of the method and instance IDs.
  151. *
  152. * @since 3.2.0
  153. * @return string
  154. */
  155. public function get_id() {
  156. return apply_filters( 'woocommerce_shipping_rate_id', $this->data['id'], $this );
  157. }
  158. /**
  159. * Set shipping method ID the rate belongs to.
  160. *
  161. * @since 3.2.0
  162. * @return string
  163. */
  164. public function get_method_id() {
  165. return apply_filters( 'woocommerce_shipping_rate_method_id', $this->data['method_id'], $this );
  166. }
  167. /**
  168. * Set instance ID the rate belongs to.
  169. *
  170. * @since 3.2.0
  171. * @return int
  172. */
  173. public function get_instance_id() {
  174. return apply_filters( 'woocommerce_shipping_rate_instance_id', $this->data['instance_id'], $this );
  175. }
  176. /**
  177. * Set rate label.
  178. *
  179. * @return string
  180. */
  181. public function get_label() {
  182. return apply_filters( 'woocommerce_shipping_rate_label', $this->data['label'], $this );
  183. }
  184. /**
  185. * Set rate cost.
  186. *
  187. * @since 3.2.0
  188. * @return string
  189. */
  190. public function get_cost() {
  191. return apply_filters( 'woocommerce_shipping_rate_cost', $this->data['cost'], $this );
  192. }
  193. /**
  194. * Set rate taxes.
  195. *
  196. * @since 3.2.0
  197. * @return array
  198. */
  199. public function get_taxes() {
  200. return apply_filters( 'woocommerce_shipping_rate_taxes', $this->data['taxes'], $this );
  201. }
  202. /**
  203. * Get shipping tax.
  204. *
  205. * @return array
  206. */
  207. public function get_shipping_tax() {
  208. return apply_filters( 'woocommerce_get_shipping_tax', count( $this->taxes ) > 0 && ! WC()->customer->get_is_vat_exempt() ? array_sum( $this->taxes ) : 0, $this );
  209. }
  210. /**
  211. * Add some meta data for this rate.
  212. *
  213. * @since 2.6.0
  214. * @param string $key Key.
  215. * @param string $value Value.
  216. */
  217. public function add_meta_data( $key, $value ) {
  218. $this->meta_data[ wc_clean( $key ) ] = wc_clean( $value );
  219. }
  220. /**
  221. * Get all meta data for this rate.
  222. *
  223. * @since 2.6.0
  224. * @return array
  225. */
  226. public function get_meta_data() {
  227. return $this->meta_data;
  228. }
  229. }