banner
2 years ago
emailkit
1 year ago
metform-promo-banner
1 year ago
notice
1 year ago
onboard
11 months ago
plugins
2 years ago
pro-awareness
1 year ago
stories
2 years ago
controls-helper.php
4 years ago
elementor-data-map.php
4 years ago
global-helper.php
3 years ago
helper.php
1 year ago
notice.php
1 year ago
shipping-calculation.php
3 years ago
util.php
2 years ago
shipping-calculation.php
79 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Utils; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use WC_Validation; |
| 8 | use Exception; |
| 9 | |
| 10 | /** |
| 11 | * Global helper class. |
| 12 | * |
| 13 | * @since 1.0.0 |
| 14 | */ |
| 15 | class Shipping_Calculation { |
| 16 | |
| 17 | /** |
| 18 | * Output the cart shortcode. |
| 19 | * |
| 20 | * @param array $atts Shortcode attributes. |
| 21 | */ |
| 22 | public static function output() { |
| 23 | |
| 24 | if(!empty($_REQUEST['woocommerce-shipping-calculator-nonce'])) { |
| 25 | $nonce_key = 'woocommerce-shipping-calculator-nonce'; |
| 26 | } else { |
| 27 | $nonce_key = '_wpnonce'; |
| 28 | } |
| 29 | |
| 30 | // Update Shipping. Nonce check uses new value and old value (woocommerce-cart). @todo remove in 4.0. |
| 31 | if ( ! empty( $_POST['calc_shipping'] ) && !empty($_REQUEST[$nonce_key]) && |
| 32 | ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST[$nonce_key] ) ), 'woocommerce-shipping-calculator' ) || |
| 33 | wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST[$nonce_key] ) ), 'woocommerce-cart' ) ) ) { |
| 34 | try { |
| 35 | WC()->shipping()->reset_shipping(); |
| 36 | |
| 37 | $address = array(); |
| 38 | |
| 39 | $address['country'] = isset( $_POST['calc_shipping_country'] ) ? map_deep( wp_unslash( $_POST['calc_shipping_country'] ), 'sanitize_text_field' ) : ''; |
| 40 | $address['state'] = isset( $_POST['calc_shipping_state'] ) ? map_deep( wp_unslash( $_POST['calc_shipping_state'] ), 'sanitize_text_field' ) : ''; |
| 41 | $address['postcode'] = isset( $_POST['calc_shipping_postcode'] ) ? map_deep( wp_unslash( $_POST['calc_shipping_postcode'] ), 'sanitize_text_field' ) : ''; |
| 42 | $address['city'] = isset( $_POST['calc_shipping_city'] ) ? map_deep( wp_unslash( $_POST['calc_shipping_city'] ), 'sanitize_text_field' ) : ''; |
| 43 | |
| 44 | $address = apply_filters( 'woocommerce_cart_calculate_shipping_address', $address ); |
| 45 | |
| 46 | if ( $address['postcode'] && ! WC_Validation::is_postcode( $address['postcode'], $address['country'] ) ) { |
| 47 | throw new Exception( __( 'Please enter a valid postcode / ZIP.', 'shopengine' ) ); |
| 48 | } elseif ( $address['postcode'] ) { |
| 49 | $address['postcode'] = wc_format_postcode( $address['postcode'], $address['country'] ); |
| 50 | } |
| 51 | |
| 52 | if ( $address['country'] ) { |
| 53 | if ( ! WC()->customer->get_billing_first_name() ) { |
| 54 | WC()->customer->set_billing_location( $address['country'], $address['state'], $address['postcode'], $address['city'] ); |
| 55 | } |
| 56 | WC()->customer->set_shipping_location( $address['country'], $address['state'], $address['postcode'], $address['city'] ); |
| 57 | } else { |
| 58 | WC()->customer->set_billing_address_to_base(); |
| 59 | WC()->customer->set_shipping_address_to_base(); |
| 60 | } |
| 61 | |
| 62 | WC()->customer->set_calculated_shipping( true ); |
| 63 | WC()->customer->save(); |
| 64 | |
| 65 | wc_add_notice( __( 'Shipping costs updated.', 'shopengine' ), 'notice' ); |
| 66 | |
| 67 | do_action( 'woocommerce_calculated_shipping' ); |
| 68 | |
| 69 | } catch ( Exception $e ) { |
| 70 | if ( ! empty( $e ) ) { |
| 71 | wc_add_notice( $e->getMessage(), 'error' ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Also calc totals before we check items so subtotals etc are up to date. |
| 76 | WC()->cart->calculate_totals(); |
| 77 | } |
| 78 | } |
| 79 | } |