How do I remove Woocommerce sidebar from cart, checkout and single product pages? - wordpress

Like many people that start using woocommerce for the first time I need to know how to customise it. In my particular situation I want to remove the sidebar from the Cart, Checkout and single product pages. My sidebar is defined and called from sidebar.php using the code below:
<?php dynamic_sidebar('global-sidebar'); ?>
I have tried for a long time to find an answer that works but I can't seem to find the correct code or solution. Perhaps asking the question myself will work. By the way I really appreciate the answers in the other articles and how-to's but they don't work for me.
Before I go any further I am using Bootstrap (latest version as of 2014) to style my Wordpress website. Not sure if that matters but maybe it does somehow.
Can someone please tell me how I find and then tell Woocommerce not to display any kind of sidebar on the Cart, Checkout and Single Product pages?
p.s.
The website can be found here > wp.wunderful.co.uk (staging site for a client website project)

Go to "Cart" page from dashboard pages, from "Page Attribute Section" -> "Templates" choose "Full Width" this will give you a page without sidebar.

In theory, something like the following ought to work, but I haven't tested it. (To be added to your theme's functions.php)
function so_25700650_remove_sidebar(){
if( is_checkout() || is_cart() || is_product() ){
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
}
}
add_action('woocommerce_before_main_content', 'so_25700650_remove_sidebar' );
If you look at the Woo templates you will see
<?php
/**
* woocommerce_sidebar hook
*
* #hooked woocommerce_get_sidebar - 10
*/
do_action( 'woocommerce_sidebar' );
?>
This is Woo saying: "Display the sidebar here". But it is adding the woocommerce_get_sidebar function to the woocommerce_sidebar hook... which is convenient because it allows you to unhook that function like I've shown above. Finally, I am using Woo's conditional logic to only unhook the function from its action on the pages you requested.
I'm running my function on the woocommerce_before_main_content hook, which I think should work assuming your theme hasn't removed that hook. If so, then you could probably use wp_head or something that is guaranteed to be there, though then you'd probably want to check that the is_checkout(), etc functions exist or risk breaking your theme should you ever deactivate WooCommerce. As I have it, i should only run on WooCommerce-specific pages and so checking if the WooCommerce functions are defined is probably overkill.
Important Note:
This assumes the default theme or a theme that isn't running its own custom sidebar functions. If your theme is doing something else you will need to investigates its particular functions and templates.

What you should do is to create a files called woocomemrce.php in your theme if it doesn't already exists. Then you should look at your page template files for full width and for page with sidebar to see how they are structured and see where they differ.
Then copy the contents of one the files into woocommerce.php and replace the loop with woocommerce_content(), see this page for details.
Lastly see where the different page templates differ and then use if-statements in the places where they differ.
if( is_post_type_archive( 'product' ) ) :
//Content to display in the list view (i.e. with sidebar)
else :
//Content to display all other views (i.e. without sidebar)
endif;

.woocommerce-cart .sidebar {
display: none;
}
.woocommerce-cart .content-area {
width: 100%;
}
add this in custom/style.css.

From
https://wordpress.org/support/topic/remove-sidebar-for-woocommerce-cart-and-checkout-pages/
On your theme, likely subtheme, function.php
add_action('wp_head', 'hide_sidebar' ); function hide_sidebar(){ if(is_cart() || is_checkout()){ ?>
<style type="text/css">
#secondary {
display: none;
}
</style>
<?php
}

To remove sidebar from the Cart, Checkout and single product pages you want to use action hook in function.php file -
add_action('woocommerce_before_main_content', 'remove_sidebar' );
function remove_sidebar()
{
if( is_checkout() || is_cart() || is_product()) {
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10);
}
}
Here you can get WooCommerce Action and Filter Hook
-https://docs.woothemes.com/wc-apidocs/hook-docs.html

Related

Change the default global edit link that lists my CustomPostType (edit.php?post_type=CPT), to use a plugin page link instead

