2013-05-24 15:51:58 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* List products. One widget to rule them all.
|
|
|
|
*
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\Widgets
|
2018-03-09 20:41:07 +00:00
|
|
|
* @version 3.3.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Widget products.
|
2013-05-24 15:51:58 +00:00
|
|
|
*/
|
|
|
|
class WC_Widget_Products extends WC_Widget {
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Constructor.
|
2013-05-24 15:51:58 +00:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->widget_cssclass = 'woocommerce widget_products';
|
2017-08-25 11:07:17 +00:00
|
|
|
$this->widget_description = __( "A list of your store's products.", 'woocommerce' );
|
2013-05-24 15:51:58 +00:00
|
|
|
$this->widget_id = 'woocommerce_products';
|
2021-05-19 13:49:29 +00:00
|
|
|
$this->widget_name = __( 'Products list', 'woocommerce' );
|
2013-05-24 15:51:58 +00:00
|
|
|
$this->settings = array(
|
2018-03-09 20:41:07 +00:00
|
|
|
'title' => array(
|
2013-05-24 15:51:58 +00:00
|
|
|
'type' => 'text',
|
|
|
|
'std' => __( 'Products', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Title', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
2018-03-09 20:41:07 +00:00
|
|
|
'number' => array(
|
2013-05-24 15:51:58 +00:00
|
|
|
'type' => 'number',
|
|
|
|
'step' => 1,
|
|
|
|
'min' => 1,
|
|
|
|
'max' => '',
|
|
|
|
'std' => 5,
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Number of products to show', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
2018-03-09 20:41:07 +00:00
|
|
|
'show' => array(
|
|
|
|
'type' => 'select',
|
|
|
|
'std' => '',
|
|
|
|
'label' => __( 'Show', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
'options' => array(
|
2016-10-12 10:16:30 +00:00
|
|
|
'' => __( 'All products', 'woocommerce' ),
|
|
|
|
'featured' => __( 'Featured products', 'woocommerce' ),
|
|
|
|
'onsale' => __( 'On-sale products', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
2018-03-09 20:41:07 +00:00
|
|
|
'orderby' => array(
|
|
|
|
'type' => 'select',
|
|
|
|
'std' => 'date',
|
|
|
|
'label' => __( 'Order by', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
'options' => array(
|
2018-03-09 20:41:07 +00:00
|
|
|
'date' => __( 'Date', 'woocommerce' ),
|
|
|
|
'price' => __( 'Price', 'woocommerce' ),
|
|
|
|
'rand' => __( 'Random', 'woocommerce' ),
|
|
|
|
'sales' => __( 'Sales', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
2018-03-09 20:41:07 +00:00
|
|
|
'order' => array(
|
|
|
|
'type' => 'select',
|
|
|
|
'std' => 'desc',
|
|
|
|
'label' => _x( 'Order', 'Sorting order', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
'options' => array(
|
|
|
|
'asc' => __( 'ASC', 'woocommerce' ),
|
|
|
|
'desc' => __( 'DESC', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
2018-03-09 20:41:07 +00:00
|
|
|
'hide_free' => array(
|
2013-05-24 15:51:58 +00:00
|
|
|
'type' => 'checkbox',
|
|
|
|
'std' => 0,
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Hide free products', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
|
|
|
'show_hidden' => array(
|
|
|
|
'type' => 'checkbox',
|
|
|
|
'std' => 0,
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Show hidden products', 'woocommerce' ),
|
|
|
|
),
|
2013-05-24 15:51:58 +00:00
|
|
|
);
|
2014-11-15 01:12:59 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Query the products and return them.
|
2018-03-09 20:41:07 +00:00
|
|
|
*
|
2018-12-14 18:50:58 +00:00
|
|
|
* @param array $args Arguments.
|
|
|
|
* @param array $instance Widget instance.
|
|
|
|
*
|
2014-10-29 11:07:02 +00:00
|
|
|
* @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 ) {
|
2018-03-09 20:41:07 +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'];
|
2016-12-08 17:01:39 +00:00
|
|
|
$orderby = ! empty( $instance['orderby'] ) ? sanitize_title( $instance['orderby'] ) : $this->settings['orderby']['std'];
|
2018-03-09 20:41:07 +00:00
|
|
|
$order = ! empty( $instance['order'] ) ? sanitize_title( $instance['order'] ) : $this->settings['order']['std'];
|
2016-12-08 17:01:39 +00:00
|
|
|
$product_visibility_term_ids = wc_get_product_visibility_term_ids();
|
2014-11-15 01:12:59 +00:00
|
|
|
|
|
|
|
$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,
|
2016-08-27 01:46:45 +00:00
|
|
|
'meta_query' => array(),
|
2016-12-08 10:56:45 +00:00
|
|
|
'tax_query' => array(
|
|
|
|
'relation' => 'AND',
|
|
|
|
),
|
2018-03-09 20:41:07 +00:00
|
|
|
); // WPCS: slow query ok.
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
if ( empty( $instance['show_hidden'] ) ) {
|
2016-12-08 10:56:45 +00:00
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
2016-12-08 17:01:39 +00:00
|
|
|
'field' => 'term_taxonomy_id',
|
|
|
|
'terms' => is_search() ? $product_visibility_term_ids['exclude-from-search'] : $product_visibility_term_ids['exclude-from-catalog'],
|
2016-12-08 10:56:45 +00:00
|
|
|
'operator' => 'NOT IN',
|
|
|
|
);
|
2018-03-09 20:41:07 +00:00
|
|
|
$query_args['post_parent'] = 0;
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-12-08 10:56:45 +00:00
|
|
|
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
|
2018-12-14 18:52:26 +00:00
|
|
|
$query_args['tax_query'][] = array(
|
2016-12-08 10:56:45 +00:00
|
|
|
array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
2016-12-08 17:01:39 +00:00
|
|
|
'field' => 'term_taxonomy_id',
|
|
|
|
'terms' => $product_visibility_term_ids['outofstock'],
|
2016-12-08 10:56:45 +00:00
|
|
|
'operator' => 'NOT IN',
|
|
|
|
),
|
2018-03-09 20:41:07 +00:00
|
|
|
); // WPCS: slow query ok.
|
2016-12-08 10:56:45 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
switch ( $show ) {
|
2018-03-09 20:41:07 +00:00
|
|
|
case 'featured':
|
2016-12-08 10:56:45 +00:00
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
2016-12-08 17:01:39 +00:00
|
|
|
'field' => 'term_taxonomy_id',
|
|
|
|
'terms' => $product_visibility_term_ids['featured'],
|
2013-05-24 15:51:58 +00:00
|
|
|
);
|
2014-11-15 01:12:59 +00:00
|
|
|
break;
|
2018-03-09 20:41:07 +00:00
|
|
|
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 ) {
|
2018-03-09 20:41:07 +00:00
|
|
|
case 'price':
|
|
|
|
$query_args['meta_key'] = '_price'; // WPCS: slow query ok.
|
2014-11-15 01:12:59 +00:00
|
|
|
$query_args['orderby'] = 'meta_value_num';
|
2013-05-24 15:51:58 +00:00
|
|
|
break;
|
2018-03-09 20:41:07 +00:00
|
|
|
case 'rand':
|
|
|
|
$query_args['orderby'] = 'rand';
|
2013-05-24 15:51:58 +00:00
|
|
|
break;
|
2018-03-09 20:41:07 +00:00
|
|
|
case 'sales':
|
|
|
|
$query_args['meta_key'] = 'total_sales'; // WPCS: slow query ok.
|
2014-11-15 01:12:59 +00:00
|
|
|
$query_args['orderby'] = 'meta_value_num';
|
2013-05-24 15:51:58 +00:00
|
|
|
break;
|
2018-03-09 20:41:07 +00:00
|
|
|
default:
|
|
|
|
$query_args['orderby'] = 'date';
|
2014-11-15 01:12:59 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2015-04-29 14:36:27 +00:00
|
|
|
return new WP_Query( apply_filters( 'woocommerce_products_widget_query_args', $query_args ) );
|
2014-10-29 11:07:02 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-10-29 11:07:02 +00:00
|
|
|
/**
|
2016-01-06 18:58:38 +00:00
|
|
|
* Output widget.
|
2014-10-29 11:07:02 +00:00
|
|
|
*
|
2018-03-09 20:41:07 +00:00
|
|
|
* @param array $args Arguments.
|
|
|
|
* @param array $instance Widget instance.
|
2018-12-14 18:50:58 +00:00
|
|
|
*
|
|
|
|
* @see WP_Widget
|
2014-10-29 11:07:02 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
2021-05-06 15:22:58 +00:00
|
|
|
wc_set_loop_prop( 'name', 'widget' );
|
|
|
|
|
2018-03-09 20:41:07 +00:00
|
|
|
$products = $this->get_products( $args, $instance );
|
|
|
|
if ( $products && $products->have_posts() ) {
|
2014-10-29 11:07:02 +00:00
|
|
|
$this->widget_start( $args, $instance );
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2017-10-27 14:35:34 +00:00
|
|
|
echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ) );
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2017-10-03 20:52:36 +00:00
|
|
|
$template_args = array(
|
2019-08-01 17:43:59 +00:00
|
|
|
'widget_id' => isset( $args['widget_id'] ) ? $args['widget_id'] : $this->widget_id,
|
2017-10-27 14:35:34 +00:00
|
|
|
'show_rating' => true,
|
2017-10-03 20:52:36 +00:00
|
|
|
);
|
|
|
|
|
2014-10-29 11:07:02 +00:00
|
|
|
while ( $products->have_posts() ) {
|
|
|
|
$products->the_post();
|
2017-10-03 20:52:36 +00:00
|
|
|
wc_get_template( 'content-widget-product.php', $template_args );
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 14:35:34 +00:00
|
|
|
echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ) );
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$this->widget_end( $args );
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wp_reset_postdata();
|
|
|
|
|
2018-03-09 20:41:07 +00:00
|
|
|
echo $this->cache_widget( $args, ob_get_clean() ); // WPCS: XSS ok.
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
}
|