'adela-widget-product-carousel', 'description' => esc_html__( 'Adela widget products by carousel.', 'adela' ), ); parent::__construct( 'adela_widget_product_carousel', esc_html__( 'Adela: Product Carousel', 'adela' ), $widget_ops ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); } function enqueue_scripts(){ wp_enqueue_style ( 'owl-carousel' ); wp_enqueue_script( 'owl-carousel' ); } function widget( $args, $instance ){ $instance = wp_parse_args( $instance, array( 'block_title' => 'Product Items', 'carousel_item_lg' => 3, 'carousel_item_md' => 3, 'carousel_item_sm' => 2, 'carousel_item_xs' => 2, 'carousel_item' => 1, 'number_post' => 3, 'carousel_margin' => 0, 'thumbnail_width' => 270, 'thumbnail_height' => 270, 'product_target' => 'recent-product', 'product_order' => 'ASC', 'product_orderby' => 'ID' ) ); $carousel_data = array( 'data-lg="' . $instance['carousel_item_lg'] .'"', 'data-md="' . $instance['carousel_item_md'] .'"', 'data-sm="' . $instance['carousel_item_sm'] .'"', 'data-xs="' . $instance['carousel_item_xs'] .'"', 'data-ts="' . $instance['carousel_item'] .'"', 'data-margin="' . $instance['carousel_margin'] .'"', ); $meta_query = WC()->query->get_meta_query(); $tax_query = WC()->query->get_tax_query(); $args = array( 'post_type' => 'product', 'posts_per_page' => $instance[ 'number_post' ], 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'order' => $instance[ 'product_order' ], 'orderby' => $instance[ 'product_orderby' ], 'meta_query' => $meta_query, 'tax_query' => $tax_query, ); if( $instance['product_target'] == 'best-selling' ){ $args[ 'meta_key' ] = 'total_sales'; $args[ 'orderby' ] = 'meta_value_num'; }elseif( $instance['product_target'] == 'top-rated' ){ $ordering_args = WC()->query->get_catalog_ordering_args(); $args[ 'orderby' ] = $ordering_args[ 'orderby' ]; $args[ 'order' ] = $ordering_args[ 'order' ]; }elseif( $instance['product_target'] == 'featured_products' ){ $tax_query = WC()->query->get_tax_query(); $tax_query[] = array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', 'operator' => 'IN', ); $args[ 'tax_query' ] = $tax_query; }elseif( $instance['product_target'] == 'on_new' ){ $args[ 'date_query' ] = array( array( 'after' => '7 days ago', 'inclusive' => true, ), ); }elseif( $instance['product_target'] == 'on_sale' ){ $product_ids_on_sale = wc_get_product_ids_on_sale(); $args[ 'post__in' ] = array_merge( array( 0 ), $product_ids_on_sale ); }else{ $ordering_args = WC()->query->get_catalog_ordering_args(); if ( isset( $ordering_args[ 'meta_key' ] ) ) { $args[ 'meta_key' ] = $ordering_args[ 'meta_key' ]; } WC()->query->remove_ordering_args(); } $loop_posts = new WP_Query( apply_filters( 'adela_widget_product_carousel_query_args', $args ) ); if ( $loop_posts->have_posts() ){ ?>