• Hi guys,

    I have wrote two hooks that filters 1.) stock status, 2.) visibility, in the back-end area.

    Can you please help me combining these two into one hook? I been struggling since 8’clock this morning. 🙁

    
    /* #### Nieuw filter #### */
     /* Filteren op gearchiveerde producten (back-end)*/
    add_action( 'restrict_manage_posts', 'filter002_admin_posts_filter_restrict_manage_posts' );
    
    function filter002_admin_posts_filter_restrict_manage_posts(){
    
        $type = 'product';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
    
        //only add filter to post type you want
        if ('product' == $type){
            //change this to the list of values you want to show
            //in 'label' => 'value' format
            $values = array(
                'Zichtbaar' => 'visible', 
                'Gearchiveerd' => 'hidden',
            );
            ?>
            <select name="Stock">
            <option value=""><?php _e('Archief', 'filter002'); ?></option>
            <?php
                $current_v = isset($_GET['Stock'])? $_GET['Stock']:'';
                foreach ($values as $label => $value) {
                    printf
                        (
                            '<option value="%s"%s>%s</option>',
                            $value,
                            $value == $current_v? ' selected="selected"':'',
                            $label
                        );
                    }
            ?>
            </select>
            <?php
        }
    }
    
    add_filter( 'parse_query', 'filter002_posts_filter' );
    
    function filter002_posts_filter( $query ){
        global $pagenow;
        $type = 'product';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
        if ( 'product' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['Stock']) && $_GET['Stock'] != '') {
            $query->query_vars['meta_key'] = '_visibility';
            $query->query_vars['meta_value'] = $_GET['Stock'];
        }
    }
    /* #### Nieuw filter #### */
    /* Add In/Out of Stock Filter to Admin *aanvulling op WooCommerce Sort By Stock* */
    add_action( 'restrict_manage_posts', 'filter001_admin_posts_filter_restrict_manage_posts' );
    
    function filter001_admin_posts_filter_restrict_manage_posts(){
    
        $type = 'product';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
    
        //only add filter to post type you want
        if ('product' == $type){
            //change this to the list of values you want to show
            //in 'label' => 'value' format
            $values = array(
                'Uitverkocht' => 'outofstock', 
                'Op voorraad' => 'instock',
            );
            ?>
            <select name="Stock">
            <option value=""><?php _e('Voorraad', 'filter001'); ?></option>
            <?php
                $current_v = isset($_GET['Stock'])? $_GET['Stock']:'';
                foreach ($values as $label => $value) {
                    printf
                        (
                            '<option value="%s"%s>%s</option>',
                            $value,
                            $value == $current_v? ' selected="selected"':'',
                            $label
                        );
                    }
            ?>
            </select>
            <?php
        }
    }
    add_filter( 'parse_query', 'filter001_posts_filter' );
    
    function filter001_posts_filter( $query ){
        global $pagenow;
        $type = 'product';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
        if ( 'product' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['Stock']) && $_GET['Stock'] != '') {
            $query->query_vars['meta_key'] = '_stock_status';
            $query->query_vars['meta_value'] = $_GET['Stock'];
        }
    }

    You’ll be my hero. Thanks! 🙂

    • This topic was modified 7 years, 5 months ago by Kathryn Presner.
    • This topic was modified 7 years, 5 months ago by Kathryn Presner. Reason: fixed code tags - please use backticks or formatting buttons
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘[Beginner] Combine two hooks’ is closed to new replies.