WC_Shortcode_Cart

Cart Shortcode.

Defined (1)

The class is defined in the following location(s).

/includes/shortcodes/class-wc-shortcode-cart.php  
  1. class WC_Shortcode_Cart { 
  2.  
  3. /** 
  4. * Calculate shipping for the cart. 
  5. */ 
  6. public static function calculate_shipping() { 
  7. try { 
  8. WC()->shipping->reset_shipping(); 
  9.  
  10. $country = wc_clean( $_POST['calc_shipping_country'] ); 
  11. $state = wc_clean( isset( $_POST['calc_shipping_state'] ) ? $_POST['calc_shipping_state'] : '' ); 
  12. $postcode = apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true ) ? wc_clean( $_POST['calc_shipping_postcode'] ) : ''; 
  13. $city = apply_filters( 'woocommerce_shipping_calculator_enable_city', false ) ? wc_clean( $_POST['calc_shipping_city'] ) : ''; 
  14.  
  15. if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) { 
  16. throw new Exception( __( 'Please enter a valid postcode / ZIP.', 'woocommerce' ) ); 
  17. } elseif ( $postcode ) { 
  18. $postcode = wc_format_postcode( $postcode, $country ); 
  19.  
  20. if ( $country ) { 
  21. WC()->customer->set_location( $country, $state, $postcode, $city ); 
  22. WC()->customer->set_shipping_location( $country, $state, $postcode, $city ); 
  23. } else { 
  24. WC()->customer->set_to_base(); 
  25. WC()->customer->set_shipping_to_base(); 
  26.  
  27. WC()->customer->set_calculated_shipping( true ); 
  28. WC()->customer->save(); 
  29.  
  30. wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' ); 
  31.  
  32. do_action( 'woocommerce_calculated_shipping' ); 
  33.  
  34. } catch ( Exception $e ) { 
  35. if ( ! empty( $e ) ) { 
  36. wc_add_notice( $e->getMessage(), 'error' ); 
  37.  
  38. /** 
  39. * Output the cart shortcode. 
  40. * @param array $atts 
  41. */ 
  42. public static function output( $atts ) { 
  43. // Constants 
  44. if ( ! defined( 'WOOCOMMERCE_CART' ) ) { 
  45. define( 'WOOCOMMERCE_CART', true ); 
  46.  
  47. $atts = shortcode_atts( array(), $atts, 'woocommerce_cart' ); 
  48.  
  49. // Update Shipping 
  50. if ( ! empty( $_POST['calc_shipping'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) { 
  51. self::calculate_shipping(); 
  52.  
  53. // Also calc totals before we check items so subtotals etc are up to date 
  54. WC()->cart->calculate_totals(); 
  55.  
  56. // Check cart items are valid 
  57. do_action( 'woocommerce_check_cart_items' ); 
  58.  
  59. // Calc totals 
  60. WC()->cart->calculate_totals(); 
  61.  
  62. if ( WC()->cart->is_empty() ) { 
  63. wc_get_template( 'cart/cart-empty.php' ); 
  64. } else { 
  65. wc_get_template( 'cart/cart.php' );