Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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>';
}
}
share|improve this question
    
what do you mean when you say "without success"? –  Mark Kaplun Jan 1 at 5:23
    
What exactly you are trying to achieve here ? can you please provide some more details ? –  Subharanjan Jan 1 at 6:32
    
I mean the code doesn't work. Used @Subharanjan code dynamic_sidebar below. –  Muzza Jan 2 at 5:32

3 Answers 3

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>';
    }
}
share|improve this answer
1  
Thanks Subharanjan, the dynamic_sidebar works a treat. –  Muzza Jan 2 at 5:30
    
The code also adds extra sidebars to the footer. How do remove these? See footer on site for an example (lne.net.au/shop) –  Muzza Jan 3 at 8:51

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.

share|improve this answer
    
Thanks @braddalton, I will read into Woo's conditions. –  Muzza Jan 2 at 5:34
up vote 0 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.