php - 如果产品价格高于 Woocommerce 中的特定金额,请在产品价格前添加文本

标签 php wordpress woocommerce hook-woocommerce price

在 Woocommerce 中,如果价格高于 59 欧元,我会尝试在价格之前添加文本

我尝试了以下代码(以及其他代码),但它们不起作用:

    add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
function custom_price_message( $price ) {
if ($price>='59'){
$new_price = $price .__('(GRATIS!)');
}
return $new_price;
}

如果产品价格高于 59,如何在产品价格前添加 (GRATIS!) 文本?

最佳答案

更新:您应该尝试以下重新访问的函数代码:

add_filter( 'woocommerce_get_price_html', 'prepend_text_to_product_price', 20, 2 );
function prepend_text_to_product_price( $price_html, $product ) {
    // Only on frontend and excluding min/max prices on variable products
    if( is_admin() || $product->is_type('variable') ) 
        return $price_html;

    // Get product price
    $price = (float) $product->get_price(); // Regular price

    if( $price > 15 )
        $price_html = '<span>'.__('(GRATIS)', 'woocommerce' ).'</span> '.$price_html;
    
    return $price_html;
}

代码位于事件子主题(或事件主题)的 function.php 文件中。

经过测试并有效。


For single product pages only you will replace this line:

if( $price > 15 ){

By this:

if( $price > 15 && is_product() ){

For single product pages and archives pages you will replace this line:

if( $price > 15 ){

By this:

if( $price > 15 && ( is_product() || is_shop() || is_product_category() || is_product_tag() ){

关于php - 如果产品价格高于 Woocommerce 中的特定金额,请在产品价格前添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48552886/

相关文章:

javascript - PHP 将变量从表单传递到 php

php - codeigniter 代码中的 Ajax 自动搜索不起作用

php - wordpress 函数 remove_menu_page() 抛出错误

MySQL:返回一个表中以另一个表为条件的值列表

php - 如何在 WooCommerce 中为类似产品设置和更新动态库存

php - 从 PHP 脚本获取 JSON

php - 条纹 : Specify Specific Card for Subscription

html - Wordpress 使用详细信息标签创建奇怪的间距

javascript - 从 jQuery 内的动态变量加载数据

wordpress - 将 woocommerce 产品连接到帖子