如何解决WooCommerce过滤器和我的自定义代码之间的问题?



我试图隐藏商店中某些类别的价格和购买按钮,为此他们帮助我们提供了一个功能来实现这一点。

但是我不能这样做,因为我使用的主题中的代码会阻止我自己的代码实现预期的效果。

如何纠正代码之间的不兼容?事实是,我们已经尝试了几种方法,但没有一种有效,看看我们使用的一些选项:

第一个选项:

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;
}

第二个选项:

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 );

甚至第三个选项:

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>';
}
}

最后,一位同事检查了站点并推断这些功能不起作用,这是因为主题使用了自己的过滤器来显示添加到购物车按钮,因此这些方法不起作用。

我找到了the Theme用来处理购物车按钮主题的文件,如下所示。但问题是,我无法检测是什么阻止主题文件执行我的代码。

当然,最理想的是修改我的代码,因为如果我修改了主题的代码,当它更新时,我将失去更改。

主题中阻止函数执行的代码部分是什么?

如何修正自定义函数以避免此问题?

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' );

我终于找到了解决办法。根据另一个用户的建议,找到可能导致冲突的文件,通过过滤器并删除,直到找到正确的文件。我在我的孩子主题中复制了这个文件,虽然我想进行改进并添加更多的功能,但现在我想分享这些成就,以防任何其他用户也有同样的问题。

这是比较详细的结论:

// 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
}

相关内容

最新更新