2012-12-31 18:25:09 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Cart Shortcode
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* @version 2.0.0
|
|
|
|
*/
|
|
|
|
class WC_Shortcode_Cart {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output the cart shortcode.
|
|
|
|
*
|
|
|
|
* @param array $atts
|
|
|
|
*/
|
|
|
|
public static function output( $atts ) {
|
2014-02-26 15:27:26 +00:00
|
|
|
|
2014-02-24 15:19:12 +00:00
|
|
|
// Check cart class is loaded or abort
|
|
|
|
if ( is_null( WC()->cart ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-12-31 18:25:09 +00:00
|
|
|
|
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' ) ) {
|
2012-12-31 18:25:09 +00:00
|
|
|
|
2013-07-19 06:58:50 +00:00
|
|
|
try {
|
2013-11-25 14:01:32 +00:00
|
|
|
WC()->shipping->reset_shipping();
|
2013-07-19 06:58:50 +00:00
|
|
|
|
2013-11-25 13:34:21 +00:00
|
|
|
$country = wc_clean( $_POST['calc_shipping_country'] );
|
2014-02-16 18:53:55 +00:00
|
|
|
$state = isset( $_POST['calc_shipping_state'] ) ? wc_clean( $_POST['calc_shipping_state'] ) : '';
|
2013-11-25 13:34:21 +00:00
|
|
|
$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'] ) : '';
|
2013-07-19 06:58:50 +00:00
|
|
|
|
2013-09-12 13:41:02 +00:00
|
|
|
if ( $postcode && ! WC_Validation::is_postcode( $postcode, $country ) ) {
|
2013-07-19 06:58:50 +00:00
|
|
|
throw new Exception( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ) );
|
|
|
|
} elseif ( $postcode ) {
|
2013-09-12 13:41:02 +00:00
|
|
|
$postcode = wc_format_postcode( $postcode, $country );
|
2013-07-19 06:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $country ) {
|
2013-11-25 14:01:32 +00:00
|
|
|
WC()->customer->set_location( $country, $state, $postcode, $city );
|
|
|
|
WC()->customer->set_shipping_location( $country, $state, $postcode, $city );
|
2013-07-19 06:58:50 +00:00
|
|
|
} else {
|
2013-11-25 14:01:32 +00:00
|
|
|
WC()->customer->set_to_base();
|
|
|
|
WC()->customer->set_shipping_to_base();
|
2013-07-19 06:58:50 +00:00
|
|
|
}
|
|
|
|
|
2013-11-25 14:01:32 +00:00
|
|
|
WC()->customer->calculated_shipping( true );
|
2012-12-31 18:25:09 +00:00
|
|
|
|
2013-11-14 00:20:27 +00:00
|
|
|
wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );
|
2012-12-31 18:25:09 +00:00
|
|
|
|
2013-07-19 06:58:50 +00:00
|
|
|
do_action( 'woocommerce_calculated_shipping' );
|
2012-12-31 18:25:09 +00:00
|
|
|
|
2013-07-19 06:58:50 +00:00
|
|
|
} catch ( Exception $e ) {
|
2012-12-31 18:25:09 +00:00
|
|
|
|
2013-07-19 06:58:50 +00:00
|
|
|
if ( ! empty( $e ) )
|
2014-01-21 11:14:19 +00:00
|
|
|
wc_add_notice( $e->getMessage(), 'error' );
|
2012-12-31 18:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check cart items are valid
|
|
|
|
do_action('woocommerce_check_cart_items');
|
|
|
|
|
|
|
|
// Calc totals
|
2013-11-25 14:01:32 +00:00
|
|
|
WC()->cart->calculate_totals();
|
2012-12-31 18:25:09 +00:00
|
|
|
|
2013-11-25 14:01:32 +00:00
|
|
|
if ( sizeof( WC()->cart->get_cart() ) == 0 )
|
2013-11-25 12:45:04 +00:00
|
|
|
wc_get_template( 'cart/cart-empty.php' );
|
2012-12-31 18:25:09 +00:00
|
|
|
else
|
2013-11-25 12:45:04 +00:00
|
|
|
wc_get_template( 'cart/cart.php' );
|
2012-12-31 18:25:09 +00:00
|
|
|
|
|
|
|
}
|
2014-02-16 18:53:55 +00:00
|
|
|
}
|