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.
|
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category Shortcodes
|
|
|
|
* @package WooCommerce/Shortcodes/Cart
|
2014-09-24 14:26:48 +00:00
|
|
|
* @version 2.3.0
|
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.
|
2012-12-31 18:25:09 +00:00
|
|
|
*/
|
2014-09-24 14:26:48 +00:00
|
|
|
public static function calculate_shipping() {
|
|
|
|
try {
|
|
|
|
WC()->shipping->reset_shipping();
|
|
|
|
|
|
|
|
$country = wc_clean( $_POST['calc_shipping_country'] );
|
|
|
|
$state = wc_clean( isset( $_POST['calc_shipping_state'] ) ? $_POST['calc_shipping_state'] : '' );
|
|
|
|
$postcode = apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true ) ? wc_clean( $_POST['calc_shipping_postcode'] ) : '';
|
|
|
|
$city = apply_filters( 'woocommerce_shipping_calculator_enable_city', false ) ? wc_clean( $_POST['calc_shipping_city'] ) : '';
|
|
|
|
|
|
|
|
if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
|
|
|
|
throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ) );
|
|
|
|
} elseif ( $postcode ) {
|
|
|
|
$postcode = wc_format_postcode( $postcode, $country );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $country ) {
|
|
|
|
WC()->customer->set_location( $country, $state, $postcode, $city );
|
|
|
|
WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
|
|
|
|
} else {
|
|
|
|
WC()->customer->set_to_base();
|
|
|
|
WC()->customer->set_shipping_to_base();
|
|
|
|
}
|
|
|
|
|
|
|
|
WC()->customer->calculated_shipping( true );
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
public static function output() {
|
2014-02-24 15:19:12 +00:00
|
|
|
// Constants
|
2013-12-31 14:02:50 +00:00
|
|
|
if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
|
|
|
|
define( 'WOOCOMMERCE_CART', true );
|
|
|
|
}
|
2012-12-31 18:25:09 +00:00
|
|
|
|
|
|
|
// Update Shipping
|
2013-12-31 14:02:50 +00:00
|
|
|
if ( ! empty( $_POST['calc_shipping'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) {
|
2014-09-24 14:26:48 +00:00
|
|
|
self::calculate_shipping();
|
2016-06-18 19:16:12 +00:00
|
|
|
|
|
|
|
// Also calc totals before we check items so subtotals etc are up to date
|
|
|
|
WC()->cart->calculate_totals();
|
2012-12-31 18:25:09 +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
|
|
|
|
|
|
|
// 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
|
|
|
}
|