2011-08-10 17:11:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Shopping Cart Widget
|
|
|
|
*
|
|
|
|
* Displays shopping cart widget
|
|
|
|
*
|
|
|
|
* @package WooCommerce
|
|
|
|
* @category Widgets
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
class WooCommerce_Widget_Cart extends WP_Widget {
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2011-08-26 21:28:55 +00:00
|
|
|
/** Variables to setup the widget. */
|
|
|
|
var $woo_widget_cssclass;
|
|
|
|
var $woo_widget_description;
|
|
|
|
var $woo_widget_idbase;
|
|
|
|
var $woo_widget_name;
|
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
/** constructor */
|
|
|
|
function WooCommerce_Widget_Cart() {
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2011-08-26 21:28:55 +00:00
|
|
|
/* Widget variable settings. */
|
|
|
|
$this->woo_widget_cssclass = 'widget_shopping_cart';
|
2012-01-05 11:31:22 +00:00
|
|
|
$this->woo_widget_description = __( "Display the user's Shopping Cart in the sidebar.", 'woocommerce' );
|
2011-08-26 21:28:55 +00:00
|
|
|
$this->woo_widget_idbase = 'woocommerce_shopping_cart';
|
2012-01-05 11:31:22 +00:00
|
|
|
$this->woo_widget_name = __('WooCommerce Shopping Cart', 'woocommerce' );
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2011-08-26 21:28:55 +00:00
|
|
|
/* Widget settings. */
|
|
|
|
$widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2011-08-26 21:28:55 +00:00
|
|
|
/* Create the widget. */
|
|
|
|
$this->WP_Widget('shopping_cart', $this->woo_widget_name, $widget_ops);
|
2011-08-10 17:11:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
/** @see WP_Widget */
|
2011-08-10 17:11:11 +00:00
|
|
|
function widget( $args, $instance ) {
|
2011-09-06 11:11:22 +00:00
|
|
|
global $woocommerce;
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2012-06-10 14:48:52 +00:00
|
|
|
if ( is_cart() || is_checkout() ) return;
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
extract($args);
|
2012-06-10 14:48:52 +00:00
|
|
|
if ( ! empty( $instance['title'] ) ) $title = $instance['title']; else $title = __('Cart', 'woocommerce');
|
2011-08-10 17:11:11 +00:00
|
|
|
$title = apply_filters('widget_title', $title, $instance, $this->id_base);
|
2012-06-10 14:48:52 +00:00
|
|
|
$hide_if_empty = isset( $instance['hide_if_empty'] ) && $instance['hide_if_empty'] ? '1' : '0';
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
echo $before_widget;
|
|
|
|
if ( $title ) echo $before_title . $title . $after_title;
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2012-02-27 11:04:09 +00:00
|
|
|
echo '<ul class="cart_list product_list_widget ';
|
|
|
|
if ($hide_if_empty) echo 'hide_cart_widget_if_empty';
|
|
|
|
echo '">';
|
2012-06-10 14:48:52 +00:00
|
|
|
|
|
|
|
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
|
|
|
|
|
|
|
|
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
|
2012-06-23 14:19:15 +00:00
|
|
|
|
|
|
|
if ( apply_filters('woocommerce_widget_cart_hide_item', 'show', $cart_item, $cart_item_key) == 'hide' ) continue;
|
|
|
|
|
2011-12-15 11:21:06 +00:00
|
|
|
$_product = $cart_item['data'];
|
2012-06-10 14:48:52 +00:00
|
|
|
|
|
|
|
if ( $_product->exists() && $cart_item['quantity'] > 0 ) {
|
|
|
|
|
2011-12-15 11:21:06 +00:00
|
|
|
echo '<li><a href="'.get_permalink($cart_item['product_id']).'">';
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2011-12-15 11:21:06 +00:00
|
|
|
echo $_product->get_image();
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2012-06-10 14:48:52 +00:00
|
|
|
echo apply_filters('woocommerce_cart_widget_product_title', $_product->get_title(), $_product) . '</a>';
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2011-12-15 11:21:06 +00:00
|
|
|
echo $woocommerce->cart->get_item_data( $cart_item );
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2012-06-10 14:48:52 +00:00
|
|
|
$product_price = get_option('woocommerce_display_cart_prices_excluding_tax') == 'yes' || $woocommerce->customer->is_vat_exempt() ? $_product->get_price_excluding_tax() : $_product->get_price();
|
|
|
|
|
|
|
|
$product_price = apply_filters('woocommerce_cart_item_price_html', woocommerce_price( $product_price ), $cart_item, $cart_item_key );
|
|
|
|
|
|
|
|
echo '<span class="quantity">' . $cart_item['quantity'] . ' × ' . $product_price . '</span></li>';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
echo '<li class="empty">' . __('No products in the cart.', 'woocommerce') . '</li>';
|
|
|
|
}
|
2011-08-10 17:11:11 +00:00
|
|
|
echo '</ul>';
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2012-06-10 14:48:52 +00:00
|
|
|
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
|
|
|
|
|
|
|
|
echo '<p class="total"><strong>' . __('Subtotal', 'woocommerce') . ':</strong> '. $woocommerce->cart->get_cart_subtotal() . '</p>';
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
do_action( 'woocommerce_widget_shopping_cart_before_buttons' );
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2012-06-10 14:48:52 +00:00
|
|
|
echo '<p class="buttons"><a href="' . $woocommerce->cart->get_cart_url() . '" class="button">' . __('View Cart →', 'woocommerce') . '</a> <a href="' . $woocommerce->cart->get_checkout_url() . '" class="button checkout">' . __('Checkout →', 'woocommerce') . '</a></p>';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
echo $after_widget;
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2012-06-10 14:48:52 +00:00
|
|
|
if ( $hide_if_empty && sizeof( $woocommerce->cart->get_cart() ) == 0 ) {
|
2012-02-27 11:04:09 +00:00
|
|
|
$inline_js = "
|
2012-03-18 19:09:05 +00:00
|
|
|
jQuery('.hide_cart_widget_if_empty').closest('.widget').hide();
|
2012-02-27 11:04:09 +00:00
|
|
|
jQuery('body').bind('adding_to_cart', function(){
|
2012-03-18 19:09:05 +00:00
|
|
|
jQuery(this).find('.hide_cart_widget_if_empty').closest('.widget').fadeIn();
|
2012-02-27 11:04:09 +00:00
|
|
|
});
|
|
|
|
";
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2012-02-27 11:04:09 +00:00
|
|
|
$woocommerce->add_inline_js( $inline_js );
|
|
|
|
}
|
2011-08-10 17:11:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
/** @see WP_Widget->update */
|
2011-08-10 17:11:11 +00:00
|
|
|
function update( $new_instance, $old_instance ) {
|
|
|
|
$instance['title'] = strip_tags(stripslashes($new_instance['title']));
|
2011-12-19 00:45:24 +00:00
|
|
|
$instance['hide_if_empty'] = !empty($new_instance['hide_if_empty']) ? 1 : 0;
|
2011-08-10 17:11:11 +00:00
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
/** @see WP_Widget->form */
|
2011-08-10 17:11:11 +00:00
|
|
|
function form( $instance ) {
|
2011-12-19 00:45:24 +00:00
|
|
|
$hide_if_empty = isset( $instance['hide_if_empty'] ) ? (bool) $instance['hide_if_empty'] : false;
|
|
|
|
?>
|
2012-01-05 11:31:22 +00:00
|
|
|
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'woocommerce') ?></label>
|
2011-12-19 00:45:24 +00:00
|
|
|
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id('title') ); ?>" name="<?php echo esc_attr( $this->get_field_name('title') ); ?>" value="<?php if (isset ( $instance['title'])) {echo esc_attr( $instance['title'] );} ?>" /></p>
|
2012-03-16 22:09:11 +00:00
|
|
|
|
2011-12-19 00:45:24 +00:00
|
|
|
<p><input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id('hide_if_empty') ); ?>" name="<?php echo esc_attr( $this->get_field_name('hide_if_empty') ); ?>"<?php checked( $hide_if_empty ); ?> />
|
2012-01-05 11:31:22 +00:00
|
|
|
<label for="<?php echo $this->get_field_id('hide_if_empty'); ?>"><?php _e( 'Hide if cart is empty', 'woocommerce' ); ?></label></p>
|
2011-12-19 00:45:24 +00:00
|
|
|
<?php
|
2011-08-10 17:11:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // class WooCommerce_Widget_Cart
|