Total_sales / Best sellers widget

This commit is contained in:
Mike Jolley 2011-11-01 15:41:47 +00:00
parent 7891105ce1
commit c7300fd9d3
4 changed files with 157 additions and 2 deletions

View File

@ -379,7 +379,15 @@ class woocommerce_order {
clean_term_cache( '', 'shop_order_status' );
// Date
if ($new_status->slug=='completed') update_post_meta( $this->id, '_completed_date', current_time('mysql') );
if ($new_status->slug=='completed') :
update_post_meta( $this->id, '_completed_date', current_time('mysql') );
endif;
// Sales
if ($this->status == 'on-hold' && ($new_status->slug=='processing' || $new_status->slug=='completed')) :
$this->record_product_sales();
endif;
endif;
endif;
@ -405,6 +413,7 @@ class woocommerce_order {
* Most of the time this should mark an order as 'processing' so that admin can process/post the items
* If the cart contains only downloadable items then the order is 'complete' since the admin needs to take no action
* Stock levels are reduced at this point
* Sales are also recorded for products
*/
function payment_complete() {
@ -415,6 +424,7 @@ class woocommerce_order {
if (sizeof($this->items)>0) foreach ($this->items as $item) :
if ($item['id']>0) :
$_product = $this->get_product_from_item( $item );
if ( $_product->exists && $_product->is_type('downloadable') ) :
@ -439,11 +449,27 @@ class woocommerce_order {
$this->update_status($new_order_status);
// Payment is complete so reduce stock levels
// Payment is complete so reduce stock levels and record sales
$this->record_product_sales();
$this->reduce_order_stock();
}
/**
* Record sales
*/
function record_product_sales() {
if (sizeof($this->items)>0) foreach ($this->items as $item) :
if ($item['id']>0) :
$sales = (int) get_post_meta( $item['id'], 'total_sales', true );
$sales += (int) $item['qty'];
if ($sales) update_post_meta( $item['id'], 'total_sales', $sales );
endif;
endforeach;
}
/**
* Reduce stock levels
*/

View File

@ -91,6 +91,8 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
* Added basic rss feeds for products and product categories
* Added functions which show tax/vat conditionally
* Made use of transients to store average ratings and improve performance
* Custom field for product total_sales when sold
* Best sellers widget based on new total_sales field
* Edit category - image fix
* Order Complete email heading fix
* 100% discount when price excludes tax logic fix

View File

@ -0,0 +1,125 @@
<?php
/**
* Best Sellers Widget
*
* @package WooCommerce
* @category Widgets
* @author WooThemes
*/
class WooCommerce_Widget_Best_Sellers extends WP_Widget {
/** Variables to setup the widget. */
var $woo_widget_cssclass;
var $woo_widget_description;
var $woo_widget_idbase;
var $woo_widget_name;
/** constructor */
function WooCommerce_Widget_Best_Sellers() {
/* Widget variable settings. */
$this->woo_widget_cssclass = 'widget_best_sellers';
$this->woo_widget_description = __( 'Display a list of your best selling products on your site.', 'woothemes' );
$this->woo_widget_idbase = 'woocommerce_best_sellers';
$this->woo_widget_name = __('WooCommerce Best Sellers', 'woothemes' );
/* Widget settings. */
$widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
/* Create the widget. */
$this->WP_Widget('best_sellers', $this->woo_widget_name, $widget_ops);
add_action( 'save_post', array(&$this, 'flush_widget_cache') );
add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
}
/** @see WP_Widget */
function widget($args, $instance) {
global $woocommerce;
$cache = wp_cache_get('widget_best_sellers', 'widget');
if ( !is_array($cache) ) $cache = array();
if ( isset($cache[$args['widget_id']]) ) {
echo $cache[$args['widget_id']];
return;
}
ob_start();
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? __('Best Sellers', 'woothemes') : $instance['title'], $instance, $this->id_base);
if ( !$number = (int) $instance['number'] )
$number = 10;
else if ( $number < 1 )
$number = 1;
else if ( $number > 15 )
$number = 15;
$query_args = array(
'showposts' => $number,
'nopaging' => 0,
'post_status' => 'publish',
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value'
);
$r = new WP_Query($query_args);
if ($r->have_posts()) :
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<ul class="product_list_widget">
<?php while ($r->have_posts()) : $r->the_post(); $_product = &new woocommerce_product(get_the_ID()); ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
<?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'.$woocommerce->plugin_url().'/assets/images/placeholder.png" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'px" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'px" />'; ?>
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>
</a> <?php echo $_product->get_price_html(); ?></li>
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>
<?php
endif;
if (isset($args['widget_id']) && isset($cache[$args['widget_id']])) $cache[$args['widget_id']] = ob_get_flush();
wp_cache_set('widget_best_sellers', $cache, 'widget');
}
/** @see WP_Widget->update */
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['number'] = (int) $new_instance['number'];
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_best_sellers']) ) delete_option('widget_best_sellers');
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('widget_best_sellers', 'widget');
}
/** @see WP_Widget->form */
function form( $instance ) {
$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
if ( !isset($instance['number']) || !$number = (int) $instance['number'] ) $number = 5;
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'woothemes'); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('title') ); ?>" name="<?php echo esc_attr( $this->get_field_name('title') ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of products to show:', 'woothemes'); ?></label>
<input id="<?php echo esc_attr( $this->get_field_id('number') ); ?>" name="<?php echo esc_attr( $this->get_field_name('number') ); ?>" type="text" value="<?php echo esc_attr( $number ); ?>" size="3" /></p>
<?php
}
}

View File

@ -20,6 +20,7 @@ include_once('widget-recent_products.php');
include_once('widget-top_rated_products.php');
include_once('widget-recent_reviews.php');
include_once('widget-recently_viewed.php');
include_once('widget-best_sellers.php');
function woocommerce_register_widgets() {
register_widget('WooCommerce_Widget_Recent_Products');
@ -33,5 +34,6 @@ function woocommerce_register_widgets() {
register_widget('WooCommerce_Widget_Top_Rated_Products');
register_widget('WooCommerce_Widget_Recent_Reviews');
register_widget('WooCommerce_Widget_Recently_Viewed');
register_widget('WooCommerce_Widget_Best_Sellers');
}
add_action('widgets_init', 'woocommerce_register_widgets');