• Resolved martyn

    (@marty2010)


    Hi, I need to have a sidebar for woocommerce, so have found this function here:

    add_action('generate_woocommerce_sidebars','generate_construct_sidebars');
    function generate_construct_sidebars()
    {
    	get_sidebar('woocommerce'); 	
    }
    
    function arphabet_widgets_init() {
    	register_sidebar( array(
    		'name'          => 'WooCommerce Sidebar',
    		'id'            => 'woocommerce_sidebar',
    		'before_widget' => '<div>',
    		'after_widget'  => '</div>',
    		'before_title'  => '<h4>',
    		'after_title'   => '</h4>',
    	) );
    }
    add_action( 'widgets_init', 'arphabet_widgets_init' );
    
    add_action( 'wp','tu_woocommerce_sidebar' );
    function tu_woocommerce_sidebar()
    {
        if ( function_exists( 'is_woocommerce' ) && is_woocommerce() ) {
            remove_action( 'generate_sidebars','generate_construct_sidebars' );
            add_action( 'generate_sidebars','tu_construct_woocommerce_sidebar' );
        }
    }
    
    function tu_construct_woocommerce_sidebar() {
        get_sidebar( 'woocommerce' );
    }

    Then i have copied and renamed the sidebar.php in sidebar-woocommerce.php in my Child.

    <div id="right-sidebar" <?php generate_do_element_classes( 'right_sidebar' ); ?>>
    	<div class="inside-right-sidebar">
    		<?php
    		/**
    		 * generate_before_right_sidebar_content hook.
    		 *
    		 * @since 0.1
    		 */
    		do_action( 'generate_before_right_sidebar_content' );
    
    		if ( ! dynamic_sidebar( 'woocommerce_sidebar' ) ) {
    			generate_do_default_sidebar_widgets( 'right-sidebar' );
    		}
    
    		/**
    		 * generate_after_right_sidebar_content hook.
    		 *
    		 * @since 0.1
    		 */
    		do_action( 'generate_after_right_sidebar_content' );
    		?>
    	</div>
    </div>

    Now the right sidebar is disappeared and the new one is working.
    Do you see something wrong in my work?
    Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Elvin

    (@ejcabquina)

    Hi,

    To clarify: You’re trying to always display the woocommerce sidebar in place of the default sidebar when on woocommerce pages?

    If so, that’s basically what your code does. It completely replaces the right-sidebar with your ‘woocommerce_sidebar’ sidebar when you’re on woocommerce pages. is_woocommerce() checks that within the tu_woocommerce_sidebar() function.

    Any particular issue you’re having? Let us know.

    • This reply was modified 4 years, 1 month ago by Elvin.
    Thread Starter martyn

    (@marty2010)

    Yes, because I need a dedicated sidebar for woocommerce.
    The issue is the right sidebar (home, pages, posts) is disappeared and the woocommerce sidebar created is working.
    I would know how to fix the function or php above to have 2 indipendent right sidebar (native and woocommerce) in generatepress.

    Ref. https://generatepress.com/forums/topic/how-do-i-add-a-custom-sidebar/#post-251055 but it doesn’t work

    • This reply was modified 4 years, 1 month ago by martyn.
    Theme Author Tom

    (@edge22)

    Hi there,

    Try this:

    add_action( 'wp', function() {
        remove_action( 'woocommerce_sidebar', 'generate_construct_sidebars' );
    
        add_action( 'woocommerce_sidebar', function() {
            get_sidebar( 'woocommerce' );
        } );
    } );

    That should use your sidebar-woocommerce.php file within WooCommerce templates.

    Let us know ??

    Thread Starter martyn

    (@marty2010)

    Hi Tom,
    The woo sidebar is registered;
    I’ve copied and paste your function;
    But the right-sidebar is ‘overwritten’ by woocommerce_sidebar, and disappeared in the widget menu too.
    I don’t know, this is the ‘last step’: the sidebar-woocommerce.php code

    <?php
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    ?>
    <div class="inside-right-sidebar">
    		<?php
    		
    		do_action( 'generate_before_right_sidebar_content' );
    
    		if ( ! dynamic_sidebar( 'woocommerce_sidebar' ) ) {
    			generate_do_default_sidebar_widgets( 'right-sidebar' );
    		}
    
    		do_action( 'generate_after_right_sidebar_content' );
    		?>
    	</div>
    </div>
    Theme Author Tom

    (@edge22)

    Can you share the full code you’re using, including the widget area registration?

    Thread Starter martyn

    (@marty2010)

    Don’t kill me, I made a mistake. I didn’t delete a section of the code below (now is commented out to show you)

    /*   WRONG PART
    add_action('generate_woocommerce_sidebars','generate_construct_sidebars');
    function generate_construct_sidebars()
    {
    	get_sidebar('woocommerce'); 	
    }
    */
    
    function arphabet_widgets_init() {
    	register_sidebar( array(
    		'name'          => 'WooCommerce Sidebar',
    		'id'            => 'woocommerce_sidebar',
    		'before_widget' => '<div>',
    		'after_widget'  => '</div>',
    		'before_title'  => '<h4>',
    		'after_title'   => '</h4>',
    	) );
    }
    add_action( 'widgets_init', 'arphabet_widgets_init' );
    
    /* Tom's code */
    add_action( 'wp', function() {
        remove_action( 'woocommerce_sidebar', 'generate_construct_sidebars' );
    
        add_action( 'woocommerce_sidebar', function() {
            get_sidebar( 'woocommerce' );
        } );
    } );

    Solved.
    Many thank’s!

    Theme Author Tom

    (@edge22)

    Glad you got it sorted! ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘woocommerce sidebar’ is closed to new replies.