|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Cart Shortcode |
|
4
|
|
|
* |
|
5
|
|
|
* Used on the cart page, the cart shortcode displays the cart contents and interface for coupon codes and other cart bits and pieces. |
|
6
|
|
|
* |
|
7
|
|
|
* @author WooThemes |
|
8
|
|
|
* @category Shortcodes |
|
9
|
|
|
* @package WooCommerce/Shortcodes/Cart |
|
10
|
|
|
* @version 2.3.0 |
|
11
|
|
|
*/ |
|
12
|
|
|
class WC_Shortcode_Cart { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Calculate shipping for the cart. |
|
16
|
|
|
*/ |
|
17
|
|
|
public static function calculate_shipping() { |
|
18
|
|
|
try { |
|
19
|
|
|
WC()->shipping->reset_shipping(); |
|
20
|
|
|
|
|
21
|
|
|
$country = wc_clean( $_POST['calc_shipping_country'] ); |
|
22
|
|
|
$state = wc_clean( isset( $_POST['calc_shipping_state'] ) ? $_POST['calc_shipping_state'] : '' ); |
|
23
|
|
|
$postcode = apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true ) ? wc_clean( $_POST['calc_shipping_postcode'] ) : ''; |
|
24
|
|
|
$city = apply_filters( 'woocommerce_shipping_calculator_enable_city', false ) ? wc_clean( $_POST['calc_shipping_city'] ) : ''; |
|
25
|
|
|
|
|
26
|
|
|
if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) { |
|
27
|
|
|
throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ) ); |
|
28
|
|
|
} elseif ( $postcode ) { |
|
29
|
|
|
$postcode = wc_format_postcode( $postcode, $country ); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if ( $country ) { |
|
33
|
|
|
WC()->customer->set_location( $country, $state, $postcode, $city ); |
|
|
|
|
|
|
34
|
|
|
WC()->customer->set_shipping_location( $country, $state, $postcode, $city ); |
|
35
|
|
|
} else { |
|
36
|
|
|
WC()->customer->set_to_base(); |
|
37
|
|
|
WC()->customer->set_shipping_to_base(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
WC()->customer->set_calculated_shipping( true ); |
|
41
|
|
|
|
|
42
|
|
|
WC()->customer->save_to_session(); |
|
43
|
|
|
|
|
44
|
|
|
wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' ); |
|
45
|
|
|
|
|
46
|
|
|
do_action( 'woocommerce_calculated_shipping' ); |
|
47
|
|
|
|
|
48
|
|
|
} catch ( Exception $e ) { |
|
49
|
|
|
if ( ! empty( $e ) ) { |
|
50
|
|
|
wc_add_notice( $e->getMessage(), 'error' ); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Output the cart shortcode. |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function output() { |
|
59
|
|
|
// Constants |
|
60
|
|
|
if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
|
61
|
|
|
define( 'WOOCOMMERCE_CART', true ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Update Shipping |
|
65
|
|
|
if ( ! empty( $_POST['calc_shipping'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) { |
|
66
|
|
|
self::calculate_shipping(); |
|
67
|
|
|
|
|
68
|
|
|
// Also calc totals before we check items so subtotals etc are up to date |
|
69
|
|
|
WC()->cart->calculate_totals(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Check cart items are valid |
|
73
|
|
|
do_action( 'woocommerce_check_cart_items' ); |
|
74
|
|
|
|
|
75
|
|
|
// Calc totals |
|
76
|
|
|
WC()->cart->calculate_totals(); |
|
77
|
|
|
|
|
78
|
|
|
if ( WC()->cart->is_empty() ) { |
|
79
|
|
|
wc_get_template( 'cart/cart-empty.php' ); |
|
80
|
|
|
} else { |
|
81
|
|
|
wc_get_template( 'cart/cart.php' ); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.