I am trying to hide the prices and the buy button of some categories of the store, and for this they helped us with a function to be able to do this.
But I can't do it because there is code from the theme that I use that prevents my own code from doing the desired effect.
How can I correct the incompatibility between code? The truth is that we have tried several methods, and none of them worked, see some of the options that we have used:
first option you use:
add_filter('woocommerce_get_price_html', 'hide_price_and_button_for_category', 10, 2);
function hide_price_and_button_for_category($price, $product) {
if (has_term('herramientas', 'product_cat', $product->get_id())) {
// If the product belongs to "your-category-slug" category
$price = '';
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);
}
return $price;
}
second option you use:
function ocultar_precios_y_compra_en_categoria( $price, $product ) {
// Aquí puedes reemplazar "nombre-de-tu-categoria" por la categoría que deseas ocultar
if ( has_term( 'herramientas', 'product_cat', $product->get_id() ) ) {
$price = '';
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'ocultar_precios_y_compra_en_categoria', 10, 2 );
Even a third option:
add_action( 'woocommerce_single_product_summary', 'hide_prices_button_category', 1 );
function hide_prices_button_category() {
global $product;
$hide_categories = array( 'fontaneria', 'herramientas' ); // Reemplaza 'categoria1' y 'categoria2' por las categorías que deseas ocultar.
if ( has_term( $hide_categories, 'product_cat', $product->get_id() ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
echo '<p>Este producto no está disponible para la venta en esta categoría.</p>';
}
}
Finally, a colleague reviewed the site and deduced that these functions did not work, it is because the theme used its own filter to show the Add to cart button, and therefore the methods do not take effect.
I found the file that The Theme uses to handle the cart button theme and it is as follows. But the problem is that I can't detect what is preventing the theme file from executing my code.
Of course, the ideal would be to modify my code, since if I modify the code of the theme, when it is updated, I will lose the changes.
What is the code part of the theme that prevents my function from executing ?
How to correct any of the custom functions to avoid this problem ?
function tema_body_classes_catalog_mode( $classes ) {
// Catalog mode
if(get_theme_mod('catalog_mode')) $classes[] = 'catalog-mode';
if(get_theme_mod('catalog_mode_prices')) $classes[] = 'no-prices';
return $classes;
}
add_filter( 'body_class', 'tema_body_classes_catalog_mode' );
function tema_catalog_mode_product(){
if(tema_option('catalog_mode_product')){
echo '<div class="catalog-product-text pb relative">';
echo do_shortcode(tema_option('catalog_mode_product'));
echo '</div>';
}
echo '<style>.woocommerce-variation-availability{display:none!important}</style>';
}
add_action('woocommerce_single_product_summary', 'tema_catalog_mode_product',30);
function tema_catalog_mode_lightbox(){
if(tema_option('catalog_mode_lightbox')) {
echo '<div class="catalog-product-text pb relative">';
echo do_shortcode(tema_option('catalog_mode_lightbox'));
echo '</div>';
}
echo '<style>.woocommerce-variation-availability{display:none!important}</style>';
}
add_action( 'woocommerce_single_product_lightbox_summary', 'flatsome_catalog_mode_lightbox', 30 );
/* Disable purchasing of products */
add_filter('woocommerce_is_purchasable', 'tema_woocommerce_is_purchasable', 10, 2);
function tema_woocommerce_is_purchasable($is_purchasable, $product) {
return false;
}
/* Remove variations add to cart */
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20);
/* Remove add to cart quick button */
remove_action( 'tema_product_box_actions', 'tema_product_box_actions_add_to_cart', 1 );
/* Remove prices from loop */
if(tema_option('catalog_mode_prices')) remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
/* Renove prices from product page */
if(tema_option('catalog_mode_prices')) remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
/* Remove prices from lightbox */
if(tema_option('catalog_mode_prices')) remove_action( 'woocommerce_single_product_lightbox_summary', 'woocommerce_template_single_price', 10 );
/* Remove sale badges */
if( get_theme_mod( 'catalog_mode_sale_badge', 0 )) add_filter( 'woocommerce_sale_flash', '__return_empty_string' );
I finally found a solution. At the suggestion of another user, find the files that could be causing the conflict, go through the filters and delete until you find the correct one. I duplicated this file in my child theme and although I want to make improvements and add more functionality, for now I want to share these achievements in case any other user comes along with the same questions.
This is the somewhat detailed conclusion:
// This snippet will replace the price with the custom text set here
add_filter( 'woocommerce_get_price_html', 'wwpp_restricted_category_for_non_wholesale__price_html', 9999, 2 );
function wwpp_restricted_category_for_non_wholesale__price_html( $price, $product ) {
if ( is_non_wholeale_and_restricted_category($product) ) {
$price = '<div><a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">' . __( 'Contacte para más información', 'bbloomer' ) . '</a></div>';
}
return $price;
}
//This snippet will remove the add to cart button
add_filter( 'woocommerce_loop_add_to_cart_link', 'wwpp_restricted_category_for_non_wholesale__add_to_cart_button', 9999, 3 );
function wwpp_restricted_category_for_non_wholesale__add_to_cart_button( $add_to_cart, $product, $args ) {
if ( is_non_wholeale_and_restricted_category($product) ) {
$add_to_cart = '';
}
return $add_to_cart;
}
// This will set the products belonging to the selected product category as non-purchasable
add_filter( 'woocommerce_is_purchasable', 'wwpp_restricted_category_for_non_wholesale__is_purchasable', 9999, 2 );
function wwpp_restricted_category_for_non_wholesale__is_purchasable( $is_purchasable, $product ) {
if ( is_non_wholeale_and_restricted_category($product) ) {
$is_purchasable = false;
}
return $is_purchasable;
}
//This snippet will restrict the selected product category to only be available to the selected roles
function is_non_wholeale_and_restricted_category( $product ) {
$user = wp_get_current_user();
$allowed_roles = array( 'shop_manager', 'administrator' );//update roles that can see price and add to cart
return ! array_intersect( $user->roles, $allowed_roles ) && has_term( 'herramientas', 'product_cat', $product->get_id() ) ? true : false ;//replace your-category-name with the product category slug you want other roles to hide, except the one set in $allowed_roles
// replace your-category-name with the product category slug you want other roles to hide, except the one set in $allowed_roles
}