How to add a custom product page dropdown option in front end shop or categories pages in woocommerce wordpress

How to give a option to customer for display product as they want
For add a custom dropdown on shop or category page , paste below code to your functions.php file of your current active theme.

// below action add a new custom dropdown box to shop or category page
add_action( 'woocommerce_before_shop_loop', 'rj_product_per_page_dropdown', 25 );

function rj_product_per_page_dropdown()
{
    global $wp_query,$woocommerce;
        $action = '';
        $cat     = '';
        $cat     = $wp_query->get_queried_object();
        $query_string = ! empty( $_SERVER['QUERY_STRING'] ) ? '?' . add_query_arg( array( 'rj_pro_page' => false ), $_SERVER['QUERY_STRING'] ) : null;
        if ( isset( $cat->term_id ) && isset( $cat->taxonomy ) ) :
            $action = get_term_link( $cat->term_id, $cat->taxonomy ) . $query_string;
        else:
            $action = get_permalink( woocommerce_get_page_id( 'shop' ) ) . $query_string;
        endif;
        $products_per_page_options = array(12,24,36,72);
    ?>
    <form method="post" action="<?php echo esc_url( $action ); ?>"  class="custom form-wppp-select products-per-page">
                <div class="select-wrapper">
                <input type="hidden" name="product_per_nonce" id="product_per_nonce" value="<?php echo wp_create_nonce('nonce_for_product_page'); ?>" />
                <select name="rj_pro_page" onchange="this.form.submit()" class="select wppp-select"><?php
                $default_posts_per_page = get_option( 'posts_per_page' );
                    foreach( $products_per_page_options as $key => $value ) :
                        // Get the right match for the selected option
                       
                        if ( isset( $_POST['rj_pro_page'] ) ) :
                            $selected_match = $_POST['rj_pro_page'];
                        elseif ( isset( $_GET['rj_pro_page'] ) ):
                            $selected_match = $_GET['rj_pro_page'];
                        else :
                            $selected_match = $default_posts_per_page;
                        endif;
                        ?><option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $selected_match ); ?>><?php
                            $ppp_text = apply_filters( 'rj_pro_page_text', __( '%s products per page', 'woocommerce-products-per-page' ), $value );
                            printf( $ppp_text, $value == -1 ? __( 'All', 'woocommerce-products-per-page' ) : $value ); // Set to 'All' when value is -1
                        ?></option><?php
                    endforeach;
                ?></select>
                </div>
               </form>
    <?php
}
// below action will display product as customer want
add_filter( 'loop_shop_per_page', 'rj_loop_shop_per_page'  );
add_action( 'woocommerce_product_query', 'rj_pre_get_posts' , 2, 50 );

function rj_pre_get_posts( $q, $class ) {
        if ( function_exists( 'woocommerce_products_will_display' ) && woocommerce_products_will_display() && $q->is_main_query() && ! is_admin() ) :
            $q->set( 'posts_per_page', rj_loop_shop_per_page() );
        endif;
}

function rj_loop_shop_per_page() {
        global $woocommerce;
        $default_posts_per_page = get_option( 'posts_per_page' );
        if ( isset( $_POST['rj_pro_page'] ) ) :
            return $_POST['rj_pro_page'];
        elseif ( isset( $_GET['rj_pro_page'] ) ) :
            return $_GET['rj_pro_page'];
        else :
            return $default_posts_per_page;
        endif;
}

Comments

Popular posts from this blog

How to add a custom sorting or order by option on category archive or product archive page in woocommerce wordpress

How to create a custom wp_list_table and bulk action in wordpress

How to add image option in nav menu in wordpress