Hook Widget to Woo Theme in functions.php

Right now I am adding a sidebar above my woocommerce products. How can I hook the sidebar to my theme.

Have registered sidebar and it works.

Read More
register_sidebar( 
    array( 
        'name'          => __( 'Filters', 'woothemes' ), 
        'id'            => 'filters', 
        'description'   => __( 'Optional widgetized shop page (displays only if widgets are added here).', 'woothemes' ), 
        'before_widget' => '<div id="%1$s" class="widget %2$s">', 
        'after_widget'  => '</div>', 
        'before_title'  => '<h3>', 
        'after_title'   => '</h3>' 
    ) 
);

This is the hook I have used without success.

add_action( 'woo_main_before', 'woo_sidebar' );
function woo_sidebar() {
if (is_woocommerce()) { 
    echo '<div class="primary">';
        woo_sidebar( 'filters' );
    echo '</div>';
}

*Update: Using the code below adds this widget area to all other widget areas. Example: In the footer of www.lne.net.au.

add_action( 'woo_main_before', 'woo_sidebar' );
function woo_sidebar() {
if (is_woocommerce()) { 
    echo '<div class="primary">';
    dynamic_sidebar( 'filters' );
    echo '</div>';
}
}

Related posts

3 comments

  1. You should use the dynamic_sidebar to spit out the widgets inside the newly created sidebar before the woo main container.

    add_action( 'woo_main_before', 'woo_sidebar' );
    function woo_sidebar() {
        if (is_woocommerce()) { 
            echo '<div class="primary">';
            dynamic_sidebar( 'filters' );
            echo '</div>';
        }
    }
    
  2. This solution has been tested on a single product page using Woo Commerce.

    This method works from your child themes functions.php file and is the suggested method for non developers who customize themes.

    function wpsites_register_woo_widget() {
    
     register_sidebar( array(
        'name' => 'Before Products Widget',
        'id' => 'before-products',
        'before_widget' => '<div>',
        'after_widget' => '</div>',
    ) );
    }
    
    add_action( 'widgets_init', 'wpsites_register_woo_widget' );
    
    add_filter( 'woo_main_before', 'before_products_widget', 25 );
    
    function before_products_widget() {
    
    
            if ( is_product() && is_active_sidebar( 'before-products' ) ) { 
            dynamic_sidebar('before-products', array(
        'before' => '<div class="before-products">',
            'after' => '</div>',
    ) );
    
    
        }
    
    }
    

    Here’s more about Woo’s conditional tags for Woo Commerce

    You can change both the hook and conditional tag.

    Note: If you don’t have a child theme created, you can use the Custom Functions section of any Woo themes functions file.

  3. Yey! Finally found a solution, replacing the depreciated hook woo_sidbar with woocommerce_sidebar. Also replacing woo_sidbar with dynamic_sidebar.

    // Add widget area above Woocommerce products
    
    add_action('woocommerce_before_shop_loop', 'woocommerce_sidebar', 40);
    
    function woocommerce_sidebar()
        {
        if (is_woocommerce())
            {
            echo '<div class="filters archive-description content-container-fullwidth">';
            dynamic_sidebar('filters');
            echo '</div>';
            }
        }
    

    Thank you everyone for your help. Muz

Comments are closed.