【问题标题】:Disabinge woocommerce variable product price doesn't work禁用 woocommerce 可变产品价格不起作用
【发布时间】:2018-01-10 10:34:19
【问题描述】:

我尝试了几个代码来禁用 woocommerce 中产品变量中的价格。

我试过了,但什么也没发生:

/*
Disable Variable Product Price Range completely:
*/

add_filter( 'woocommerce_variable_sale_price_html','my_remove_variation_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'my_remove_variation_price', 10, 2 );

function my_remove_variation_price( $price ) {
$price = '';
return $price;
}

我把它放在functions.php中,但什么也没发生。

我的主题是来自 theme-junkie 的 saha。

【问题讨论】:

  • 你写了 2 个参数,但你的函数只包含一个,编辑它。我认为这会有所帮助:hookr.io/filters/woocommerce_variable_sale_price_html
  • 您可以安全地将其更改为 1 而不是 2 因为您要做的就是删除 price 并且只会调用此挂钩关于可变产品。但有一些注意事项,1) woocommerce_variable_price_html 钩子是在 v3.0.2 上引入的,因此它不适用于以前的版本,2) 你应该检查你是否在与 产品显示相关的前端页面上.

标签: wordpress woocommerce


【解决方案1】:

最好的方法是连接到woocommerce_get_price_html filter

它传递$price HTML 和WC_Product* 对象。您可以检查它是否为instance of WC_Product_Variable,以确定它是否是可变产品。然后返回 '' 如果是空字符串。这将处理父变量产品的价格显示。

示例

add_filter('woocommerce_get_price_html', 'mm_handle_variation_prices', 10, 2);
function mm_handle_variation_prices( $price_html, $product_obj ){
    // Bail unless we are on WooCommerce page on frontend and this is an ajax request.
    if( is_admin() || is_ajax() || ! is_woocommerce() ) {
        return $price_html;
    }

    // If this is an Variable product only then return empty string.
    // '$product_obj instanceof WC_Product_Variable' check would work as well.
    if( 'variable' === $product_obj->get_tyepe() ) {
        return '';
    } else {
        return $price_html;
    }
}

【讨论】:

  • 我不明白为什么这个答案在没有任何 cmets 的情况下也会被否决?此功能可在所有 Woocommerce 版本上完美运行,完全向后兼容,而在 v3.0.2 中添加了 woocommerce_variable_price_html 挂钩,因此它不向后兼容。
猜你喜欢
  • 2019-02-12
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 2016-10-15
  • 2018-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多