Minor tweaks to cart shortcode
This commit is contained in:
parent
416d167842
commit
1c9331737e
|
@ -77,11 +77,14 @@ class WC_Shortcodes {
|
||||||
* Cart page shortcode.
|
* Cart page shortcode.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param mixed $atts
|
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function cart( $atts ) {
|
public static function cart() {
|
||||||
return self::shortcode_wrapper( array( 'WC_Shortcode_Cart', 'output' ), $atts );
|
if ( ! is_null( WC()->cart ) ) {
|
||||||
|
return self::shortcode_wrapper( array( 'WC_Shortcode_Cart', 'output' ) );
|
||||||
|
} else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -7,22 +7,53 @@
|
||||||
* @author WooThemes
|
* @author WooThemes
|
||||||
* @category Shortcodes
|
* @category Shortcodes
|
||||||
* @package WooCommerce/Shortcodes/Cart
|
* @package WooCommerce/Shortcodes/Cart
|
||||||
* @version 2.0.0
|
* @version 2.3.0
|
||||||
*/
|
*/
|
||||||
class WC_Shortcode_Cart {
|
class WC_Shortcode_Cart {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Output the cart shortcode.
|
* Calculate shipping for the cart
|
||||||
*
|
|
||||||
* @param array $atts
|
|
||||||
*/
|
*/
|
||||||
public static function output( $atts ) {
|
public static function calculate_shipping() {
|
||||||
|
try {
|
||||||
|
WC()->shipping->reset_shipping();
|
||||||
|
|
||||||
// Check cart class is loaded or abort
|
$country = wc_clean( $_POST['calc_shipping_country'] );
|
||||||
if ( is_null( WC()->cart ) ) {
|
$state = wc_clean( isset( $_POST['calc_shipping_state'] ) ? $_POST['calc_shipping_state'] : '' );
|
||||||
return;
|
$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' );
|
||||||
|
|
||||||
|
do_action( 'woocommerce_calculated_shipping' );
|
||||||
|
|
||||||
|
} catch ( Exception $e ) {
|
||||||
|
if ( ! empty( $e ) ) {
|
||||||
|
wc_add_notice( $e->getMessage(), 'error' );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Output the cart shortcode.
|
||||||
|
*/
|
||||||
|
public static function output() {
|
||||||
// Constants
|
// Constants
|
||||||
if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
|
if ( ! defined( 'WOOCOMMERCE_CART' ) ) {
|
||||||
define( 'WOOCOMMERCE_CART', true );
|
define( 'WOOCOMMERCE_CART', true );
|
||||||
|
@ -30,52 +61,19 @@ class WC_Shortcode_Cart {
|
||||||
|
|
||||||
// Update Shipping
|
// Update Shipping
|
||||||
if ( ! empty( $_POST['calc_shipping'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) {
|
if ( ! empty( $_POST['calc_shipping'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-cart' ) ) {
|
||||||
|
self::calculate_shipping();
|
||||||
try {
|
|
||||||
WC()->shipping->reset_shipping();
|
|
||||||
|
|
||||||
$country = wc_clean( $_POST['calc_shipping_country'] );
|
|
||||||
$state = isset( $_POST['calc_shipping_state'] ) ? wc_clean( $_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' );
|
|
||||||
|
|
||||||
do_action( 'woocommerce_calculated_shipping' );
|
|
||||||
|
|
||||||
} catch ( Exception $e ) {
|
|
||||||
|
|
||||||
if ( ! empty( $e ) )
|
|
||||||
wc_add_notice( $e->getMessage(), 'error' );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check cart items are valid
|
// Check cart items are valid
|
||||||
do_action('woocommerce_check_cart_items');
|
do_action( 'woocommerce_check_cart_items' );
|
||||||
|
|
||||||
// Calc totals
|
// Calc totals
|
||||||
WC()->cart->calculate_totals();
|
WC()->cart->calculate_totals();
|
||||||
|
|
||||||
if ( sizeof( WC()->cart->get_cart() ) == 0 )
|
if ( 0 === sizeof( WC()->cart->get_cart() ) ) {
|
||||||
wc_get_template( 'cart/cart-empty.php' );
|
wc_get_template( 'cart/cart-empty.php' );
|
||||||
else
|
} else {
|
||||||
wc_get_template( 'cart/cart.php' );
|
wc_get_template( 'cart/cart.php' );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue