Created
January 13, 2023 15:49
-
-
Save FrancoStino/3c307b9356fd3aedef81c3c75e2a88b6 to your computer and use it in GitHub Desktop.
Hide Add to cart for certain categories - Woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Replace 'Add to cart' button to link on Shop Page and Archive Pages if is Simple Product | |
*/ | |
function my_replacing_add_to_cart_button( $button, $product ) { | |
$categories = array( 'pavimenti' ); | |
if (has_term( $categories, 'product_cat', get_the_id() ) ) { | |
if ( $product->is_type( 'simple' ) ) { | |
$button_text = __("View product", "woocommerce"); | |
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>'; | |
} | |
} | |
return $button; | |
} | |
add_filter( 'woocommerce_loop_add_to_cart_link', 'my_replacing_add_to_cart_button', 10, 2 ); | |
/** | |
* Remove Add to Cart button from product description of product if is product category (for all product types) | |
*/ | |
function my_replace_add_to_cart_btn() { | |
global $product; | |
$categories = array( 'pavimenti' ); | |
if (has_term( $categories, 'product_cat', get_the_id() ) ) { | |
?> | |
<style>div.quantity,.qty{display:none}</style> | |
<style>div.quantity,.single_add_to_cart_button{display:none}</style> | |
<script>jQuery(document).ready(function($){$('div.quantity,.qty').remove();})</script> | |
<script>jQuery(document).ready(function($){$('div.quantity,.single_add_to_cart_button').remove();})</script> | |
<?php | |
} | |
} | |
add_action( 'woocommerce_single_product_summary', 'my_replace_add_to_cart_btn', 31 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment