2012-12-31 18:25:09 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* Cart Shortcode
2012-12-31 18:25:09 +00:00
*
* Used on the cart page , the cart shortcode displays the cart contents and interface for coupon codes and other cart bits and pieces .
*
2018-03-09 15:59:01 +00:00
* @ package WooCommerce / Shortcodes / Cart
* @ version 2.3 . 0
*/
defined ( 'ABSPATH' ) || exit ;
/**
* Shortcode cart class .
2012-12-31 18:25:09 +00:00
*/
class WC_Shortcode_Cart {
/**
2015-11-03 13:31:20 +00:00
* Calculate shipping for the cart .
2018-03-09 15:59:01 +00:00
*
* @ throws Exception When some data is invalid .
2012-12-31 18:25:09 +00:00
*/
2014-09-24 14:26:48 +00:00
public static function calculate_shipping () {
try {
2018-10-02 15:03:17 +00:00
WC () -> shipping () -> reset_shipping ();
2014-09-24 14:26:48 +00:00
2018-05-11 16:26:34 +00:00
$address = array ();
2014-09-24 14:26:48 +00:00
2018-05-11 16:26:34 +00:00
$address [ 'country' ] = isset ( $_POST [ 'calc_shipping_country' ] ) ? wc_clean ( wp_unslash ( $_POST [ 'calc_shipping_country' ] ) ) : '' ; // WPCS: input var ok, CSRF ok, sanitization ok.
$address [ 'state' ] = isset ( $_POST [ 'calc_shipping_state' ] ) ? wc_clean ( wp_unslash ( $_POST [ 'calc_shipping_state' ] ) ) : '' ; // WPCS: input var ok, CSRF ok, sanitization ok.
$address [ 'postcode' ] = isset ( $_POST [ 'calc_shipping_postcode' ] ) ? wc_clean ( wp_unslash ( $_POST [ 'calc_shipping_postcode' ] ) ) : '' ; // WPCS: input var ok, CSRF ok, sanitization ok.
$address [ 'city' ] = isset ( $_POST [ 'calc_shipping_city' ] ) ? wc_clean ( wp_unslash ( $_POST [ 'calc_shipping_city' ] ) ) : '' ; // WPCS: input var ok, CSRF ok, sanitization ok.
$address = apply_filters ( 'woocommerce_cart_calculate_shipping_address' , $address );
if ( $address [ 'postcode' ] && ! WC_Validation :: is_postcode ( $address [ 'postcode' ], $address [ 'country' ] ) ) {
2016-10-11 01:39:13 +00:00
throw new Exception ( __ ( 'Please enter a valid postcode / ZIP.' , 'woocommerce' ) );
2018-05-11 16:26:34 +00:00
} elseif ( $address [ 'postcode' ] ) {
$address [ 'postcode' ] = wc_format_postcode ( $address [ 'postcode' ], $address [ 'country' ] );
2014-09-24 14:26:48 +00:00
}
2018-05-11 16:26:34 +00:00
if ( $address [ 'country' ] ) {
2019-01-15 16:49:01 +00:00
WC () -> customer -> set_billing_location ( $address [ 'country' ], $address [ 'state' ], $address [ 'postcode' ], $address [ 'city' ] );
2018-05-11 16:26:34 +00:00
WC () -> customer -> set_shipping_location ( $address [ 'country' ], $address [ 'state' ], $address [ 'postcode' ], $address [ 'city' ] );
2014-09-24 14:26:48 +00:00
} else {
2017-10-13 18:22:09 +00:00
WC () -> customer -> set_billing_address_to_base ();
WC () -> customer -> set_shipping_address_to_base ();
2014-09-24 14:26:48 +00:00
}
2016-03-09 20:49:02 +00:00
WC () -> customer -> set_calculated_shipping ( true );
2016-11-14 18:18:08 +00:00
WC () -> customer -> save ();
2014-09-24 14:26:48 +00:00
wc_add_notice ( __ ( 'Shipping costs updated.' , 'woocommerce' ), 'notice' );
2014-09-20 19:33:32 +00:00
2014-09-24 14:26:48 +00:00
do_action ( 'woocommerce_calculated_shipping' );
} catch ( Exception $e ) {
if ( ! empty ( $e ) ) {
wc_add_notice ( $e -> getMessage (), 'error' );
}
2014-02-24 15:19:12 +00:00
}
2014-09-24 14:26:48 +00:00
}
2012-12-31 18:25:09 +00:00
2014-09-24 14:26:48 +00:00
/**
* Output the cart shortcode .
2017-01-03 18:17:04 +00:00
*
2018-03-09 15:59:01 +00:00
* @ param array $atts Shortcode attributes .
2014-09-24 14:26:48 +00:00
*/
2017-01-03 18:17:04 +00:00
public static function output ( $atts ) {
2019-10-01 17:28:31 +00:00
if ( ! apply_filters ( 'woocommerce_output_cart_shortcode_content' , true ) ) {
return ;
}
2017-09-05 19:52:39 +00:00
// Constants.
wc_maybe_define_constant ( 'WOOCOMMERCE_CART' , true );
2012-12-31 18:25:09 +00:00
2018-03-13 14:16:56 +00:00
$atts = shortcode_atts ( array (), $atts , 'woocommerce_cart' );
$nonce_value = wc_get_var ( $_REQUEST [ 'woocommerce-shipping-calculator-nonce' ], wc_get_var ( $_REQUEST [ '_wpnonce' ], '' ) ); // @codingStandardsIgnoreLine.
2017-01-03 18:17:04 +00:00
2018-03-19 17:59:30 +00:00
// Update Shipping. Nonce check uses new value and old value (woocommerce-cart). @todo remove in 4.0.
if ( ! empty ( $_POST [ 'calc_shipping' ] ) && ( wp_verify_nonce ( $nonce_value , 'woocommerce-shipping-calculator' ) || wp_verify_nonce ( $nonce_value , 'woocommerce-cart' ) ) ) { // WPCS: input var ok.
2014-09-24 14:26:48 +00:00
self :: calculate_shipping ();
2016-06-18 19:16:12 +00:00
2018-03-09 15:59:01 +00:00
// Also calc totals before we check items so subtotals etc are up to date.
2016-06-18 19:16:12 +00:00
WC () -> cart -> calculate_totals ();
2012-12-31 18:25:09 +00:00
}
2018-03-09 15:59:01 +00:00
// Check cart items are valid.
2014-09-24 14:26:48 +00:00
do_action ( 'woocommerce_check_cart_items' );
2012-12-31 18:25:09 +00:00
2018-03-09 15:59:01 +00:00
// Calc totals.
2013-11-25 14:01:32 +00:00
WC () -> cart -> calculate_totals ();
2012-12-31 18:25:09 +00:00
2015-05-14 21:18:53 +00:00
if ( WC () -> cart -> is_empty () ) {
2013-11-25 12:45:04 +00:00
wc_get_template ( 'cart/cart-empty.php' );
2014-09-24 14:26:48 +00:00
} else {
2013-11-25 12:45:04 +00:00
wc_get_template ( 'cart/cart.php' );
2014-09-24 14:26:48 +00:00
}
2012-12-31 18:25:09 +00:00
}
2014-02-16 18:53:55 +00:00
}