woocommerce/shortcodes/shortcode-cart.php

101 lines
2.7 KiB
PHP
Raw Normal View History

2011-08-10 17:11:11 +00:00
<?php
/**
* Cart Shortcode
2012-08-14 17:37:50 +00:00
*
2011-08-10 17:11:11 +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.
*
2012-08-14 17:37:50 +00:00
* @author WooThemes
* @category Shortcodes
* @package WooCommerce/Shortcodes/Cart
* @version 1.6.4
*/
/**
* Get the cart shortcode content.
*
* @access public
* @param array $atts
* @return string
2011-08-10 17:11:11 +00:00
*/
function get_woocommerce_cart( $atts ) {
global $woocommerce;
2012-08-14 17:37:50 +00:00
return $woocommerce->shortcode_wrapper( 'woocommerce_cart', $atts );
2011-08-10 17:11:11 +00:00
}
2012-08-14 17:37:50 +00:00
/**
* Output the cart shortcode.
*
* @access public
* @param array $atts
* @return void
*/
2011-08-10 17:11:11 +00:00
function woocommerce_cart( $atts ) {
global $woocommerce;
2012-08-14 17:37:50 +00:00
2012-02-24 21:05:15 +00:00
$woocommerce->nocache();
2012-08-14 17:37:50 +00:00
2012-06-10 11:40:07 +00:00
if ( ! defined( 'WOOCOMMERCE_CART' ) ) define( 'WOOCOMMERCE_CART', true );
2012-08-14 17:37:50 +00:00
2012-06-10 11:40:07 +00:00
// Add Discount
if ( ! empty( $_POST['apply_coupon'] ) ) {
2012-08-14 17:37:50 +00:00
2012-06-10 11:40:07 +00:00
if ( ! empty( $_POST['coupon_code'] ) ) {
$woocommerce->cart->add_discount( stripslashes( trim( $_POST['coupon_code'] ) ) );
} else {
2012-08-14 17:37:50 +00:00
$woocommerce->add_error( __('Please enter a coupon code.', 'woocommerce') );
2012-06-10 11:40:07 +00:00
}
2012-08-14 17:37:50 +00:00
// Remove Discount Codes
2012-06-10 11:40:07 +00:00
} elseif ( isset( $_GET['remove_discounts'] ) ) {
2012-08-14 17:37:50 +00:00
$woocommerce->cart->remove_coupons( $_GET['remove_discounts'] );
2012-08-14 17:37:50 +00:00
2011-08-10 17:11:11 +00:00
// Update Shipping
2012-06-10 11:40:07 +00:00
} elseif ( ! empty( $_POST['calc_shipping'] ) && $woocommerce->verify_nonce('cart') ) {
2012-08-14 17:37:50 +00:00
$validation = $woocommerce->validation();
2012-08-14 17:37:50 +00:00
2012-09-07 18:28:27 +00:00
$woocommerce->customer->calculated_shipping( true );
2012-09-07 17:26:13 +00:00
unset( $woocommerce->session->chosen_shipping_method );
2011-08-10 17:11:11 +00:00
$country = $_POST['calc_shipping_country'];
$state = $_POST['calc_shipping_state'];
$postcode = $_POST['calc_shipping_postcode'];
2012-08-14 17:37:50 +00:00
if ( $postcode && ! $validation->is_postcode( $postcode, $country ) ) {
$woocommerce->add_error( __('Please enter a valid postcode/ZIP.', 'woocommerce') );
2011-08-10 17:11:11 +00:00
$postcode = '';
2012-08-14 17:37:50 +00:00
} elseif ( $postcode ) {
$postcode = $validation->format_postcode( $postcode, $country );
2012-08-14 17:37:50 +00:00
}
if ( $country ) {
2011-08-10 17:11:11 +00:00
// Update customer location
$woocommerce->customer->set_location( $country, $state, $postcode );
$woocommerce->customer->set_shipping_location( $country, $state, $postcode );
2012-01-05 11:31:22 +00:00
$woocommerce->add_message( __('Shipping costs updated.', 'woocommerce') );
2012-08-14 17:37:50 +00:00
} else {
2012-02-18 10:08:12 +00:00
$woocommerce->customer->set_to_base();
$woocommerce->customer->set_shipping_to_base();
2012-01-05 11:31:22 +00:00
$woocommerce->add_message( __('Shipping costs updated.', 'woocommerce') );
2012-08-14 17:37:50 +00:00
}
do_action( 'woocommerce_calculated_shipping' );
2012-06-10 11:40:07 +00:00
}
2012-08-14 17:37:50 +00:00
// Check cart items are valid
do_action('woocommerce_check_cart_items');
2012-08-14 17:37:50 +00:00
2012-01-14 16:42:04 +00:00
// Calc totals
$woocommerce->cart->calculate_totals();
2012-08-14 17:37:50 +00:00
if ( sizeof( $woocommerce->cart->get_cart() ) == 0 )
woocommerce_get_template( 'cart/empty.php' );
2012-08-14 17:37:50 +00:00
else
woocommerce_get_template( 'cart/cart.php' );
2012-08-14 17:37:50 +00:00
2011-08-10 17:11:11 +00:00
}