2013-05-24 15:51:58 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* List products. One widget to rule them all.
|
|
|
|
*
|
2014-11-15 01:12:59 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category Widgets
|
|
|
|
* @package WooCommerce/Widgets
|
|
|
|
* @version 2.3.0
|
|
|
|
* @extends WC_Widget
|
2013-05-24 15:51:58 +00:00
|
|
|
*/
|
|
|
|
|
2014-09-20 19:37:45 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
|
|
|
class WC_Widget_Products extends WC_Widget {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->widget_cssclass = 'woocommerce widget_products';
|
|
|
|
$this->widget_description = __( 'Display a list of your products on your site.', 'woocommerce' );
|
|
|
|
$this->widget_id = 'woocommerce_products';
|
|
|
|
$this->widget_name = __( 'WooCommerce Products', 'woocommerce' );
|
|
|
|
$this->settings = array(
|
|
|
|
'title' => array(
|
|
|
|
'type' => 'text',
|
|
|
|
'std' => __( 'Products', 'woocommerce' ),
|
|
|
|
'label' => __( 'Title', 'woocommerce' )
|
|
|
|
),
|
|
|
|
'number' => array(
|
|
|
|
'type' => 'number',
|
|
|
|
'step' => 1,
|
|
|
|
'min' => 1,
|
|
|
|
'max' => '',
|
|
|
|
'std' => 5,
|
|
|
|
'label' => __( 'Number of products to show', 'woocommerce' )
|
|
|
|
),
|
|
|
|
'show' => array(
|
|
|
|
'type' => 'select',
|
|
|
|
'std' => '',
|
|
|
|
'label' => __( 'Show', 'woocommerce' ),
|
|
|
|
'options' => array(
|
|
|
|
'' => __( 'All Products', 'woocommerce' ),
|
|
|
|
'featured' => __( 'Featured Products', 'woocommerce' ),
|
|
|
|
'onsale' => __( 'On-sale Products', 'woocommerce' ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'orderby' => array(
|
|
|
|
'type' => 'select',
|
|
|
|
'std' => 'date',
|
|
|
|
'label' => __( 'Order by', 'woocommerce' ),
|
|
|
|
'options' => array(
|
|
|
|
'date' => __( 'Date', 'woocommerce' ),
|
|
|
|
'price' => __( 'Price', 'woocommerce' ),
|
|
|
|
'rand' => __( 'Random', 'woocommerce' ),
|
|
|
|
'sales' => __( 'Sales', 'woocommerce' ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'order' => array(
|
|
|
|
'type' => 'select',
|
|
|
|
'std' => 'desc',
|
2013-10-11 13:59:52 +00:00
|
|
|
'label' => _x( 'Order', 'Sorting order', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
'options' => array(
|
|
|
|
'asc' => __( 'ASC', 'woocommerce' ),
|
|
|
|
'desc' => __( 'DESC', 'woocommerce' ),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'hide_free' => array(
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'std' => 0,
|
|
|
|
'label' => __( 'Hide free products', 'woocommerce' )
|
|
|
|
),
|
|
|
|
'show_hidden' => array(
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'std' => 0,
|
|
|
|
'label' => __( 'Show hidden products', 'woocommerce' )
|
|
|
|
)
|
|
|
|
);
|
2014-11-15 01:12:59 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-29 11:07:02 +00:00
|
|
|
* Query the products and return them
|
|
|
|
* @param array $args
|
|
|
|
* @param array $instance
|
|
|
|
* @return WP_Query
|
2013-05-24 15:51:58 +00:00
|
|
|
*/
|
2014-10-29 11:07:02 +00:00
|
|
|
public function get_products( $args, $instance ) {
|
2014-11-15 01:12:59 +00:00
|
|
|
$number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std'];
|
|
|
|
$show = ! empty( $instance['show'] ) ? sanitize_title( $instance['show'] ) : $this->settings['show']['std'];
|
|
|
|
$orderby = ! empty( $instance['orderby'] ) ? sanitize_title( $instance['orderby'] ) : $this->settings['orderby']['std'];
|
|
|
|
$order = ! empty( $instance['order'] ) ? sanitize_title( $instance['order'] ) : $this->settings['order']['std'];
|
|
|
|
|
|
|
|
$query_args = array(
|
|
|
|
'posts_per_page' => $number,
|
2014-10-29 11:07:02 +00:00
|
|
|
'post_status' => 'publish',
|
|
|
|
'post_type' => 'product',
|
|
|
|
'no_found_rows' => 1,
|
2014-11-15 01:12:59 +00:00
|
|
|
'order' => $order,
|
2014-10-29 11:07:02 +00:00
|
|
|
'meta_query' => array()
|
2014-11-15 01:12:59 +00:00
|
|
|
);
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
if ( empty( $instance['show_hidden'] ) ) {
|
2013-11-25 14:01:32 +00:00
|
|
|
$query_args['meta_query'][] = WC()->query->visibility_meta_query();
|
2013-05-24 15:51:58 +00:00
|
|
|
$query_args['post_parent'] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $instance['hide_free'] ) ) {
|
2014-11-15 01:12:59 +00:00
|
|
|
$query_args['meta_query'][] = array(
|
|
|
|
'key' => '_price',
|
|
|
|
'value' => 0,
|
|
|
|
'compare' => '>',
|
|
|
|
'type' => 'DECIMAL',
|
2013-05-24 15:51:58 +00:00
|
|
|
);
|
2014-11-15 01:12:59 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$query_args['meta_query'][] = WC()->query->stock_status_meta_query();
|
|
|
|
$query_args['meta_query'] = array_filter( $query_args['meta_query'] );
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
switch ( $show ) {
|
|
|
|
case 'featured' :
|
|
|
|
$query_args['meta_query'][] = array(
|
2013-05-24 15:51:58 +00:00
|
|
|
'key' => '_featured',
|
|
|
|
'value' => 'yes'
|
|
|
|
);
|
2014-11-15 01:12:59 +00:00
|
|
|
break;
|
|
|
|
case 'onsale' :
|
2014-10-29 11:07:02 +00:00
|
|
|
$product_ids_on_sale = wc_get_product_ids_on_sale();
|
|
|
|
$product_ids_on_sale[] = 0;
|
2013-05-24 15:51:58 +00:00
|
|
|
$query_args['post__in'] = $product_ids_on_sale;
|
2014-11-15 01:12:59 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
switch ( $orderby ) {
|
2013-05-24 15:51:58 +00:00
|
|
|
case 'price' :
|
|
|
|
$query_args['meta_key'] = '_price';
|
2014-11-15 01:12:59 +00:00
|
|
|
$query_args['orderby'] = 'meta_value_num';
|
2013-05-24 15:51:58 +00:00
|
|
|
break;
|
|
|
|
case 'rand' :
|
2014-11-15 01:12:59 +00:00
|
|
|
$query_args['orderby'] = 'rand';
|
2013-05-24 15:51:58 +00:00
|
|
|
break;
|
|
|
|
case 'sales' :
|
|
|
|
$query_args['meta_key'] = 'total_sales';
|
2014-11-15 01:12:59 +00:00
|
|
|
$query_args['orderby'] = 'meta_value_num';
|
2013-05-24 15:51:58 +00:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
$query_args['orderby'] = 'date';
|
2014-11-15 01:12:59 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-10-29 11:07:02 +00:00
|
|
|
return new WP_Query( $query_args );
|
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-10-29 11:07:02 +00:00
|
|
|
/**
|
|
|
|
* widget function.
|
|
|
|
*
|
|
|
|
* @see WP_Widget
|
|
|
|
* @access public
|
|
|
|
* @param array $args
|
|
|
|
* @param array $instance
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function widget( $args, $instance ) {
|
|
|
|
if ( $this->get_cached_widget( $args ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-10-29 11:07:02 +00:00
|
|
|
ob_start();
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-10-29 11:07:02 +00:00
|
|
|
if ( ( $products = $this->get_products( $args, $instance ) ) && $products->have_posts() ) {
|
|
|
|
$this->widget_start( $args, $instance );
|
2013-05-24 15:51:58 +00:00
|
|
|
|
|
|
|
echo '<ul class="product_list_widget">';
|
|
|
|
|
2014-10-29 11:07:02 +00:00
|
|
|
while ( $products->have_posts() ) {
|
|
|
|
$products->the_post();
|
|
|
|
wc_get_template( 'content-widget-product.php', array( 'show_rating' => false ) );
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
echo '</ul>';
|
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$this->widget_end( $args );
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wp_reset_postdata();
|
|
|
|
|
2014-10-29 11:07:02 +00:00
|
|
|
echo $this->cache_widget( $args, ob_get_clean() );
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
}
|