Woocommerce产品ID列表仅返回一个产品,而不是多个产品

时间:2019-01-31 09:17:41

标签: wordpress woocommerce

WooCommerce的特色产品小部件具有选定的ID,但只有一个产品退货。

我的目标是我有一些产品ID,例如1202,125,2152,885,1254,需要将它们显示为列表

我制作了一个metabox,可以在其中选择产品并获取ID并将其存储为$featured $featured返回的输出类似于1202,125,2152,885,1254

我使用$query_args['post__in'] = array($featured);,但结果仅输出一种产品,而不是所选的5种产品。

有人可以帮我吗?

完整代码

<?php
defined( 'ABSPATH' ) || exit;

class FPW_Widget_Featured_Products extends WC_Widget {


    public function __construct() {
        $this->widget_cssclass    = 'woocommerce widget_products';
        $this->widget_description = __( "A list of your store's products.", 'woocommerce' );
        $this->widget_id          = 'fpw_widget_featured_products';
        $this->widget_name        = __( 'FPW Featured Product', 'woocommerce' );
        $this->settings           = array(
            'title'       => array(
                'type'  => 'text',
                'std'   => __( 'Featured Product', 'woocommerce' ),
                'label' => __( 'Title', 'woocommerce' ),
            ),
            'number'      => array(
                'type'  => 'number',
                'step'  => 1,
                'min'   => 1,
                'max'   => 10,
                'std'   => 5,
                'label' => __( 'Number of products to show from selected', 'woocommerce' ),
            ),

        );
        parent::__construct();
    }
    /**
     * Query the products and return them.
     *
     * @param array $args     Arguments.
     * @param array $instance Widget instance.
     *
     * @return WP_Query
     */
    public function get_products( $args, $instance ) {
        global $post;
        $featured = get_post_meta($post->ID, '_blog_feature_product',true);
        $number              = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std'];
        $product_term_ids    = wc_get_product_visibility_term_ids();
        $query_args = array(
            'posts_per_page' => $number,
            'post_status'    => 'publish',
            'post_type'      => 'product',
            'meta_query'     => array(),
            'tax_query'      => array(
                'relation' => 'AND',
            ),
        ); // WPCS: slow query ok.

        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_term_ids['outofstock'],
                    'operator' => 'NOT IN',
                ),
            ); // WPCS: slow query ok.
        }

        if ( ! empty( $featured ) && is_array( $featured ) ) {
            $featured = implode( ',', array_map( 'absint', $featured ) );
            $query_args['post__in'] = array($featured);
        }else{
            $query_args['post__in'] = '';
        }   


        return new WP_Query($query_args);
    }
    /**
     * Output widget.
     *
     * @param array $args     Arguments.
     * @param array $instance Widget instance.
     *
     * @see WP_Widget
     */
    public function widget( $args, $instance ) {
        if ( $this->get_cached_widget( $args ) ) {
            return;
        }
        ob_start();
        $products = $this->get_products( $args, $instance );

        if ( $products && $products->have_posts() ) {
            $this->widget_start( $args, $instance );
            echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ) );
            $template_args = array(
                'widget_id'   => $args['widget_id'],
                'show_rating' => true,
            );
            while ( $products->have_posts() ) {
                $products->the_post();
                wc_get_template( 'content-widget-product.php', $template_args );
            }
            echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ) );
            $this->widget_end( $args );
        }
        wp_reset_postdata();
        echo $this->cache_widget( $args, ob_get_clean() ); // WPCS: XSS ok.
    }
}

function fpw_register_featured_products_widgets() {
    register_widget( 'FPW_Widget_Featured_Products' );
}
add_action( 'widgets_init', 'fpw_register_featured_products_widgets' );

1 个答案:

答案 0 :(得分:1)

如果在使用$featured时将值作为数组传递给$featured = get_post_meta($post->ID, '_blog_feature_product',true);变量 然后,您不需要使用此

$featured = implode( ',', array_map( 'absint', $featured ) ); 
$query_args['post__in'] = array($featured);

只需使用以下内容即可:

$query_args['post__in'] = $featured;

如果值以逗号分隔即$featured传递给$featured = "1,2,3,4,5";

然后使用此

$query_args['post__in'] = explode( ",", $featured);

现在应该可以正常工作了。