views
11 months ago
class-sst-cart-proxy.php
1 year ago
class-sst-checkout.php
1 month ago
class-sst-my-account.php
2 years ago
class-sst-cart-proxy.php
218 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * SST Cart Proxy. |
| 5 | * |
| 6 | * Provides a simple, backward-compatible interface to the WC Cart object. |
| 7 | * |
| 8 | * @author Brett Porcelli |
| 9 | * @package SST |
| 10 | * @since 5.6 |
| 11 | */ |
| 12 | class SST_Cart_Proxy { |
| 13 | |
| 14 | /** |
| 15 | * The WC_Cart object being wrapped. |
| 16 | * |
| 17 | * @var WC_Cart |
| 18 | */ |
| 19 | private $cart = null; |
| 20 | |
| 21 | /** |
| 22 | * Constructor. |
| 23 | * |
| 24 | * @param WC_Cart $cart WC_Cart object to wrap. |
| 25 | * |
| 26 | * @since 5.6 |
| 27 | */ |
| 28 | public function __construct( $cart ) { |
| 29 | $this->cart = $cart; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Get cart taxes. |
| 34 | * |
| 35 | * @return array of cart taxes. |
| 36 | * @since 5.6 |
| 37 | */ |
| 38 | public function get_cart_taxes() { |
| 39 | return wc_array_merge_recursive_numeric( |
| 40 | $this->cart->get_cart_contents_taxes(), |
| 41 | $this->cart->get_fee_taxes() |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get shipping taxes. |
| 47 | * |
| 48 | * @return array of shipping taxes. |
| 49 | * @since 5.6 |
| 50 | */ |
| 51 | public function get_shipping_taxes() { |
| 52 | return $this->cart->get_shipping_taxes(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Set cart tax amount. |
| 57 | * |
| 58 | * @param string $value Value to set. |
| 59 | * |
| 60 | * @since 5.6 |
| 61 | */ |
| 62 | public function set_cart_tax( $value ) { |
| 63 | $this->cart->set_cart_contents_tax( $value ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Set shipping tax. |
| 68 | * |
| 69 | * @param string $value Value to set. |
| 70 | * |
| 71 | * @since 3.2.0 |
| 72 | */ |
| 73 | public function set_shipping_tax( $value ) { |
| 74 | $this->cart->set_shipping_tax( $value ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Set the tax for a particular cart item. |
| 79 | * |
| 80 | * @param mixed $key Cart item key. |
| 81 | * @param float $tax Sales tax for cart item. |
| 82 | * |
| 83 | * @since 5.0 |
| 84 | */ |
| 85 | public function set_cart_item_tax( $key, $tax ) { |
| 86 | $cart_contents = $this->cart->get_cart_contents(); |
| 87 | $tax_data = $cart_contents[ $key ]['line_tax_data']; |
| 88 | |
| 89 | $tax_data['subtotal'][ SST_RATE_ID ] = $tax; |
| 90 | $tax_data['total'][ SST_RATE_ID ] = $tax; |
| 91 | |
| 92 | $cart_contents[ $key ]['line_tax_data'] = $tax_data; |
| 93 | $cart_contents[ $key ]['line_subtotal_tax'] = array_sum( $tax_data['subtotal'] ); |
| 94 | $cart_contents[ $key ]['line_tax'] = array_sum( $tax_data['total'] ); |
| 95 | |
| 96 | $this->cart->set_cart_contents( $cart_contents ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Set the tax for a particular fee. |
| 101 | * |
| 102 | * @param mixed $id Fee ID. |
| 103 | * @param float $tax Sales tax for fee. |
| 104 | * |
| 105 | * @since 5.0 |
| 106 | */ |
| 107 | public function set_fee_item_tax( $id, $tax ) { |
| 108 | $fees = $this->cart->fees_api()->get_fees(); |
| 109 | |
| 110 | $fees[ $id ]->tax_data[ SST_RATE_ID ] = $tax; |
| 111 | $fees[ $id ]->tax = array_sum( $this->cart->fees[ $id ]->tax_data ); |
| 112 | |
| 113 | $this->cart->fees_api()->set_fees( $fees ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Set the tax for a shipping package. |
| 118 | * |
| 119 | * @param mixed $key Package key. |
| 120 | * @param float $tax Sales tax for package. |
| 121 | * |
| 122 | * @since 5.6 |
| 123 | */ |
| 124 | public function set_package_tax( $key, $tax ) { |
| 125 | $this->cart->sst_shipping_taxes[ $key ] = $tax; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Set the tax amount for a given tax rate. |
| 130 | * |
| 131 | * @param string $tax_rate_id ID of the tax rate to set taxes for. |
| 132 | * @param float $amount Tax amount for tax rate. |
| 133 | * |
| 134 | * @since 5.6 |
| 135 | */ |
| 136 | public function set_tax_amount( $tax_rate_id, $amount ) { |
| 137 | $taxes = $this->cart-> get_cart_contents_taxes(); |
| 138 | $taxes[ $tax_rate_id ] = $amount; |
| 139 | $this->cart->set_cart_contents_taxes( $taxes ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Set the shipping tax amount for a given tax rate. |
| 144 | * |
| 145 | * @param string $tax_rate_id ID of the tax rate to set shipping taxes for. |
| 146 | * @param float $amount Tax amount for tax rate. |
| 147 | * |
| 148 | * @since 5.6 |
| 149 | */ |
| 150 | public function set_shipping_tax_amount( $tax_rate_id, $amount ) { |
| 151 | $taxes = $this->cart->get_shipping_taxes(); |
| 152 | $taxes[ $tax_rate_id ] = $amount; |
| 153 | $this->cart->set_shipping_taxes( $taxes ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Update tax totals based on tax arrays. |
| 158 | * |
| 159 | * @since 5.6 |
| 160 | */ |
| 161 | public function update_tax_totals() { |
| 162 | $this->set_cart_tax( WC_Tax::get_tax_total( $this->get_cart_taxes() ) ); |
| 163 | $this->set_shipping_tax( WC_Tax::get_tax_total( $this->get_shipping_taxes() ) ); |
| 164 | |
| 165 | $cart_total_tax = wc_round_tax_total( |
| 166 | $this->get_cart_contents_tax() + $this->get_shipping_tax() + $this->get_fee_tax() |
| 167 | ); |
| 168 | $this->cart->set_total_tax( $cart_total_tax ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Reset package shipping taxes before running calculations. |
| 173 | * |
| 174 | * @since 5.6 |
| 175 | */ |
| 176 | public function reset_shipping_taxes() { |
| 177 | $this->cart->sst_shipping_taxes = array(); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Forward calls to inaccessible methods to the underlying cart object. |
| 182 | * |
| 183 | * @param string $name Name of method being called. |
| 184 | * @param array $args Parameters of method. |
| 185 | * |
| 186 | * @return mixed |
| 187 | * @since 5.6 |
| 188 | */ |
| 189 | public function __call( $name, $args ) { |
| 190 | return call_user_func_array( array( $this->cart, $name ), $args ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Forward read requests for inaccessible properties to the underlying cart object. |
| 195 | * |
| 196 | * @param string $name Name of property being read. |
| 197 | * |
| 198 | * @return mixed |
| 199 | * @since 5.6 |
| 200 | */ |
| 201 | public function __get( $name ) { |
| 202 | return $this->cart->$name; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Forward write requests for inaccessible properties to the underlying cart object. |
| 207 | * |
| 208 | * @param string $name Name of property being written to. |
| 209 | * @param mixed $value Value being written. |
| 210 | * |
| 211 | * @since 5.6 |
| 212 | */ |
| 213 | public function __set( $name, $value ) { |
| 214 | $this->cart->$name = $value; |
| 215 | } |
| 216 | |
| 217 | } |
| 218 |