• Никакой политики на форуме. Иначе - бан!
  • Вопрос без рабочей ссылки на проблему считается риторическим. Без ссылки и скриншота - провокацией!
  • Темы озаглавленные с маленькой буквы или капсом удаляются без предупреждения!

Виджет с новыми товарами из той же категории

p0zitiv

Новичок
Подскажите как сделать, чтобы стандартный виджет на странице товара отображал только новые товары из той же категории?

Так же у стандартного виджета есть функция отображения случайных товаров, но в среди них попадаются товары которых нет на складе. Как запретить виджету их отображение?

Код виджета следующий:

<?php

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* List products. One widget to rule them all.
*
* @author WooThemes
* @category Widgets
* @package WooCommerce/Widgets
* @version 2.3.0
* @extends WC_Widget
*/
class WC_Widget_Products extends WC_Widget {

/**
* Constructor.
*/
public function __construct() {
$this->widget_cssclass = 'woocommerce widget_products';
$this->widget_description = __( "A list of your store's products.", 'woocommerce' );
$this->widget_id = 'woocommerce_products';
$this->widget_name = __( '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(
'rand' => __( 'Random', 'woocommerce' ),
'date' => __( 'Date', 'woocommerce' ),
'price' => __( 'Price', 'woocommerce' ),
'sales' => __( 'Sales', 'woocommerce' ),
),
),
'order' => array(
'type' => 'select',
'std' => 'desc',
'label' => _x( 'Order', 'Sorting order', 'woocommerce' ),
'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' ),
),
);

parent::__construct();
}

/**
* Query the products and return them.
* @param array $args
* @param array $instance
* @return WP_Query
*/
public function get_products( $args, $instance ) {
$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'];
$product_visibility_term_ids = wc_get_product_visibility_term_ids();

$query_args = array(
'posts_per_page' => $number,
'post_status' => 'publish',
'post_type' => 'product',
'no_found_rows' => 1,
'order' => $order,
'meta_query' => array(),
'tax_query' => array(
'relation' => 'AND',
),
);

if ( empty( $instance['show_hidden'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => is_search() ? $product_visibility_term_ids['exclude-from-search'] : $product_visibility_term_ids['exclude-from-catalog'],
'operator' => 'NOT IN',
);
$query_args['post_parent'] = 0;
}

if ( ! empty( $instance['hide_free'] ) ) {
$query_args['meta_query'][] = array(
'key' => '_price',
'value' => 0,
'compare' => '>',
'type' => 'DECIMAL',
);
}

if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_term_ids['outofstock'],
'operator' => 'NOT IN',
),
);
}

switch ( $show ) {
case 'featured' :
$query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_term_ids['featured'],
);
break;
case 'onsale' :
$product_ids_on_sale = wc_get_product_ids_on_sale();
$product_ids_on_sale[] = 0;
$query_args['post__in'] = $product_ids_on_sale;
break;
}

switch ( $orderby ) {
case 'price' :
$query_args['meta_key'] = '_price';
$query_args['orderby'] = 'meta_value_num';
break;
case 'rand' :
$query_args['orderby'] = 'rand';
break;
case 'sales' :
$query_args['meta_key'] = 'total_sales';
$query_args['orderby'] = 'meta_value_num';
break;
default :
$query_args['orderby'] = 'date';
}

return new WP_Query( apply_filters( 'woocommerce_products_widget_query_args', $query_args ) );
}

/**
* Output widget.
*
* @see WP_Widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
if ( $this->get_cached_widget( $args ) ) {
return;
}

ob_start();

if ( ( $products = $this->get_products( $args, $instance ) ) && $products->have_posts() ) {
$this->widget_start( $args, $instance );

echo apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' );

while ( $products->have_posts() ) {
$products->the_post();
wc_get_template( 'content-widget-product.php', array( 'show_rating' => false ) );
}

echo apply_filters( 'woocommerce_after_widget_product_list', '</ul>' );

$this->widget_end( $args );
}

wp_reset_postdata();

echo $this->cache_widget( $args, ob_get_clean() );
}
}
 

p0zitiv

Новичок
С помощью кода ниже получается сделать так, чтобы товары в садбаре были из одной определенной категории, которую я задам вручную. Как сделать, чтобы 'terms' брал значение их текущей рубрики, в которой товар находится?

$query_args = array(
'posts_per_page' => $number,
'post_status' => 'publish',
'post_type' => 'product',
'no_found_rows' => 1,
'order' => $order,
'meta_query' => array(),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => '274',
),
),
);
 

p0zitiv

Новичок
Вроде бы так заработало, может кому пригодиться:

$query_args = array(
'posts_per_page' => $number,
'post_status' => 'publish',
'post_type' => 'product',
'no_found_rows' => 1,
'order' => $order,
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
),
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => wc_get_product_term_ids( $product_id, 'product_cat' ),
),
),
);
 

tuxfighter

Гуру
Местный
надеюсь вы ясно понимаете, что весь ваш титанический труд ровно до первого обновления ВУУ?
 

p0zitiv

Новичок
Меня это устраивает на 100%

Если есть другие варианты, с радостью выслушаю.
 

tuxfighter

Гуру
Местный
Если есть другие варианты, с радостью выслушаю.
создать свой, отдельный плагин с нужным виджетом.
И обновлять его когда он становится не совместимым с текущей версией ВУУ.
Возможно даже выложить его в оф. репо.
Только там ответственности нужно больше.
 
Сверху Снизу