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
|
|
|
* @package WooCommerce/Shortcodes/Cart |
8
|
|
|
* @version 2.3.0 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
defined( 'ABSPATH' ) || exit; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Shortcode cart class. |
15
|
|
|
*/ |
16
|
|
|
class WC_Shortcode_Cart { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Calculate shipping for the cart. |
20
|
|
|
* |
21
|
|
|
* @throws Exception When some data is invalid. |
22
|
|
|
*/ |
23
|
|
|
public static function calculate_shipping() { |
24
|
|
|
try { |
25
|
|
|
WC()->shipping()->reset_shipping(); |
26
|
|
|
|
27
|
|
|
$address = array(); |
28
|
|
|
|
29
|
|
|
$address['country'] = isset( $_POST['calc_shipping_country'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_country'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok. |
30
|
|
|
$address['state'] = isset( $_POST['calc_shipping_state'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_state'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok. |
31
|
|
|
$address['postcode'] = isset( $_POST['calc_shipping_postcode'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_postcode'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok. |
32
|
|
|
$address['city'] = isset( $_POST['calc_shipping_city'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_city'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok. |
33
|
|
|
|
34
|
|
|
$address = apply_filters( 'woocommerce_cart_calculate_shipping_address', $address ); |
35
|
|
|
|
36
|
|
|
if ( $address['postcode'] && ! WC_Validation::is_postcode( $address['postcode'], $address['country'] ) ) { |
37
|
|
|
throw new Exception( __( 'Please enter a valid postcode / ZIP.', 'woocommerce' ) ); |
38
|
|
|
} elseif ( $address['postcode'] ) { |
39
|
|
|
$address['postcode'] = wc_format_postcode( $address['postcode'], $address['country'] ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ( $address['country'] ) { |
43
|
|
|
WC()->customer->set_billing_location( $address['country'], $address['state'], $address['postcode'], $address['city'] ); |
44
|
|
|
WC()->customer->set_shipping_location( $address['country'], $address['state'], $address['postcode'], $address['city'] ); |
45
|
|
|
} else { |
46
|
|
|
WC()->customer->set_billing_address_to_base(); |
47
|
|
|
WC()->customer->set_shipping_address_to_base(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
WC()->customer->set_calculated_shipping( true ); |
51
|
|
|
WC()->customer->save(); |
52
|
|
|
|
53
|
|
|
wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' ); |
54
|
|
|
|
55
|
|
|
do_action( 'woocommerce_calculated_shipping' ); |
56
|
|
|
|
57
|
|
|
} catch ( Exception $e ) { |
58
|
|
|
if ( ! empty( $e ) ) { |
59
|
|
|
wc_add_notice( $e->getMessage(), 'error' ); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Output the cart shortcode. |
66
|
|
|
* |
67
|
|
|
* @param array $atts Shortcode attributes. |
68
|
|
|
*/ |
69
|
|
|
public static function output( $atts ) { |
70
|
|
|
if ( ! apply_filters( 'woocommerce_output_cart_shortcode_content', true ) ) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Constants. |
75
|
|
|
wc_maybe_define_constant( 'WOOCOMMERCE_CART', true ); |
76
|
|
|
|
77
|
|
|
$atts = shortcode_atts( array(), $atts, 'woocommerce_cart' ); |
|
|
|
|
78
|
|
|
$nonce_value = wc_get_var( $_REQUEST['woocommerce-shipping-calculator-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine. |
79
|
|
|
|
80
|
|
|
// Update Shipping. Nonce check uses new value and old value (woocommerce-cart). @todo remove in 4.0. |
81
|
|
View Code Duplication |
if ( ! empty( $_POST['calc_shipping'] ) && ( wp_verify_nonce( $nonce_value, 'woocommerce-shipping-calculator' ) || wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) ) { // WPCS: input var ok. |
82
|
|
|
self::calculate_shipping(); |
83
|
|
|
|
84
|
|
|
// Also calc totals before we check items so subtotals etc are up to date. |
85
|
|
|
WC()->cart->calculate_totals(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Check cart items are valid. |
89
|
|
|
do_action( 'woocommerce_check_cart_items' ); |
90
|
|
|
|
91
|
|
|
// Calc totals. |
92
|
|
|
WC()->cart->calculate_totals(); |
93
|
|
|
|
94
|
|
|
if ( WC()->cart->is_empty() ) { |
95
|
|
|
wc_get_template( 'cart/cart-empty.php' ); |
96
|
|
|
} else { |
97
|
|
|
wc_get_template( 'cart/cart.php' ); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.