PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 2.4.0
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v2.4.0
4.9.2 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 3 years ago elementor-data-map.php 3 years ago helper.php 3 years ago notice.php 3 years ago shipping-calculation.php 3 years ago
shipping-calculation.php
83 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 * Calculate shipping for the cart.
19 *
20 * @throws Exception When some data is invalid.
21 */
22 public static function calculate_shipping() {
23
24 try {
25 WC()->shipping()->reset_shipping();
26
27 $address = array();
28
29 $address['country'] = isset( $_POST['calc_shipping_country'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_country'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok.
30 $address['state'] = isset( $_POST['calc_shipping_state'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_state'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok.
31 $address['postcode'] = isset( $_POST['calc_shipping_postcode'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_postcode'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok.
32 $address['city'] = isset( $_POST['calc_shipping_city'] ) ? wc_clean( wp_unslash( $_POST['calc_shipping_city'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok.
33
34 $address = apply_filters( 'woocommerce_cart_calculate_shipping_address', $address );
35
36 if ( $address['postcode'] && ! WC_Validation::is_postcode( $address['postcode'], $address['country'] ) ) {
37 throw new Exception( __( 'Please enter a valid postcode / ZIP.', 'shopengine' ) );
38 } elseif ( $address['postcode'] ) {
39 $address['postcode'] = wc_format_postcode( $address['postcode'], $address['country'] );
40 }
41
42 if ( $address['country'] ) {
43 if ( ! WC()->customer->get_billing_first_name() ) {
44 WC()->customer->set_billing_location( $address['country'], $address['state'], $address['postcode'], $address['city'] );
45 }
46 WC()->customer->set_shipping_location( $address['country'], $address['state'], $address['postcode'], $address['city'] );
47 } else {
48 WC()->customer->set_billing_address_to_base();
49 WC()->customer->set_shipping_address_to_base();
50 }
51
52 WC()->customer->set_calculated_shipping( true );
53 WC()->customer->save();
54
55 wc_add_notice( __( 'Shipping costs updated.', 'shopengine' ), 'notice' );
56
57 do_action( 'woocommerce_calculated_shipping' );
58
59 } catch ( Exception $e ) {
60 if ( ! empty( $e ) ) {
61 wc_add_notice( $e->getMessage(), 'error' );
62 }
63 }
64 }
65
66 /**
67 * Output the cart shortcode.
68 *
69 * @param array $atts Shortcode attributes.
70 */
71 public static function output() {
72
73 $nonce_value = wc_get_var( $_REQUEST['woocommerce-shipping-calculator-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine.
74
75 // Update Shipping. Nonce check uses new value and old value (woocommerce-cart). @todo remove in 4.0.
76 if ( ! empty( $_POST['calc_shipping'] ) && ( wp_verify_nonce( $nonce_value, 'woocommerce-shipping-calculator' ) || wp_verify_nonce( $nonce_value, 'woocommerce-cart' ) ) ) { // WPCS: input var ok.
77 self::calculate_shipping();
78
79 // Also calc totals before we check items so subtotals etc are up to date.
80 WC()->cart->calculate_totals();
81 }
82 }
83 }