class-wc-shortcode-cart.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Shortcode cart class.
  13. */
  14. class WC_Shortcode_Cart {
  15. /**
  16. * Calculate shipping for the cart.
  17. *
  18. * @throws Exception When some data is invalid.
  19. */
  20. public static function calculate_shipping() {
  21. try {
  22. WC()->shipping->reset_shipping();
  23. $country = isset( $_POST['calc_shipping_country'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_country'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok.
  24. $state = isset( $_POST['calc_shipping_state'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_state'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok.
  25. $postcode = isset( $_POST['calc_shipping_postcode'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_postcode'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok.
  26. $city = isset( $_POST['calc_shipping_city'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_city'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok.
  27. if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
  28. throw new Exception( __( 'Please enter a valid postcode / ZIP.', 'woocommerce' ) );
  29. } elseif ( $postcode ) {
  30. $postcode = wc_format_postcode( $postcode, $country );
  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_billing_address_to_base();
  37. WC()->customer->set_shipping_address_to_base();
  38. }
  39. WC()->customer->set_calculated_shipping( true );
  40. WC()->customer->save();
  41. wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );
  42. do_action( 'woocommerce_calculated_shipping' );
  43. } catch ( Exception $e ) {
  44. if ( ! empty( $e ) ) {
  45. wc_add_notice( $e->getMessage(), 'error' );
  46. }
  47. }
  48. }
  49. /**
  50. * Output the cart shortcode.
  51. *
  52. * @param array $atts Shortcode attributes.
  53. */
  54. public static function output( $atts ) {
  55. // Constants.
  56. wc_maybe_define_constant( 'WOOCOMMERCE_CART', true );
  57. $atts = shortcode_atts( array(), $atts, 'woocommerce_cart' );
  58. $nonce_value = wc_get_var( $_REQUEST['woocommerce-shipping-calculator-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
  59. // Update Shipping. Nonce check uses new value and old value (woocommerce-cart). @todo remove in 4.0.
  60. if ( ! empty( $_POST['calc_shipping'] ) && ( wp_verify_nonce( $nonce_value, 'woocommerce-shipping-calculator' ) || wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) ) { // WPCS: input var ok.
  61. self::calculate_shipping();
  62. // Also calc totals before we check items so subtotals etc are up to date.
  63. WC()->cart->calculate_totals();
  64. }
  65. // Check cart items are valid.
  66. do_action( 'woocommerce_check_cart_items' );
  67. // Calc totals.
  68. WC()->cart->calculate_totals();
  69. if ( WC()->cart->is_empty() ) {
  70. wc_get_template( 'cart/cart-empty.php' );
  71. } else {
  72. wc_get_template( 'cart/cart.php' );
  73. }
  74. }
  75. }