WC_Legacy_Cart

Legacy cart class.


Description Description


Source Source

File: includes/legacy/class-wc-legacy-cart.php

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
abstract class WC_Legacy_Cart {
 
    /**
     * Array of defaults. Not used since 3.2.
     *
     * @deprecated 3.2.0
     */
    public $cart_session_data = array(
        'cart_contents_total'         => 0,
        'total'                       => 0,
        'subtotal'                    => 0,
        'subtotal_ex_tax'             => 0,
        'tax_total'                   => 0,
        'taxes'                       => array(),
        'shipping_taxes'              => array(),
        'discount_cart'               => 0,
        'discount_cart_tax'           => 0,
        'shipping_total'              => 0,
        'shipping_tax_total'          => 0,
        'coupon_discount_amounts'     => array(),
        'coupon_discount_tax_amounts' => array(),
        'fee_total'                   => 0,
        'fees'                        => array(),
    );
 
    /**
     * Contains an array of coupon usage counts after they have been applied.
     *
     * @deprecated 3.2.0
     * @var array
     */
    public $coupon_applied_count = array();
 
    /**
     * Map legacy variables.
     *
     * @param string $name Property name.
     * @param mixed  $value Value to set.
     */
    public function __isset( $name ) {
        if ( array_key_exists( $name, $this->cart_session_data ) || 'fees' === $name ) {
            return true;
        }
        return false;
    }
 
    /**
     * Magic getters.
     *
     * @param string $name Property name.
     * @return mixed
     */
    public function &__get( $name ) {
        $value = '';
 
        switch ( $name ) {
            case 'dp' :
                $value = wc_get_price_decimals();
                break;
            case 'prices_include_tax' :
                $value = wc_prices_include_tax();
                break;
            case 'round_at_subtotal' :
                $value = 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' );
                break;
            case 'cart_contents_total' :
                $value = $this->get_cart_contents_total();
                break;
            case 'total' :
                $value = $this->get_total( 'edit' );
                break;
            case 'subtotal' :
                $value = $this->get_subtotal() + $this->get_subtotal_tax();
                break;
            case 'subtotal_ex_tax' :
                $value = $this->get_subtotal();
                break;
            case 'tax_total' :
                $value = $this->get_fee_tax() + $this->get_cart_contents_tax();
                break;
            case 'fee_total' :
                $value = $this->get_fee_total();
                break;
            case 'discount_cart' :
                $value = $this->get_discount_total();
                break;
            case 'discount_cart_tax' :
                $value = $this->get_discount_tax();
                break;
            case 'shipping_total' :
                $value = $this->get_shipping_total();
                break;
            case 'shipping_tax_total' :
                $value = $this->get_shipping_tax();
                break;
            case 'display_totals_ex_tax' :
            case 'display_cart_ex_tax' :
                $value = ! $this->display_prices_including_tax();
                break;
            case 'cart_contents_weight' :
                $value = $this->get_cart_contents_weight();
                break;
            case 'cart_contents_count' :
                $value = $this->get_cart_contents_count();
                break;
            case 'coupons' :
                $value = $this->get_coupons();
                break;
 
            // Arrays returned by reference to allow modification without notices. TODO: Remove in 4.0.
            case 'taxes' :
                wc_deprecated_function( 'WC_Cart->taxes', '3.2', sprintf( 'getters (%s) and setters (%s)', 'WC_Cart::get_cart_contents_taxes()', 'WC_Cart::set_cart_contents_taxes()' ) );
                $value = &$this->totals[ 'cart_contents_taxes' ];
                break;
            case 'shipping_taxes' :
                wc_deprecated_function( 'WC_Cart->shipping_taxes', '3.2', sprintf( 'getters (%s) and setters (%s)', 'WC_Cart::get_shipping_taxes()', 'WC_Cart::set_shipping_taxes()' ) );
                $value = &$this->totals[ 'shipping_taxes' ];
                break;
            case 'coupon_discount_amounts' :
                $value = &$this->coupon_discount_totals;
                break;
            case 'coupon_discount_tax_amounts' :
                $value = &$this->coupon_discount_tax_totals;
                break;
            case 'fees' :
                wc_deprecated_function( 'WC_Cart->fees', '3.2', sprintf( 'the fees API (%s)', 'WC_Cart::get_fees' ) );
 
                // Grab fees from the new API.
                $new_fees   = $this->fees_api()->get_fees();
 
                // Add new fees to the legacy prop so it can be adjusted via legacy property.
                $this->fees = $new_fees;
 
                // Return by reference.
                $value = &$this->fees;
                break;
            // Deprecated args. TODO: Remove in 4.0.
            case 'tax' :
                wc_deprecated_argument( 'WC_Cart->tax', '2.3', 'Use WC_Tax directly' );
                $this->tax = new WC_Tax();
                $value = $this->tax;
                break;
            case 'discount_total':
                wc_deprecated_argument( 'WC_Cart->discount_total', '2.3', 'After tax coupons are no longer supported. For more information see: https://woocommerce.wordpress.com/2014/12/upcoming-coupon-changes-in-woocommerce-2-3/' );
                $value = 0;
                break;
        }
        return $value;
    }
 
