woocommerce hooks and is_shop() / is_archive() - php

I want to do two things:
remove the breadcrumbs in category pages, but leave it in single product pages.
remove the title from the shop page.
For the breadcrumbs, I tried this code and it doesn't work:
if( is_archive() ){
add_action( 'init', 'porto_remove_wc_breadcrumbs' );
function porto_remove_wc_breadcrumbs() {
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );
}
}
(without the conditional tag it does work, or if I change the conditional to if(1<2) instead).
no matter what archive page i'm displaying, with the code above the breadcrumbs is shown.
About the second problem, this code doesn't work either:
if( is_shop() ){
add_filter( 'woocommerce_show_page_title' , 'woo_hide_page_title' );
function woo_hide_page_title() {
return false;
}
}
There's still title in shop page after running this code. I guess the solution for both problems are the same.
What am I doing wrong?

Related

How can I remove short description on the shop page in Woocommerce

Shortly, I want to just remove the short description on the shop/archive page. I added two images. it will be helpful for your understanding. I tried many combination but not working on shop page.
I found a code but it was just removed on the product page.
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
function woocommerce_template_single_excerpt() {
return;
}
Option 1: To remove description entirely place the following action in your child theme functions.php
remove_action('woocommerce_after_shop_loop_item_title','electro_template_loop_product_excerpt', 80 );
Option 2: To override the function place the following function in your child theme functions.php file
function electro_template_loop_product_excerpt() {
global $post;
// In case you want to limit description for example or output different content there
if ( ! is_object( $post ) || ! $post->post_excerpt || $post->post_excerpt ) {
return;
}
}

WooCommerce: How to exclude a specific page in a hooked action

This code works perfectly except I'm trying to determine how to EXCLUDE the main shop page from being included in the action. This hook adds the HTML code to all archive-type pages including the main shop page. Given this is a link back to the shop page, it should be excluded on the shop page.
Any help would be greatly appreciated.
function add_back_to_catalog_category() {
?>
<div><br><p class="button-catalog">Back to Catalog - All Products</p></div><?php
}
add_action( 'woocommerce_after_shop_loop', 'add_back_to_catalog_category' );
Visual reference to woocommerce_after_shop_loop action hook.
You can use the WooCommerce conditional tag is_shop() like:
add_action( 'woocommerce_after_shop_loop', 'add_back_to_catalog_category' );
function add_back_to_catalog_category() {
// Not on shop page
if( ! is_shop() ) {
printf( '<div><br><p class="button-catalog">%s</p></div>',
get_permalink( wc_get_page_id( 'shop' ) ),
__("Back to Catalog - All Products", "your-text-domain")
);
}
}
Code goes in functions.php file of the active child theme (or active theme). It should works.

How to change the position of the the breadcrumbs using hooks?

I am learning Woocommerce. I am on shop page and I have to display the breadcrumbs below the product header. By default it is showing like below image.
I have to display the below of the header. I want to know that what hook I have to use it.
I tried below
add_action( 'woocommerce_breadcrumb_defaults', 'woocommerce_show_page_title', 10, 2 );
but it's not working even I am also not getting my home in the breadcrumbs
You must use the following hooks to display breadcrumb under the archive title:
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
add_action( 'woocommerce_archive_description', 'woocommerce_breadcrumb', 15 );
If you need to do this on specific pages, you should use conditional tags:
// just for Shop page
if ( is_shop() && ! is_product_tag() && ! is_product_category() ) {
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
add_action( 'woocommerce_archive_description', 'woocommerce_breadcrumb', 15 );
}

How to Redirect specific category product pages to cart page in Woocommerce

i want to redirect users when they click single page product directly to cart without going to product description page for only one category products.
i used this code:
add_action( 'wp', 'ts_redirect_product_pages', 99 );
function ts_redirect_product_pages() {
if ( is_product() ) {
wp_safe_redirect( home_url('/cart/'));
exit;
}
}
it's redirecting to the cart page but all the other product categories also redirecting i only want one product category to be redirected, please guide me how to do this
Better to use template_redirect hook. To target specific product category(ies), you can use WordPress has_term() conditional function. For cart url, is better to use WooCommerce function wc_get_cart_url()...
So in the code below define your product category(ies) term(s) name(s), slug(s) or Id(s):
add_action( 'template_redirect', 'ts_redirect_product_pages' );
function ts_redirect_product_pages() {
$categories = array('my-category-1', 'my-category-2');
if ( is_product() && has_term( $categories, 'product_cat' ) ) {
wp_safe_redirect( wc_get_cart_url() );
exit;
}
}
Code goes in functions.php file of the active child theme (or active theme). It should work.

remove woocommerce storefront homepage title php

I am using storefront theme by woocommerce. I need to remove homepage title (h1) with php, i know css solution, but i don't want to use it, because i want to add h1 to other place in that page and it's bad for seo to have 2 h1's in one page! I also know about plugins that remove page title, but they are working as css display:none; property!
I tried all the snippets that i could find in web, but no luck!
Here is my site domain BrightBells.com
Here is PHP code snippets that i tried one by one by adding to my functions.php file, but none of its help!
Snippet 1
function wc_hide_page_title() {
if( is_front_page() )
return true;
}
add_filter( 'woocommerce_show_page_title', 'wc_hide_page_title' );
Snippet 2
function sf_change_homepage_title( $args ) {
remove_action( 'storefront_page', 'storefront_page_header', 10 );
}
add_action( 'init', 'sf_change_homepage_title' );
Snippet 3
function sf_change_homepage_title( $args ) {
remove_action( 'storefront_page', 'storefront_page_header', 10 );
}
add_action( 'wp', 'sf_change_homepage_title' );
Snippet 4
function sf_change_homepage_title( $args ) {
remove_action( 'storefront_page', 'storefront_page_header', 10 );
}
add_action( 'after_setup_theme', 'sf_change_homepage_title' );
Snippet 5
function sf_change_homepage_title( $args ) {
if(is_front_page()) {
remove_action( 'storefront_page', 'storefront_page_header', 10 );
}
}
add_action( 'init', 'sf_change_homepage_title' );
Snippet 6
if ( is_front_page() ) {
remove_action( 'storefront_page', 'storefront_page_header' );
}
Snippet 7
if ( is_page('page id') )
{
add_filter( 'the_title', '__return_false' );
}
None of this snippets help, please help!
Here is page settings screenshots!
setting page screenshot
page screenshot
Thanks in advance
I hope you are using a child theme or plugin in doing this.
Because it will just be removed when your theme updates.
In storefront version 2.2.5, it can be done with this code:
remove_action( 'storefront_homepage', 'storefront_homepage_header', 10 );
Update:
if on a child theme please do something like this:
if ( ! function_exists( 'storefront_homepage_header' ) ) {
function storefront_homepage_header() { }
}
Child themes are run first before the parent theme. By doing so, we will define this storefront_homepage_header function first. In that case, the parent theme will not create it. Applicable only on themes that uses function_exists, luckily, storefront does.
remove_action will not work because the action has not been added yet.
Try this ---
if ( is_front_page() || is_home() )
{
add_filter( 'the_title', '__return_false' );
}

Categories