Brak opisu

class-wc-shortcode-cart.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. $address = array();
  24. $address['country'] = isset( $_POST['calc_shipping_country'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_country'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok.
  25. $address['state'] = isset( $_POST['calc_shipping_state'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_state'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok.
  26. $address['postcode'] = isset( $_POST['calc_shipping_postcode'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_postcode'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok.
  27. $address['city'] = isset( $_POST['calc_shipping_city'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_city'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok.
  28. $address = apply_filters( 'woocommerce_cart_calculate_shipping_address', $address );
  29. if ( $address['postcode'] && ! WC_Validation::is_postcode( $address['postcode'], $address['country'] ) ) {
  30. throw new Exception( __( 'Please enter a valid postcode / ZIP.', 'woocommerce' ) );
  31. } elseif ( $address['postcode'] ) {
  32. $address['postcode'] = wc_format_postcode( $address['postcode'], $address['country'] );
  33. }
  34. if ( $address['country'] ) {
  35. if ( ! WC()->customer->get_billing_first_name() ) {
  36. WC()->customer->set_billing_location( $address['country'], $address['state'], $address['postcode'], $address['city'] );
  37. }
  38. WC()->customer->set_shipping_location( $address['country'], $address['state'], $address['postcode'], $address['city'] );
  39. } else {
  40. WC()->customer->set_billing_address_to_base();
  41. WC()->customer->set_shipping_address_to_base();
  42. }
  43. WC()->customer->set_calculated_shipping( true );
  44. WC()->customer->save();
  45. wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );
  46. do_action( 'woocommerce_calculated_shipping' );
  47. } catch ( Exception $e ) {
  48. if ( ! empty( $e ) ) {
  49. wc_add_notice( $e->getMessage(), 'error' );
  50. }
  51. }
  52. }
  53. /**
  54. * Output the cart shortcode.
  55. *
  56. * @param array $atts Shortcode attributes.
  57. */
  58. public static function output( $atts ) {
  59. if ( ! apply_filters( 'woocommerce_output_cart_shortcode_content', true ) ) {
  60. return;
  61. }
  62. // Constants.
  63. wc_maybe_define_constant( 'WOOCOMMERCE_CART', true );
  64. $atts = shortcode_atts( array(), $atts, 'woocommerce_cart' );
  65. $nonce_value = wc_get_var( $_REQUEST['woocommerce-shipping-calculator-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
  66. // Update Shipping. Nonce check uses new value and old value (woocommerce-cart). @todo remove in 4.0.
  67. if ( ! empty( $_POST['calc_shipping'] ) && ( wp_verify_nonce( $nonce_value, 'woocommerce-shipping-calculator' ) || wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) ) { // WPCS: input var ok.
  68. self::calculate_shipping();
  69. // Also calc totals before we check items so subtotals etc are up to date.
  70. WC()->cart->calculate_totals();
  71. }
  72. // Check cart items are valid.
  73. do_action( 'woocommerce_check_cart_items' );
  74. // Calc totals.
  75. WC()->cart->calculate_totals();
  76. if ( WC()->cart->is_empty() ) {
  77. wc_get_template( 'cart/cart-empty.php' );
  78. } else {
  79. wc_get_template( 'cart/cart.php' );
  80. }
  81. }
  82. }