    /**
     * Map legacy variables to setters.
     *
     * @param string $name Property name.
     * @param mixed  $value Value to set.
     */
    public function __set( $name, $value ) {
        switch ( $name ) {
            case 'cart_contents_total' :
                $this->set_cart_contents_total( $value );
                break;
            case 'total' :
                $this->set_total( $value );
                break;
            case 'subtotal' :
                $this->set_subtotal( $value );
                break;
            case 'subtotal_ex_tax' :
                $this->set_subtotal( $value );
                break;
            case 'tax_total' :
                $this->set_cart_contents_tax( $value );
                $this->set_fee_tax( 0 );
                break;
            case 'taxes' :
                $this->set_cart_contents_taxes( $value );
                break;
            case 'shipping_taxes' :
                $this->set_shipping_taxes( $value );
                break;
            case 'fee_total' :
                $this->set_fee_total( $value );
                break;
            case 'discount_cart' :
                $this->set_discount_total( $value );
                break;
            case 'discount_cart_tax' :
                $this->set_discount_tax( $value );
                break;
            case 'shipping_total' :
                $this->set_shipping_total( $value );
                break;
            case 'shipping_tax_total' :
                $this->set_shipping_tax( $value );
                break;
            case 'coupon_discount_amounts' :
                $this->set_coupon_discount_totals( $value );
                break;
            case 'coupon_discount_tax_amounts' :
                $this->set_coupon_discount_tax_totals( $value );
                break;
            case 'fees' :
                wc_deprecated_function( 'WC_Cart->fees', '3.2', sprintf( 'the fees API (%s)', 'WC_Cart::add_fee' ) );
                $this->fees = $value;
                break;
            default :
                $this->$name = $value;
                break;
        }
    }
 
    /**
     * Methods moved to session class in 3.2.0.
     */
    public function get_cart_from_session() { $this->session->get_cart_from_session(); }
    public function maybe_set_cart_cookies() { $this->session->maybe_set_cart_cookies(); }
    public function set_session() { $this->session->set_session(); }
    public function get_cart_for_session() { return $this->session->get_cart_for_session(); }
    public function persistent_cart_update() { $this->session->persistent_cart_update(); }
    public function persistent_cart_destroy() { $this->session->persistent_cart_destroy(); }
 
    /**
     * Get the total of all cart discounts.
     *
     * @return float
     */
    public function get_cart_discount_total() {
        return $this->get_discount_total();
    }
 
    /**
     * Get the total of all cart tax discounts (used for discounts on tax inclusive prices).
     *
     * @return float
     */
    public function get_cart_discount_tax_total() {
        return $this->get_discount_tax();
    }
 
    /**
     * Renamed for consistency.
     *
     * @param string $coupon_code
     * @return bool True if the coupon is applied, false if it does not exist or cannot be applied.
     */
    public function add_discount( $coupon_code ) {
        return $this->apply_coupon( $coupon_code );
    }
    /**
     * Remove taxes.
     *
     * @deprecated 3.2.0 Taxes are never calculated if customer is tax except making this function unused.
     */
    public function remove_taxes() {
        wc_deprecated_function( 'WC_Cart::remove_taxes', '3.2', '' );
    }
    /**
     * Init.
     *
     * @deprecated 3.2.0 Session is loaded via hooks rather than directly.
     */
    public function init() {
        wc_deprecated_function( 'WC_Cart::init', '3.2', '' );
        $this->get_cart_from_session();
    }
 
