Archived topic
Adding WooCommerce-only sidebar
11 replies · Started by neverything on July 9, 2018
Hi, I am using GP Premium with the WooCommerce plugin, and I want to display a WooCommerce-specific sidebar on my Woo archive pages. I would prefer to not use another plugin to filter widgets, or anything like that. (I know this topic has come up a few times here in the support forums, but I went through all the past solutions and can't seem to get it to work.)
I have the new Woo-only sidebar registered and showing correctly (yay) but the regular right sidebar is also showing above it. I have narrowed the problem down to the first 3 lines below:
// this code seems to have no effect...
if ( function_exists( 'is_woocommerce' ) && is_woocommerce() ) {
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
}
// this code below works great and adds my Woo sidebar correctly
add_action( 'woocommerce_sidebar', function () {
get_sidebar( 'woocommerce' );
} );
My theory is that the GP Premium Woo plugin is overriding and doing its own sidebar display. I found the generate_sidebar_layout filter and played with that but no luck. How can I get just my Woo sidebar to show on my Woo is_archive() pages, and not the regular sidebar at all?
Thanks very much,
Eric
p.s. I see so many requests for a WooCommerce-specific sidebar, it would be cool if this was an option in the GP Premium plugin... turn it on and it shows up in widgets, ready to roll.
Hi there
can you provide a link to the site? You can edit your original post and use the Site URL field which is for admins only.
Hi David, this is Eric on a different account - I've edited the post above to include the site URL (it is on a staging server) but you'll need a user account to get in. How can I get you those credentials? Thanks.
You can send it through Account Issue here:
https://generatepress.com/contact/
Make sure to reference the topic URL.
Ok! Just sent. Thanks for your support. E
Hi Eric,
ok just spotted that you're trying to disable a sidebar that GP doesn't generate in that first function - that is we don't have a woocommerce_sidebar. What is being displayed is the GP right-sidebar. This article here has a filter for changing the layout, the first example is for a woocmmerce setup - just need to return no-sidebar.
https://docs.generatepress.com/article/sidebar-layout/
Let me know.
Hi David, thank you for this reply, sorry it has taken me a few days to track it down and try it out.
I am not sure I completely understand what you are saying. :-)
In my functions I register a new sidebar called 'woocommerce'. How can I hook into GP to tell it to draw this sidebar on the Woo is_archive() page, but not anywhere else?
I tried adapting the code snippet you mentioned, but now, the normal sidebar is showing up on the Blog page (good), and on my Woo page, I get both the normal sidebar (bad) and a weird phantom sidebar with just search and archives (??). I don't see my custom woocommerce sidebar anywhere, but on my woo page I am swimming in sidebars. :-)
I just pushed out a new deploy with that changed code snippet. Any chance you can take a peek? And feel free to look at what I did in functions.php. You'll see where I create the custom woocommerce sidebar in there, too.
Thank you for your help...
best
Eric
Apologies, think i am having a meltdown lol - this is a lot more complicated then it looks. Have you thought of using a plugin like:
Hi David, heh, thank you for the reply. I could use a plugin but that seems like major overkill. I can write the code to draw a sidebar, and I can easily limit that to woocommerce archive pages only. The issue is where to hook in with GP. Maybe I can kill the GP Premium plugin that plays with sidebars and that will let me do it. I'll let ya know what happens....
Ultimately it was easier just to override this GP function and put this into my child theme functions.php... But I would strongly recommend you guys add a hook for in here to let the user add their own sidebar easily! :-)
// override this function
function generate_construct_sidebars() {
$layout = generate_get_layout();
// When to show the right sidebar.
$rs = [ 'right-sidebar', 'both-sidebars', 'both-right', 'both-left' ];
// When to show the left sidebar.
$ls = [ 'left-sidebar', 'both-sidebars', 'both-right', 'both-left' ];
// If left sidebar, show it.
if ( in_array( $layout, $ls ) ) {
get_sidebar( 'left' );
}
if ( function_exists( 'is_woocommerce' ) && is_woocommerce() && is_archive() ) {
get_sidebar( 'woocommerce' );
} elseif ( in_array( $layout, $rs ) ) {
// If right sidebar, show it.
get_sidebar();
}
}
Hi there, glad you found a solution and thank you for sharing. Nice method.
I believe Tom has something planned in this area.
Review ✔ done