I'm working on a plugin (with a https://wppb.me/ plugin boilerplate base, if relevant).
I defined my custom post type, with the editor options I want, that I can edit with the default /wp-admin/post.php?post={id}&action=edit
The listing of those CPT is done on my plugin pages only (no specific menu for the CPT) and I want to keep it that way.
Problem:
I'd like to change the base edit link of this post type when no ID is specified
( eg : /wp-admin/edit.php?post_type=CPT ) to be the URL of my plugin
( eg : /wp-admin/admin.php?page=myplugin )
This, mostly because in Gutenberg Editor Fullscreen, the top left wordpress logo links to the /wp-admin/edit.php?post_type=CPT that I don't wont to use nor show. I'd like this link to be a page of my plugin (here, it would be the homepage of my plugin)
I tried with a filter on get_edit_post_link when there is no ID provided, but it doesn't seem to be the correct way to fix my problem.
Any tips or help to get me in the right direction in my research are welcome !
I found one of the possible solutions. After entering the site, you must redirect the user.
function wp_custom_update_meta_cache_filter() {
global $pagenow, $typenow;
if ( $pagenow == 'edit.php' && $typenow == 'CPT' ) {
wp_safe_redirect();
die();
}
}
add_action( 'admin_init', 'wp_custom_update_meta_cache_filter' );
Based in (233 line):
https://github.com/dipolukarov/wordpress/blob/master/wp-content/plugins/woocommerce/admin/woocommerce-admin-init.php

How to show WooCommerce Categories on 'shop' page instead of products?

I have seen this question and answer. That does not work.
Setup
I am running:
WordPress 5.4.1
WooCommerce 4.1.1
I have a custom theme that overrides some of the WooCommerce templates by placing my own templates in: themes/my_theme/woocommerce/template-name.php
I have established that the shop page (homepage) uses the template archive-product.php. I have copied the this from plugins/woocommerce/templates/archive-product.php into my theme and made some minor HTML changes which work perfectly. There are no functional changes in my theme's copy, just some HTML.
The problem
I want the homepage to only show the store categories, as thumbnails. There is an option to set the shop page to show categories instead of products:
Appearance > Customize > WooCommerce > Catalogue
Shop page display has been set to Show Categories
However the shop homepage seems to ignore this setting entirely, and it still shows the products. It seems surprising that WooCommerce's own template does not honour this setting!
How do I find this setting in the template and then show the categories (as thumbnails) on the shop homepage?
Is there an equivalent to woocommerce_product_loop() for looping categories?
As a side note, the Storefront theme does honour the setting but Storefront does not have the template archive-product.php. Storefront seems to be highly abstracted and after much debugging of it / trying to take it apart I have so far not worked out which template file it is using for the shop page.
My theme is already in production and I just want to make an update so that the homepage shows the categories instead of going directly into the products list.
I found a workable solution. The WooCommerce default templates don't support the setting to show categories on the Shop page.
However using a shortcode with do_shortcode(), and a condition this can be achieved as follows:
if (is_shop()) {
echo do_shortcode('[product_categories hide_empty="0"]');
} else {
woocommerce_product_loop_start();
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
/**
* Hook: woocommerce_shop_loop.
*/
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
}
woocommerce_product_loop_end();
}
Still:
I would like to know how to pick up the 'show categories' customisation setting shown in the question, so my theme responds to that.
Is there a better way than using do_shortcode(), this feels like a bit of a hack

WooCommerce: How to hide/remove the category under the product title in the shop page?

