__( 'The Product Slide Choose your type', 'fashstore' ) ) ); } /** * Helper function that holds widget fields * Array is used in update and form functions */ private function widget_fields() { $prod_type = array( 'category' => __( 'Category', 'fashstore' ), 'latest_product' => __( 'Latest Product', 'fashstore' ), 'upsell_product' => __( 'UpSell Product', 'fashstore' ), 'feature_product' => __( 'Feature Product', 'fashstore' ), 'on_sale' => __( 'On Sale Product', 'fashstore' ), ); $taxonomy = 'product_cat'; $empty = 1; $orderby = 'name'; $show_count = 0; // 1 for yes, 0 for no $pad_counts = 0; // 1 for yes, 0 for no $hierarchical = 1; // 1 for yes, 0 for no $title = ''; $empty = 0; $args = array( 'taxonomy' => $taxonomy, 'orderby' => $orderby, 'show_count' => $show_count, 'pad_counts' => $pad_counts, 'hierarchical' => $hierarchical, 'title_li' => $title, 'hide_empty' => $empty ); $woocommerce_categories = array(); $woocommerce_categories_obj = get_categories($args); $woocommerce_categories[''] = 'Select Product Category:'; foreach ($woocommerce_categories_obj as $category) { $woocommerce_categories[$category->term_id] = $category->name; } $fields = array( 'product_title' => array( 'accesspress_store_widgets_name' => 'product_title', 'accesspress_store_widgets_title' => __( 'Title', 'fashstore' ), 'accesspress_store_widgets_field_type' => 'text', ), 'product_type' => array( 'accesspress_store_widgets_name' => 'product_type', 'accesspress_store_widgets_title' => __( 'Select Product Type', 'fashstore' ), 'accesspress_store_widgets_field_type' => 'select', 'accesspress_store_widgets_field_options' => $prod_type ), 'product_category' => array( 'accesspress_store_widgets_name' => 'product_category', 'accesspress_store_widgets_title' => __( 'Select Product Category', 'fashstore' ), 'accesspress_store_widgets_field_type' => 'select', 'accesspress_store_widgets_field_options' => $woocommerce_categories ), 'product_number' => array( 'accesspress_store_widgets_name' => 'number_of_prod', 'accesspress_store_widgets_title' => __( 'Select the number of Product to show', 'fashstore' ), 'accesspress_store_widgets_field_type' => 'number', ), ); return $fields; } public function widget($args, $instance){ extract($args); $product_title = $instance['product_title']; $product_type = $instance['product_type']; $product_category = $instance['product_category']; $product_number = $instance['number_of_prod']; $product_label_custom = ''; $product_args = ''; if($product_type == 'category'){ $product_args = array( 'post_type' => 'product', 'tax_query' => array(array('taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $product_category )), 'posts_per_page' => $product_number ); } elseif($product_type == 'latest_product'){ $product_label_custom = __('New!', 'fashstore' ); $product_args = array( 'post_type' => 'product', 'posts_per_page' => $product_number ); } elseif($product_type == 'upsell_product'){ $product_args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'meta_key' => 'total_sales', 'orderby' => 'meta_value_num', 'posts_per_page' => $product_number ); } elseif($product_type == 'feature_product'){ $product_args = array( 'post_type' => 'product', 'meta_key' => '_featured', 'meta_value' => 'yes', 'posts_per_page' => $product_number ); } elseif($product_type == 'on_sale'){ $product_args = array( 'post_type' => 'product', 'meta_query' => array( 'relation' => 'OR', array( // Simple products type 'key' => '_sale_price', 'value' => 0, 'compare' => '>', 'type' => 'numeric' ), array( // Variable products type 'key' => '_min_variation_sale_price', 'value' => 0, 'compare' => '>', 'type' => 'numeric' ) ) ); } ?>
widget_fields(); // Loop through fields foreach ($widget_fields as $widget_field) { extract($widget_field); // Use helper function to get updated field values $instance[$accesspress_store_widgets_name] = accesspress_store_widgets_updated_field_value($widget_field, $new_instance[$accesspress_store_widgets_name]); } return $instance; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. * * @uses accesspress_store_widgets_show_widget_field() defined in widget-fields.php */ public function form($instance) { $widget_fields = $this->widget_fields(); // Loop through fields foreach ($widget_fields as $widget_field) { // Make array elements available as variables extract($widget_field); $accesspress_store_widgets_field_value = !empty($instance[$accesspress_store_widgets_name]) ? esc_attr($instance[$accesspress_store_widgets_name]) : ''; accesspress_store_widgets_show_widget_field($this, $widget_field, $accesspress_store_widgets_field_value); } } }