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

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.

Related

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.

How to hide Woocommerce product Description tab only for unlogged users?

How to hide product "Description" tab' in Woocommerce plugin only for unlogged users, but visible for registered customers (and logged-in users).
To remove product description tab on sigle product pages for non logged users, you will use:
add_filter( 'woocommerce_product_tabs', 'customize_product_tabs', 100 );
function customize_product_tabs( $tabs ) {
if ( ! is_user_logged_in() ) {
unset( $tabs['description'] ); // remove the description tab
}
return $tabs;
}
This code goes in functions.php file of your active child theme (or active theme). Tested and works.
Try this, add this snippet into the function.php
add_action( 'init', 'hide_price_add_cart_not_logged_in' );
function hide_price_add_cart_not_logged_in() {
if ( !is_user_logged_in() ) {
//Remove short description (excerpt) from single product page
remove_action( 'woocommerce_product_tabs', 'woocommerce_template_single_excerpt', 20 );
}
}

Add a text under specific Woocommerce product category archive page

I would like to show text under Shop page main content. Different text for different categories.
I am aware of showing category description on the Shop page for each category but this is already in use.
Right now I am this far but it does not seem to work in the Child theme functions.php
if ( is_category( 'category1' ) ) {
function add_my_text() {
print '<p>This is my extra text.</p>';
}
add_action( 'woocommerce_after_main_content', 'add_my_text' );
}
Will be thankful if anyone knows how to improve this function
Thrilled I found this answer. Was struggling mightily to add content based on a custom taxonomy created with Custom Post Type UI. In case anyone else needs to know, you don't have to change permalinks or anything silly like that. Simple modification of LoicTheAztec's answer above does the trick.
add_action( 'woocommerce_product_meta_end', 'add_my_text' );
function add_my_text() {
if ( wc_get_product_class( $class = 'taxonomy-term' ) ) {
echo '<h2>IT WORKS!!!</h2>';
}
}
If you want to insert content into a different place on the page, choose a different hook from this very helpful WooCommerce Visual Hook Guide.
Hope this saves somebody else 9 hours :)
The Wordpress conditional function is_category() doesn't work with Woocommerce product categories which are a custom taxonomy. Instead use Woocommerce conditional tag is_product_category() inside your hooked function like:
add_action( 'woocommerce_after_main_content', 'add_my_text' );
function add_my_text() {
if ( is_product_category( 'category1' ) ) {
echo '<p>This is my extra text.</p>';
}
}
Code goes on function.php file of your active child theme (or active theme). It should works.

How to hide the image of a single product on a specific pages in Woocommerce

I have woo commerce site, but in few specific product pages i don't want to show products images. How can i hide product image in specific pages only? once hide/remove product image, need to speared the text/content.
This can be done with this simple code:
add_action( 'template_redirect', 'single_product_remove_images', 1 );
function single_product_remove_images() {
// HERE below set your Page ID, title or slug
if( is_page( 56 ) ){
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
If you want to target a post id istead, you will use something like if( get_the_id() == 56 ){

Remove "Add To Card" only Home page in WooCommerce

I want to remove "Add To Card" or any other button on home page of WooCommerce Theme but not from product single page.
For example:
Home Page: https://demo.woothemes.com/storefront/
Product Single Page: https://demo.woothemes.com/storefront/product/build-your-dslr/
I don't want to show any button on home page under product.
But I want to show button on product page.
I want to do this because i want to force my website visitor to visit product single page.
Which code line will I remove from website > Appearance > Editor?
Try the following:
add_action( 'woocommerce_after_shop_loop_item', 'remove_loop_add_to_cart_on_home', 1 );
function remove_loop_add_to_cart_on_home() {
if( is_front_page() ){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
}
Code goes in function.php file of your active child theme (or active theme).
To remove it on shop page too you will have:
add_action( 'woocommerce_after_shop_loop_item', 'remove_loop_add_to_cart_on_home', 1 );
function remove_loop_add_to_cart_on_home() {
if( is_front_page() || is_shop() ){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
}
Code goes in function.php file of your active child theme (or active theme).
See Woocommerce conditional tags

Categories