1

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

3 Answers 3

0

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>';
    }
}
1
  • 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
    Commented Jan 3, 2014 at 8:51
0

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.

0
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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