PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 4.3.0
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v4.3.0
4.9.1 4.9.0 2.0.0 2.1.0 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.1 3.0.0 3.1.0 3.1.1 4.0.0 4.0.1 4.1.0 4.1.1 4.2.0 4.2.1 4.3.0 4.3.1 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.6.8 4.6.9 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.8.9 trunk 0.1.2-beta 0.1.3-beta 0.1.4-beta 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.5.0 1.5.1 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.9.0
shopengine / utils / shipping-calculation.php
shopengine / utils Last commit date
controls-helper.php 4 years ago elementor-data-map.php 4 years ago global-helper.php 3 years ago helper.php 3 years ago notice.php 3 years ago shipping-calculation.php 3 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 }