| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Shortcode; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use Exception; |
| 10 |
use StoreEngine; |
| 11 |
use StoreEngine\Utils\Formatting; |
| 12 |
use StoreEngine\Utils\Helper; |
| 13 |
use StoreEngine\Utils\Template; |
| 14 |
use StoreEngine\Utils\Validation; |
| 15 |
|
| 16 |
class CartSubTotalTable { |
| 17 |
public function __construct() { |
| 18 |
add_shortcode( 'storeengine_cart_sub_total_table', array( $this, 'render_cart_sub_total_table' ) ); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Calculate shipping for the cart. |
| 23 |
*/ |
| 24 |
public static function calculate_shipping() { |
| 25 |
try { |
| 26 |
\StoreEngine\Shipping\Shipping::init()->reset_shipping(); |
| 27 |
|
| 28 |
$address = []; |
| 29 |
|
| 30 |
// phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Formatting::clean sanitizes the input |
| 31 |
$address['country'] = isset( $_POST['calc_shipping_country'] ) ? Formatting::clean( wp_unslash( $_POST['calc_shipping_country'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok. |
| 32 |
$address['state'] = isset( $_POST['calc_shipping_state'] ) ? Formatting::clean( wp_unslash( $_POST['calc_shipping_state'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok. |
| 33 |
$address['postcode'] = isset( $_POST['calc_shipping_postcode'] ) ? Formatting::clean( wp_unslash( $_POST['calc_shipping_postcode'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok. |
| 34 |
$address['city'] = isset( $_POST['calc_shipping_city'] ) ? Formatting::clean( wp_unslash( $_POST['calc_shipping_city'] ) ) : ''; // WPCS: input var ok, CSRF ok, sanitization ok. |
| 35 |
// phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Formatting::clean sanitizes the input |
| 36 |
|
| 37 |
if ( $address['postcode'] ) { |
| 38 |
$address['postcode'] = Formatting::format_postcode( $address['postcode'], $address['country'] ); |
| 39 |
} |
| 40 |
|
| 41 |
$address = apply_filters( 'storeengine/cart/calculate_shipping_address', $address ); |
| 42 |
|
| 43 |
if ( $address['postcode'] && ! Validation::is_postcode( $address['postcode'], $address['country'] ) ) { |
| 44 |
throw new Exception( esc_html__( 'Please enter a valid postcode / ZIP.', 'storeengine' ) ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( $address['country'] ) { |
| 48 |
if ( ! StoreEngine::init()->customer->get_billing_first_name() ) { |
| 49 |
StoreEngine::init()->customer->set_billing_location( $address['country'], $address['state'], $address['postcode'], $address['city'] ); |
| 50 |
} |
| 51 |
StoreEngine::init()->customer->set_shipping_location( $address['country'], $address['state'], $address['postcode'], $address['city'] ); |
| 52 |
} else { |
| 53 |
StoreEngine::init()->customer->set_billing_address_to_base(); |
| 54 |
StoreEngine::init()->customer->set_shipping_address_to_base(); |
| 55 |
} |
| 56 |
|
| 57 |
StoreEngine::init()->customer->set_calculated_shipping( true ); |
| 58 |
StoreEngine::init()->customer->save(); |
| 59 |
|
| 60 |
storeengine_show_notice( __( 'Shipping costs updated.', 'storeengine' ), 'notice' ); |
| 61 |
|
| 62 |
do_action( 'storeengine/cart/calculated_shipping' ); |
| 63 |
|
| 64 |
} catch ( Exception $e ) { |
| 65 |
Helper::log_error( $e ); |
| 66 |
storeengine_show_notice( $e->getMessage(), 'error' ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public function render_cart_sub_total_table() { |
| 71 |
if ( Formatting::string_to_bool( get_query_var( 'order_pay' ) ) ) { |
| 72 |
return ''; |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! empty( $_POST['calc_shipping'] ) && ! empty( $_REQUEST['security'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['security'] ) ), 'storeengine_nonce' ) ) { |
| 76 |
self::calculate_shipping(); |
| 77 |
|
| 78 |
// Update cart total. |
| 79 |
StoreEngine::init()->cart->calculate_totals(); |
| 80 |
} |
| 81 |
|
| 82 |
ob_start(); |
| 83 |
Template::get_template( 'shortcode/cart-sub-total-table.php' ); |
| 84 |
return ob_get_clean(); |
| 85 |
} |
| 86 |
} |
| 87 |
|