Plugin Directory

Changeset 3415876


Ignore:
Timestamp:
12/09/2025 11:24:17 PM (4 months ago)
Author:
razorfrog
Message:

Product Dropdown Widget for WooCommerce v1.1.4

Location:
woo-product-dropdown-widget
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • woo-product-dropdown-widget/trunk/readme.txt

    r2983983 r3415876  
    11=== Product Dropdown Widget for WooCommerce ===
    2 Contributors: Razorfrog
    3 Tags: woocommerce, widget, categories, products
    4 Donate link: https://razorfrog.com/
    5 Requires at least: 3.1
    6 Tested up to: 6.3.2
    7 Stable tag: 1.1.3
     2Contributors: razorfrog
     3Tags: woocommerce, woo, widget, categories, products
     4Requires at least: 5.2
     5Tested up to: 6.9
     6Requires PHP: 7.2
     7Stable tag: 1.1.4
    88License: GPLv2 or later
    9 License URI: http://www.gnu.org/licenses/gpl-2.0.html
     9License URI: https://www.gnu.org/licenses/gpl-2.0.html
    1010
    11 Display and link to WooCommerce products in a dropdown menu widget. Allows for display of all products or products in individual categories as well as multiple sorting methods including price and most reviewed.
     11Dropdown widget for WooCommerce products with category selection and sorting by price, reviews, or other criteria.
    1212
    1313== Description ==
    14 This plugin creates a widget to display and link to WooCommerce products by category in a dropdown menu. Visitors to your site will have one-click access to products in your sidebar (or other widgetized areas) without valuable real estate being used up by a regular products list. 
     14This plugin creates a widget to display and link to WooCommerce products by category in a dropdown menu. Visitors to your site will have one-click access to products in your sidebar (or other widgetized areas) without valuable real estate being used up by a regular products list.
    1515
    1616**Plugin options include the abilities to:**
     
    2222This plugin is also compatible with the [Post Types Order](https://wordpress.org/plugins/post-types-order/) plugin, so you can also sort by your own customized menu order if desired.
    2323
    24 With these options you can use this plugin for a variety of uses. You could use multiple instances of the widget to have product dropdowns for each of your categories; you could display the top 5 most frequently bought products in a specific category; you could display the top 20 most reviewed products in your entire store or just in a specific category; and so on. 
     24With these options you can use this plugin for a variety of uses. You could use multiple instances of the widget to have product dropdowns for each of your categories; you could display the top 5 most frequently bought products in a specific category; you could display the top 20 most reviewed products in your entire store or just in a specific category; and so on.
    2525
    2626**Note**
     
    3838
    3939= Why are my dropdowns out of order when using the [Post Types Order](https://wordpress.org/plugins/post-types-order/) plugin? =
    40 The Post Types Order Plugin defaults to overriding any wp_query to sort by menu order.  Visit Settings > Post Types Order and uncheck the Auto Sort option to avoid this problem. 
     40The Post Types Order Plugin defaults to overriding any wp_query to sort by menu order.  Visit Settings > Post Types Order and uncheck the Auto Sort option to avoid this problem.
    4141
    4242== Screenshots ==
     
    4545
    4646== Changelog ==
     47
     481.1.4
     49
     50* Security updates
     51* WP Core compatibility update
    4752
    48531.1.3
     
    61661.1.0
    6267
    63 * UI improvements 
     68* UI improvements
    6469* WP Core compatibility update
    6570
  • woo-product-dropdown-widget/trunk/woo-product-dropdown-widget.php

    r2983983 r3415876  
    11<?php
    2 /*
    3 Plugin Name: Woo Product Dropdown Widget
    4 Plugin URI: https://razorfrog.com/plugin-release-woocommerce-dropdown-widget/
    5 Description: Display WooCommerce products by category in a dropdown menu widget
    6 Version: 1.1.3
    7 Author: Razorfrog Web Design
    8 Author URI: https://razorfrog.com/
    9 License: GPLv2 or later
    10 License URI: http://www.gnu.org/licenses/gpl-2.0.html
    11 */
     2
     3/**
     4 * Plugin Name:       Product Dropdown Widget for WooCommerce
     5 * Plugin URI:        https://razorfrog.com/plugin-release-woocommerce-dropdown-widget/
     6 * Description:       Display WooCommerce products by category in a dropdown menu widget.
     7 * Version:           1.1.4
     8 * Requires at least: 5.2
     9 * Requires PHP:      7.2
     10 * Author:            Razorfrog Web Design
     11 * Author URI:        https://razorfrog.com/
     12 * License:           GPLv2 or later
     13 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
     14 * Text Domain:       woo-product-dropdown-widget
     15 */
    1216
    1317class rz_woo_category_widget extends WP_Widget {
     
    1620        parent::__construct(
    1721            'rz_woo_category_widget', // Base ID
    18             __( 'WooCommerce Product Dropdown', 'text_domain' ), // Name
    19             array( 'description' => __( 'Display products from a specific product category as dropdown.', 'text_domain' ), ) // Args
    20         );
    21     }
    22    
     22            __( 'WooCommerce Product Dropdown', 'woo-product-dropdown-widget' ), // Name
     23            array(
     24                'description' => __( 'Display products from a specific product category as dropdown.', 'woo-product-dropdown-widget' ),
     25            )
     26        );
     27    }
     28
    2329    ////////////////////////////////////////////////////////////////////////////////
    2430    // Widget frontend
     
    2632
    2733    public function widget( $args, $instance ) {
    28    
    29         extract( $args );
    30         echo $before_widget;       
    31    
    32         $title = $instance['title'];
    33         $show_posts = (is_numeric($instance['show_posts'])) ? $instance['show_posts'] : -1;
    34        
     34
     35        extract( $args );
     36        echo wp_kses_post( $before_widget );
     37
     38        $title        = isset( $instance['title'] ) ? $instance['title'] : '';
     39        $show_posts   = ( isset( $instance['show_posts'] ) && is_numeric( $instance['show_posts'] ) ) ? $instance['show_posts'] : -1;
     40        $sort         = isset( $instance['sort'] ) ? $instance['sort'] : '';
     41        $order        = isset( $instance['order'] ) ? $instance['order'] : '';
     42        $cat_dropdown = isset( $instance['cat_dropdown'] ) ? (int) $instance['cat_dropdown'] : 0;
     43
    3544        $args = array( // defaults to all categories
    36                 'post_type' => 'product',
    37                 'orderby' => $instance['sort'],
    38                 'order' => $instance['order'],
    39                 'numberposts' => $show_posts
    40             );
    41         $product_cat = "Products";
    42        
    43         if ($instance['cat_dropdown'] != 0) { // single product category
     45            'post_type'   => 'product',
     46            'orderby'     => $sort,
     47            'order'       => $order,
     48            'numberposts' => $show_posts,
     49        );
     50
     51        $product_cat = 'Products';
     52
     53        if ( 0 !== $cat_dropdown ) { // single product category
    4454            $args['tax_query'] = array(
    4555                array(
    4656                    'taxonomy' => 'product_cat',
    4757                    'field'    => 'id',
    48                     'terms'    => $instance['cat_dropdown'],
     58                    'terms'    => $cat_dropdown,
    4959                ),
    5060            );
    51             $product_cat = get_cat_name($instance['cat_dropdown']);
     61            $product_cat = get_cat_name( $cat_dropdown );
    5262        }
    53        
    54         if ($instance['sort'] == "price") { // order by price
    55             $args['orderby'] = 'meta_value_num';
     63
     64        if ( 'price' === $sort ) { // order by price
     65            $args['orderby']  = 'meta_value_num';
    5666            $args['meta_key'] = '_price';
    57         } else if ($instance['sort'] == "sales") { // order by total sales
    58             $args['orderby'] = 'meta_value_num';
     67        } elseif ( 'sales' === $sort ) { // order by total sales
     68            $args['orderby']  = 'meta_value_num';
    5969            $args['meta_key'] = 'total_sales';
    6070        }
    61        
    62         $posts = get_posts($args);
    63        
    64         if ($posts) {
    65             echo '<div class="'.$this->widget_options['classname'].'-content">';
    66                    
    67             if ( ! empty( $instance['title'] ) ) {
    68                 echo $before_title . apply_filters( 'widget_title', $instance['title'] ) . $after_title;
     71
     72        $posts = get_posts( $args );
     73
     74        if ( $posts ) {
     75            echo '<div class="' . esc_attr( $this->widget_options['classname'] ) . '-content">';
     76
     77            if ( ! empty( $title ) ) {
     78                // Filter then escape the widget title.
     79                $filtered_title = apply_filters( 'widget_title', $title );
     80                echo wp_kses_post( $before_title ) . esc_html( $filtered_title ) . wp_kses_post( $after_title );
    6981            }
    70            
     82
    7183            echo '<select class="category-posts-dropdown" onchange="location = this.value;" style="width:100%">';
    72             echo '<option value="">View '. $product_cat . '</option>';
    73            
    74             foreach ($posts as $post) {
    75                
    76                 $additional = "";
    77            
     84            echo '<option value="">' . esc_html( 'View ' . $product_cat ) . '</option>';
     85
     86            foreach ( $posts as $post ) {
     87
     88                $additional = '';
     89
    7890                // If sorted by reviews, show (count)
    79                 if (($instance['sort'] == "comment_count") && ($post->comment_count == 1)) {
    80                     $additional = " (".$post->comment_count." Review)";
    81                 } else if (($instance['sort'] == "comment_count") && ($post->comment_count > 1)) {
    82                     $additional = " (".$post->comment_count." Reviews)";
     91                if ( 'comment_count' === $sort && 1 === (int) $post->comment_count ) {
     92                    $additional = ' (' . absint( $post->comment_count ) . ' ' . __( 'Review', 'woo-product-dropdown-widget' ) . ')';
     93                } elseif ( 'comment_count' === $sort && (int) $post->comment_count > 1 ) {
     94                    $additional = ' (' . absint( $post->comment_count ) . ' ' . __( 'Reviews', 'woo-product-dropdown-widget' ) . ')';
    8395                }
     96                ?>
     97                <option value="<?php echo esc_url( get_permalink( $post->ID ) ); ?>">
     98                    <?php echo esc_html( $post->post_title . $additional ); ?>
     99                </option>
     100                <?php
     101            }
     102
     103            echo '</select>';
     104            echo '</div>';
     105        }
     106
     107        echo wp_kses_post( $after_widget );
     108    }
     109
     110    ////////////////////////////////////////////////////////////////////////////////
     111    // Widget backend - admin dashboard
     112    ////////////////////////////////////////////////////////////////////////////////
     113
     114    public function form( $instance ) {
     115        $defaults = array(
     116            'cat_dropdown' => '',
     117            'title'        => '',
     118            'sort'         => 'title',
     119            'order'        => 'ASC',
     120            'show_posts'   => -1,
     121        );
     122
     123        $instance     = wp_parse_args( (array) $instance, $defaults );
     124        $cat_dropdown = isset( $instance['cat_dropdown'] ) ? $instance['cat_dropdown'] : '';
     125
     126        $product_categories_dropdown = wp_dropdown_categories(
     127            array(
     128                'taxonomy'        => 'product_cat',
     129                'orderby'         => 'slug',
     130                'hierarchical'    => true,
     131                'echo'            => false,
     132                'show_option_all' => __( 'All Products', 'woo-product-dropdown-widget' ),
     133                'show_count'      => true,
     134                'selected'        => $cat_dropdown,
     135                'name'            => $this->get_field_name( 'cat_dropdown' ),
     136                'id'              => $this->get_field_id( 'cat_dropdown' ),
     137                'class'           => 'widefat',
     138            )
     139        );
     140        ?>
     141
     142        <p>
     143            <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
     144                <?php esc_html_e( 'Title:', 'woo-product-dropdown-widget' ); ?>
     145            </label>
     146            <input type="text"
     147                class="widefat"
     148                id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
     149                name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
     150                value="<?php echo esc_attr( $instance['title'] ); ?>" />
     151        </p>
     152
     153        <p>
     154            <label for="<?php echo esc_attr( $this->get_field_id( 'cat_dropdown' ) ); ?>">
     155                <?php esc_html_e( 'Category:', 'woo-product-dropdown-widget' ); ?>
     156            </label>
     157            <?php
     158            // Markup is generated by wp_dropdown_categories() and is safe in the admin.
     159            echo $product_categories_dropdown; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    84160            ?>
    85                 <option value="<?php echo get_permalink($post->ID);?>"><?php echo $post->post_title . $additional;?></option>
    86             <?php }
    87                
    88             echo '</select>';
    89         }
    90        
    91         echo '</div>';
    92         echo $after_widget; 
    93     }
    94    
    95     ////////////////////////////////////////////////////////////////////////////////
    96     // Widget backend - admin dashboard
    97     ////////////////////////////////////////////////////////////////////////////////
    98 
    99     public function form( $instance ) {
    100         $defaults = array(
    101             'cat_dropdown' => '', // Set the default value for cat_dropdown
    102             'title' => '',       // Set default values for other fields as needed
    103             'sort' => 'title',   // Default sort value
    104             'order' => 'ASC',    // Default order value
    105             'show_posts' => -1,  // Default show_posts value
    106         );
    107        
    108         $instance = wp_parse_args( (array) $instance, $defaults );
    109         $cat_dropdown = isset( $instance['cat_dropdown'] ) ? $instance['cat_dropdown'] : '';
    110        
    111         $product_categories_dropdown = wp_dropdown_categories( array(
    112             'taxonomy' => 'product_cat',
    113             'orderby' => 'slug',
    114             'hierarchical' => true,
    115             'echo' => false,
    116             'show_option_all' => 'Select Category',
    117             'show_count' => true,
    118             'selected' => $instance['cat_dropdown'],
    119             'name' => $this->get_field_name('cat_dropdown'),
    120             'id' => $this->get_field_id('cat_dropdown'),
    121             'class' => 'widefat',
    122             'show_option_all' => 'All Products',
    123         )); ?>
    124        
    125         <p>
    126             <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
    127             <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
    128         </p>
    129        
    130         <p>
    131             <label for="<?php echo $this->get_field_id('cat_dropdown'); ?>"><?php _e('Category:'); ?></label>
    132             <?php echo $product_categories_dropdown;  ?>
    133         </p>   
    134        
    135         <p>
    136             <label for="<?php echo $this->get_field_id('sort'); ?>"><?php _e('Sort by:'); ?></label>
    137             <select class="widefat" id="<?php echo $this->get_field_id('sort'); ?>" name="<?php echo $this->get_field_name('sort'); ?>">
    138                 <?php
    139                 echo '<option'. selected( $instance['sort'], 'title' ) .' value="title">Product Name</option>';
    140                 echo '<option'. selected( $instance['sort'], 'menu_order' ) .' value="menu_order">Menu Order</option>';
    141                 echo '<option'. selected( $instance['sort'], 'price' ) .' value="price">Price</option>';
    142                 echo '<option'. selected( $instance['sort'], 'sales' ) .' value="sales">Total Sales</option>';
    143                 echo '<option'. selected( $instance['sort'], 'date' ) .' value="date">Date Published</option>';
    144                 echo '<option'. selected( $instance['sort'], 'modified' ) .' value="modified">Date Last Modified</option>';
    145                 echo '<option'. selected( $instance['sort'], 'comment_count' ) .' value="comment_count">Reviews</option>';
    146                 echo '<option'. selected( $instance['sort'], 'rand' ) .' value="rand">Random</option>';
    147                 ?>
     161        </p>
     162
     163        <p>
     164            <label for="<?php echo esc_attr( $this->get_field_id( 'sort' ) ); ?>">
     165                <?php esc_html_e( 'Sort by:', 'woo-product-dropdown-widget' ); ?>
     166            </label>
     167            <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'sort' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'sort' ) ); ?>">
     168                <option value="title" <?php selected( $instance['sort'], 'title' ); ?>>
     169                    <?php esc_html_e( 'Product Name', 'woo-product-dropdown-widget' ); ?>
     170                </option>
     171                <option value="menu_order" <?php selected( $instance['sort'], 'menu_order' ); ?>>
     172                    <?php esc_html_e( 'Menu Order', 'woo-product-dropdown-widget' ); ?>
     173                </option>
     174                <option value="price" <?php selected( $instance['sort'], 'price' ); ?>>
     175                    <?php esc_html_e( 'Price', 'woo-product-dropdown-widget' ); ?>
     176                </option>
     177                <option value="sales" <?php selected( $instance['sort'], 'sales' ); ?>>
     178                    <?php esc_html_e( 'Total Sales', 'woo-product-dropdown-widget' ); ?>
     179                </option>
     180                <option value="date" <?php selected( $instance['sort'], 'date' ); ?>>
     181                    <?php esc_html_e( 'Date Published', 'woo-product-dropdown-widget' ); ?>
     182                </option>
     183                <option value="modified" <?php selected( $instance['sort'], 'modified' ); ?>>
     184                    <?php esc_html_e( 'Date Last Modified', 'woo-product-dropdown-widget' ); ?>
     185                </option>
     186                <option value="comment_count" <?php selected( $instance['sort'], 'comment_count' ); ?>>
     187                    <?php esc_html_e( 'Reviews', 'woo-product-dropdown-widget' ); ?>
     188                </option>
     189                <option value="rand" <?php selected( $instance['sort'], 'rand' ); ?>>
     190                    <?php esc_html_e( 'Random', 'woo-product-dropdown-widget' ); ?>
     191                </option>
    148192            </select>
    149193        </p>
    150        
    151         <p>
    152             <label for="<?php echo $this->get_field_id('order'); ?>"><?php _e('Sort Order:'); ?></label>
    153             <select class="widefat" id="<?php echo $this->get_field_id('order'); ?>" name="<?php echo $this->get_field_name('order'); ?>">
    154                 <?php
    155                 echo '<option'. selected( $instance['order'], 'ASC' ) .' value="ASC">Ascending (A > Z)</option>';
    156                 echo '<option'. selected( $instance['order'], 'DESC' ) .' value="DESC">Descending (Z > A)</option>';
    157                 ?>
     194
     195        <p>
     196            <label for="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>">
     197                <?php esc_html_e( 'Sort Order:', 'woo-product-dropdown-widget' ); ?>
     198            </label>
     199            <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>">
     200                <option value="ASC" <?php selected( $instance['order'], 'ASC' ); ?>>
     201                    <?php esc_html_e( 'Ascending (A > Z)', 'woo-product-dropdown-widget' ); ?>
     202                </option>
     203                <option value="DESC" <?php selected( $instance['order'], 'DESC' ); ?>>
     204                    <?php esc_html_e( 'Descending (Z > A)', 'woo-product-dropdown-widget' ); ?>
     205                </option>
    158206            </select>
    159207        </p>
    160        
    161         <p>
    162             <label for="<?php echo $this->get_field_id( 'show_posts' ); ?>">Number of Products (Defaults to All):</label>
    163             <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'show_posts' ); ?>" name="<?php echo $this->get_field_name( 'show_posts' ); ?>" value="<?php echo $instance['show_posts']; ?>" />
    164         </p><?php
     208
     209        <p>
     210            <label for="<?php echo esc_attr( $this->get_field_id( 'show_posts' ) ); ?>">
     211                <?php esc_html_e( 'Number of Products (Defaults to All):', 'woo-product-dropdown-widget' ); ?>
     212            </label>
     213            <input type="text"
     214                class="widefat"
     215                id="<?php echo esc_attr( $this->get_field_id( 'show_posts' ) ); ?>"
     216                name="<?php echo esc_attr( $this->get_field_name( 'show_posts' ) ); ?>"
     217                value="<?php echo esc_attr( $instance['show_posts'] ); ?>" />
     218        </p>
     219        <?php
    165220    }
    166221
     
    168223    // Sanitize widget form values as they are saved
    169224    ////////////////////////////////////////////////////////////////////////////////
    170    
     225
    171226    public function update( $new_instance, $old_instance ) {
    172         $instance = array();
    173         $instance['title'] = $new_instance['title'];
    174         $instance['show_posts'] = sanitize_text_field($new_instance['show_posts']);
    175         $instance['sort'] = sanitize_text_field($new_instance['sort']);
    176         $instance['order'] = sanitize_text_field($new_instance['order']);
    177         $instance['cat_dropdown'] = (int) $new_instance['cat_dropdown'];
     227        $instance                 = array();
     228        $instance['title']        = isset( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '';
     229        $instance['show_posts']   = isset( $new_instance['show_posts'] ) ? sanitize_text_field( $new_instance['show_posts'] ) : '';
     230        $instance['sort']         = isset( $new_instance['sort'] ) ? sanitize_text_field( $new_instance['sort'] ) : '';
     231        $instance['order']        = isset( $new_instance['order'] ) ? sanitize_text_field( $new_instance['order'] ) : '';
     232        $instance['cat_dropdown'] = isset( $new_instance['cat_dropdown'] ) ? (int) $new_instance['cat_dropdown'] : 0;
    178233        return $instance;
    179234    }
     
    182237
    183238add_action( 'widgets_init', 'register_rz_woo_dropdown_widget' );
    184 function register_rz_woo_dropdown_widget() {;
     239function register_rz_woo_dropdown_widget() {
    185240    register_widget( 'rz_woo_category_widget' );
    186241}
Note: See TracChangeset for help on using the changeset viewer.