I would like to hide or remove the category meta from the shops page (catalogue
) which is showing right under the product title in my store. I have tried several things but nothing works.
I tried CSS:
.product__categories {display:none!importamt;}
and this...
.product_meta .sku_wrapper {
display:none!important;
}
.product_meta .posted_in {
display:none!important;
}
I tired this in functions too remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
How can I hide the category meta?
I used this:
.product-details .posted_in { display: none; }
and it worked for me
The .product_meta class you are trying to use is not on the catalogue page - it is on the product detail page.
This is an old question, but you might try
remove_action('woocommerce_after_single_product_summary',
'woocommerce_template_single_meta', 40);
in a theme (like in functions.php) or a code snippet.
The PHP solution should be slightly better because it changes the page before delivering it, rather than still serving the meta section and including some CSS instructions to not display it.
This works in WC 6.7.0. If it doesn't work, perform a search in the WooCommerce plugin for the exact lettering, woocommerce_template_single_meta (You can do this by loading the folder into something like Sublime Text.), and adjust accordingly. For example, if in the future, you find that it's added with add_action( 'woocommerce_random_action_why_was_this_changed', 'woocommerce_template_single_meta', 50 ); you need to use remove_action('woocommerce_random_action_why_was_this_changed', 'woocommerce_template_single_meta', 50);.
Update: This isn't working on my own site and I don't know why.
Another thing you can do to remove the product meta section is to create a file in your theme or child theme within a /woocommerce/single-product path called meta.php, i.e: yourawesometheme/woocommerce/single-product/meta.php. Give it this content:
<?php
if (!defined('ABSPATH')) exit;
This works consistently.
(I think you could leave the file completely blank, skipping the (!defined('ABSPATH')) part, but I'm not certain. Perhaps someone else could clarify.)

How to remove footer from some pages dynamicaly using function.php

I am new to wordpress and learning Plugin development, I m creating a custom plugin, which display a list of page-title with check-boxes in admin section and on checking the selected pages footer should be remove from that pages, now I m facing issue with how to remove footer section?
I dont want to remove footer on single page, so custom template can not be used
I dont want to remove footer using css(like display none)
Can anybody help?
You can use Wordpress's remove_action to help remove unwanted php calls. More on that found here
Something like this:
function your__conditional_footer() {
if( is_front_page() )
return;
remove_action( 'theme_before_footer', 'theme_footer_widget_areas' );
}
Mind you, the function arguments are theme dependent.
Hope this points close.

wordpress : how to add categories and tags on pages?

I have generated pages using a custom template by creating a php file in my theme directory
something like :
<?php
*
* Template Name: Contact Page
*/
?>
<html ..... </html>
and then adding a new page on the dashboard selecting this new template
How can i now associate tags and categories to each pages ?
Is creating posts instead of pages the only solution?
Even better is to add to functions.php in your theme folder:
function myplugin_settings() {
// Add tag metabox to page
register_taxonomy_for_object_type('post_tag', 'page');
// Add category metabox to page
register_taxonomy_for_object_type('category', 'page');
}
// Add to the admin_init hook of your theme functions.php file
add_action( 'init', 'myplugin_settings' );
Tried using the accepted answer but for some reason it only shows the Post types and none of the Pages shows in the category page. E.g. /category/entertainment/
To fix that, I have to do this:
// add tag and category support to pages
function tags_categories_support_all() {
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
}
// ensure all tags and categories are included in queries
function tags_categories_support_query($wp_query) {
if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
if ($wp_query->get('category_name')) $wp_query->set('post_type', 'any');
}
// tag and category hooks
add_action('init', 'tags_categories_support_all');
add_action('pre_get_posts', 'tags_categories_support_query');
Try this:
add_action( 'init', 'wpse34528_add_page_cats' );
function wpse34528_add_page_cats(){
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
}
Not at all helpful to say 'download plugin' for beginners who are most likely not going to have downloaded wordpress and are therefore not able to install said plugin. Here is some short code for those like me that have been scouring the web for something that actually works on regular pages with regular accounts - ie you're not a developer.
First, make sure you have your pages in your menu set up properly.
YOU DO NOT NEED TO MAKE YOUR PAGES 'Categories' or 'Tags'!
This wouldn't give you actual pages to then go and edit, so if you are wanting to add sliders, text, an intro, or anything for that matter, you wouldn't be able to.
Then go to WP Admin > Pages
Select a page to edit and go to the text editor instead of visual editor (far right hand side tab)
Then past the following short code:
[display-posts category="hair,makeup,reviews,beauty" posts_per_page="10" include_date="true" text-decoration: none date_format="F j, Y" order="DESC" include_excerpt="true" wrapper="div" image_size="large"]
<
(The shortcode collects all the posts that you have assigned certain categories in your blog posts i.e. mine was hair and beauty. So obviously change yours to ones that are appropriate. It then allocates how many posts (mine was 10), the date (in descending order,) with a large image and an excerpt of the post)
this plugin sorted me out :
http://wordpress.org/extend/plugins/add-tags-and-category-to-page/
with the standard instructions :
Upload the plugin files to the /wp-content/plugins/ directory
Activate the plugin through the 'Plugins' menu in WordPress
Use the setting page of the plugin from Settings > Add Tags And Category For Page.

Resources