shipping-calculator.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Shipping Calculator
  4. *
  5. * This template can be overridden by copying it to yourtheme/woocommerce/cart/shipping-calculator.php.
  6. *
  7. * HOWEVER, on occasion WooCommerce will need to update template files and you
  8. * (the theme developer) will need to copy the new files to your theme to
  9. * maintain compatibility. We try to do this as little as possible, but it does
  10. * happen. When this occurs the version of the template file will be bumped and
  11. * the readme will list any important changes.
  12. *
  13. * @see https://docs.woocommerce.com/document/template-structure/
  14. * @package WooCommerce/Templates
  15. * @version 3.4.0
  16. */
  17. defined( 'ABSPATH' ) || exit;
  18. if ( 'no' === get_option( 'woocommerce_enable_shipping_calc' ) || ! WC()->cart->needs_shipping() ) {
  19. return;
  20. }
  21. wp_enqueue_script( 'wc-country-select' );
  22. do_action( 'woocommerce_before_shipping_calculator' ); ?>
  23. <form class="woocommerce-shipping-calculator" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
  24. <p><a href="#" class="shipping-calculator-button"><?php esc_html_e( 'Calculate shipping', 'woocommerce' ); ?></a></p>
  25. <section class="shipping-calculator-form" style="display:none;">
  26. <p class="form-row form-row-wide" id="calc_shipping_country_field">
  27. <select name="calc_shipping_country" id="calc_shipping_country" class="country_to_state country_select" rel="calc_shipping_state">
  28. <option value=""><?php esc_html_e( 'Select a country&hellip;', 'woocommerce' ); ?></option>
  29. <?php
  30. foreach ( WC()->countries->get_shipping_countries() as $key => $value ) {
  31. echo '<option value="' . esc_attr( $key ) . '"' . selected( WC()->customer->get_shipping_country(), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
  32. }
  33. ?>
  34. </select>
  35. </p>
  36. <?php if ( apply_filters( 'woocommerce_shipping_calculator_enable_state', true ) ) : ?>
  37. <p class="form-row form-row-wide" id="calc_shipping_state_field">
  38. <?php
  39. $current_cc = WC()->customer->get_shipping_country();
  40. $current_r = WC()->customer->get_shipping_state();
  41. $states = WC()->countries->get_states( $current_cc );
  42. if ( is_array( $states ) && empty( $states ) ) {
  43. ?>
  44. <input type="hidden" name="calc_shipping_state" id="calc_shipping_state" placeholder="<?php esc_attr_e( 'State / County', 'woocommerce' ); ?>" />
  45. <?php
  46. } elseif ( is_array( $states ) ) {
  47. ?>
  48. <span>
  49. <select name="calc_shipping_state" class="state_select" id="calc_shipping_state" placeholder="<?php esc_attr_e( 'State / County', 'woocommerce' ); ?>">
  50. <option value=""><?php esc_html_e( 'Select a state&hellip;', 'woocommerce' ); ?></option>
  51. <?php
  52. foreach ( $states as $ckey => $cvalue ) {
  53. echo '<option value="' . esc_attr( $ckey ) . '" ' . selected( $current_r, $ckey, false ) . '>' . esc_html( $cvalue ) . '</option>';
  54. }
  55. ?>
  56. </select>
  57. </span>
  58. <?php
  59. } else {
  60. ?>
  61. <input type="text" class="input-text" value="<?php echo esc_attr( $current_r ); ?>" placeholder="<?php esc_attr_e( 'State / County', 'woocommerce' ); ?>" name="calc_shipping_state" id="calc_shipping_state" />
  62. <?php
  63. }
  64. ?>
  65. </p>
  66. <?php endif; ?>
  67. <?php if ( apply_filters( 'woocommerce_shipping_calculator_enable_city', true ) ) : ?>
  68. <p class="form-row form-row-wide" id="calc_shipping_city_field">
  69. <input type="text" class="input-text" value="<?php echo esc_attr( WC()->customer->get_shipping_city() ); ?>" placeholder="<?php esc_attr_e( 'City', 'woocommerce' ); ?>" name="calc_shipping_city" id="calc_shipping_city" />
  70. </p>
  71. <?php endif; ?>
  72. <?php if ( apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true ) ) : ?>
  73. <p class="form-row form-row-wide" id="calc_shipping_postcode_field">
  74. <input type="text" class="input-text" value="<?php echo esc_attr( WC()->customer->get_shipping_postcode() ); ?>" placeholder="<?php esc_attr_e( 'Postcode / ZIP', 'woocommerce' ); ?>" name="calc_shipping_postcode" id="calc_shipping_postcode" />
  75. </p>
  76. <?php endif; ?>
  77. <p><button type="submit" name="calc_shipping" value="1" class="button"><?php esc_html_e( 'Update totals', 'woocommerce' ); ?></button></p>
  78. <?php wp_nonce_field( 'woocommerce-shipping-calculator', 'woocommerce-shipping-calculator-nonce' ); ?>
  79. </section>
  80. </form>
  81. <?php do_action( 'woocommerce_after_shipping_calculator' ); ?>