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->calculated_shipping( true ); |
41
|
|
|
|
42
|
|
|
wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' ); |
43
|
|
|
|
44
|
|
|
do_action( 'woocommerce_calculated_shipping' ); |
45
|
|
|
|
46
|
|
|
} catch ( Exception $e ) { |
47
|
|
|
if ( ! empty( $e ) ) { |
48
|
|
|
wc_add_notice( $e->getMessage(), 'error' ); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Output the cart shortcode. |
55
|
|
|
*/ |
56
|
|
|
public static function output() { |
57
|
|
|
// Constants |
58
|
|
|
if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
59
|
|
|
define( 'WOOCOMMERCE_CART', true ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Update Shipping |
63
|
|
|
if ( ! empty( $_POST['calc_shipping'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) { |
64
|
|
|
self::calculate_shipping(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Check cart items are valid |
68
|
|
|
do_action( 'woocommerce_check_cart_items' ); |
69
|
|
|
|
70
|
|
|
// Calc totals |
71
|
|
|
WC()->cart->calculate_totals(); |
72
|
|
|
|
73
|
|
|
if ( WC()->cart->is_empty() ) { |
74
|
|
|
wc_get_template( 'cart/cart-empty.php' ); |
75
|
|
|
} else { |
76
|
|
|
wc_get_template( 'cart/cart.php' ); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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.