    /**
     * Function to apply discounts to a product and get the discounted price (before tax is applied).
     *
     * @deprecated Calculation and coupon logic is handled in WC_Cart_Totals.
     * @param mixed $values Cart item.
     * @param mixed $price Price of item.
     * @param bool  $add_totals Legacy.
     * @return float price
     */
    public function get_discounted_price( $values, $price, $add_totals = false ) {
        wc_deprecated_function( 'WC_Cart::get_discounted_price', '3.2', '' );
 
        $cart_item_key = $values['key'];
        $cart_item     = $this->cart_contents[ $cart_item_key ];
 
        return $cart_item['line_total'];
    }
 
    /**
     * Gets the url to the cart page.
     *
     * @deprecated 2.5.0 in favor to wc_get_cart_url()
     * @return string url to page
     */
    public function get_cart_url() {
        wc_deprecated_function( 'WC_Cart::get_cart_url', '2.5', 'wc_get_cart_url' );
        return wc_get_cart_url();
    }
 
    /**
     * Gets the url to the checkout page.
     *
     * @deprecated 2.5.0 in favor to wc_get_checkout_url()
     * @return string url to page
     */
    public function get_checkout_url() {
        wc_deprecated_function( 'WC_Cart::get_checkout_url', '2.5', 'wc_get_checkout_url' );
        return wc_get_checkout_url();
    }
 
    /**
     * Sees if we need a shipping address.
     *
     * @deprecated 2.5.0 in favor to wc_ship_to_billing_address_only()
     * @return bool
     */
    public function ship_to_billing_address_only() {
        wc_deprecated_function( 'WC_Cart::ship_to_billing_address_only', '2.5', 'wc_ship_to_billing_address_only' );
        return wc_ship_to_billing_address_only();
    }
 
    /**
     * Coupons enabled function. Filterable.
     *
     * @deprecated 2.5.0 in favor to wc_coupons_enabled()
     * @return bool
     */
    public function coupons_enabled() {
        return wc_coupons_enabled();
    }
 
    /**
     * Gets the total (product) discount amount - these are applied before tax.
     *
     * @deprecated Order discounts (after tax) removed in 2.3 so multiple methods for discounts are no longer required.
     * @return mixed formatted price or false if there are none.
     */
    public function get_discounts_before_tax() {
        wc_deprecated_function( 'get_discounts_before_tax', '2.3', 'get_total_discount' );
        if ( $this->get_cart_discount_total() ) {
            $discounts_before_tax = wc_price( $this->get_cart_discount_total() );
        } else {
            $discounts_before_tax = false;
        }
        return apply_filters( 'woocommerce_cart_discounts_before_tax', $discounts_before_tax, $this );
    }
 
    /**
     * Get the total of all order discounts (after tax discounts).
     *
     * @deprecated Order discounts (after tax) removed in 2.3.
     * @return int
     */
    public function get_order_discount_total() {
        wc_deprecated_function( 'get_order_discount_total', '2.3' );
        return 0;
    }
 
    /**
     * Function to apply cart discounts after tax.
     *
     * @deprecated Coupons can not be applied after tax.
     * @param $values
     * @param $price
     */
    public function apply_cart_discounts_after_tax( $values, $price ) {
        wc_deprecated_function( 'apply_cart_discounts_after_tax', '2.3' );
    }
 
    /**
     * Function to apply product discounts after tax.
     *
     * @deprecated Coupons can not be applied after tax.
     *
     * @param $values
     * @param $price
     */
    public function apply_product_discounts_after_tax( $values, $price ) {
        wc_deprecated_function( 'apply_product_discounts_after_tax', '2.3' );
    }
 
    /**
     * Gets the order discount amount - these are applied after tax.
     *
     * @deprecated Coupons can not be applied after tax.
     */
    public function get_discounts_after_tax() {
        wc_deprecated_function( 'get_discounts_after_tax', '2.3' );
    }
}


Top ↑

Methods Methods


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.