Remove sidebar in Twenty Seventeen from single products in Woocommerce

有些话、适合烂在心里 提交于 2020-01-03 02:46:13

问题


I have been trying to figure out how to remove the shop sidebar from my single product pages. I have tried various options for this. I am using the twentyseventeen theme. I first tried this snippet from business bloommer in my child functions file.

 add_action( 'wp', 'bbloomer_remove_sidebar_product_pages' );

function bbloomer_remove_sidebar_product_pages() {
if ( is_product() ) {
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
}
}

But this did not work.Subsequently read a thread from another user saying that snippet did not work for twentyseventeen theme.

I then tried to override the woo commerce template woocommerce/templates/singleproduct.php by creating woocommerce/singleproduct.php in my child theme and removing the following:

 <?php
    /**
     * woocommerce_sidebar hook.
     *
     * @hooked woocommerce_get_sidebar - 10
     */
    do_action( 'woocommerce_sidebar' );
?>

This did not work either.Am I missing something obvious here? Very grateful if anyone has some insights on what I am doing wrong. Many thanks.


回答1:


For Twenty Seventeen theme, there is a special class in Woocommerce WC_Twenty_Seventeen that handle the sidebar.

So use the following to remove the sidebar from single product pages:

remove_action( 'woocommerce_after_main_content', array( 'WC_Twenty_Seventeen', 'output_content_wrapper_end' ), 10 );
add_action( 'woocommerce_after_main_content', 'remove_sidebar_from_output_content_wrapper_end', 10 );
function remove_sidebar_from_output_content_wrapper_end() {
    // Only for single product pages
    if( is_product() ) {
        echo '</main></div></div>';
    } else {
        echo '</main></div>';
        get_sidebar();
        echo '</div>';
    }

}

Code goes in function.php file of the active child theme (or active theme). Tested and works.

But you will have to add some CSS rules to your styles.css file, something like:

.has-sidebar.single-product.woocommerce-page:not(.error404) #primary {
    width: 100% !important;
    float: none !important;
}


来源:https://stackoverflow.com/questions/53030852/remove-sidebar-in-twenty-seventeen-from-single-products-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!