1 <?php
2 3 4 5 6 7 8 9 10 11
12 class WC_Shortcode_Cart {
13
14 15 16 17 18
19 public static function output( $atts ) {
20
21
22 if ( is_null( WC()->cart ) ) {
23 return;
24 }
25
26
27 if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
28 define( 'WOOCOMMERCE_CART', true );
29 }
30
31
32 if ( ! empty( $_POST['calc_shipping'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) {
33
34 try {
35 WC()->shipping->reset_shipping();
36
37 $country = wc_clean( $_POST['calc_shipping_country'] );
38 $state = isset( $_POST['calc_shipping_state'] ) ? wc_clean( $_POST['calc_shipping_state'] ) : '';
39 $postcode = apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true ) ? wc_clean( $_POST['calc_shipping_postcode'] ) : '';
40 $city = apply_filters( 'woocommerce_shipping_calculator_enable_city', false ) ? wc_clean( $_POST['calc_shipping_city'] ) : '';
41
42 if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
43 throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ) );
44 } elseif ( $postcode ) {
45 $postcode = wc_format_postcode( $postcode, $country );
46 }
47
48 if ( $country ) {
49 WC()->customer->set_location( $country, $state, $postcode, $city );
50 WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
51 } else {
52 WC()->customer->set_to_base();
53 WC()->customer->set_shipping_to_base();
54 }
55
56 WC()->customer->calculated_shipping( true );
57
58 wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );
59
60 do_action( 'woocommerce_calculated_shipping' );
61
62 } catch ( Exception $e ) {
63
64 if ( ! empty( $e ) )
65 wc_add_notice( $e->getMessage(), 'error' );
66 }
67 }
68
69
70 do_action('woocommerce_check_cart_items');
71
72
73 WC()->cart->calculate_totals();
74
75 if ( sizeof( WC()->cart->get_cart() ) == 0 )
76 wc_get_template( 'cart/cart-empty.php' );
77 else
78 wc_get_template( 'cart/cart.php' );
79
80 }
81 }
82