English 中文(简体)
如果零,如何清除产品销售价格?
原标题:How to clear product sale price if it Zero?

我正在创建一个有4000种产品的WooCommerce商店。产品清单每天由制造商更新,然后通过FTP在这个网络商店更新。

不幸的是,制造商给出了“0”值作为非折扣产品的销售价格。 我怎样才能删除这一价值?

我试过使用以下代码:

add_filter( woocommerce_get_price_html ,  custom_price_html , 10, 2 );
function custom_price_html( $price, $product ) {

    if ( $product->is_on_sale() && $product->get_sale_price() ==  0  ) {
        $price = wc_price( wc_get_price_to_display( $product, array(  price  => $product->get_regular_price() ) ) ) . $product->get_price_suffix();
    }
    return $price;
}

但只有显示的价格才会被改变(网络商店显示原始价格), 但如果我把它加进车内, 价格“ 0” 就会被考虑进去。

我不仅想展示零价的原始价钱 我想删掉

顺便说一句,价格过滤器在网站上也没有这样做,因为它认为这是“0”价格。

最佳回答

您的代码中缺少的东西... 您应该尝试使用以下的替换, 这将过滤零销售价格, 并重设它们 :

// Adjust product active price
add_filter( woocommerce_product_get_price ,  adjust_product_active_price , 20, 2);
add_filter( woocommerce_product_variation_get_price ,  adjust_product_active_price , 20, 2);
function adjust_product_active_price( $price, $product ) {
    if ( $price == 0 && $product->get_regular_price() > 0 ) {
        return floatval($product->get_regular_price());
    }
    return $price;
}

// Empty product sale price with zero value
add_filter( woocommerce_product_get_sale_price ,  adjust_product_zero_sale_price , 20, 2 );
add_filter( woocommerce_product_variation_get_sale_price ,  adjust_product_zero_sale_price , 20, 2 );
function adjust_product_zero_sale_price( $price, $product ) {
    if ( $price == 0 ) {
        return   ;
    }
    return $price;
}

// Utility function (Remove zero sale prices)
function remove_zero_prices( $prices ) {
    foreach( $prices as $key => $price ) {
        if ( $price == 0 ) {
            unset($prices[$key]);
        }
    }
    return $prices;
}

// Adjust displayed variable price
add_filter(  woocommerce_variable_price_html ,  adjust_variable_price_html , 20, 2 );
function adjust_variable_price_html( $price_html, $product ) {
    $prices = $product->get_variation_prices( true );

    if ( min($prices[ sale_price ]) > 0 ) {
        return $price_html;
    }
    $active_prices  = remove_zero_prices($prices[ price ]);
    $regular_prices = $prices[ regular_price ];
    $min_price      = min(array_merge($active_prices, $regular_prices));
    $max_price      = max(array_merge($active_prices, $regular_prices));
    $min_reg_price  = current($regular_prices);
    $max_reg_price  = end($regular_prices);

    if ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) {
        $price_html = wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) );
    } elseif ( $min_price !== $max_price ) {
        $price_html = wc_format_price_range( $min_price, $max_price );
    } else {
        $price_html = wc_price( $min_price );
    }
    return $price_html;
}

add_filter( woocommerce_product_is_on_sale ,  filter_variable_product_is_on_sale , 20, 2);
function filter_variable_product_is_on_sale( $is_on_sale, $product ) {
    if( $product->is_type( variable ) ) {
        $is_on_sale = false;
        
        foreach( $product->get_available_variations(  ) as $variation ) {
            if( $variation->is_on_sale() ) {
                return true;
            }
        }
    }
    return $is_on_sale;
}

代码会在您的子主题( 或插件) 的.php 文件中输入函数. php 。 测试并工作 。

相关:

问题回答

您是否试图在销售价格中添加空字符串?

function clear_zero_sale_price( $product ) {
    if ( $product->get_sale_price() ==  0  ) {
        $product->set_sale_price(    );
        $product->save();
    }
}


add_action(  woocommerce_product_set_stock ,  clear_zero_sale_price  );

试试这个

add_filter( woocommerce_get_price_html ,  custom_display_price_if_sale_zero , 100, 2);

function custom_display_price_if_sale_zero($price, $product) {
    if ($product->is_on_sale() && $product->get_sale_price() == 0) {
        $regular_price = wc_price($product->get_regular_price());
        $price = sprintf( <span class="woocommerce-Price-amount amount">%s</span> , $regular_price);
    }
    return $price;
}
add_action(  woocommerce_before_calculate_totals ,  change_sale_price_to_regular_price  );

function change_sale_price_to_regular_price( $cart ) {
    if ( is_admin() && ! defined(  DOING_AJAX  ) ) {
        return;
    }
    
    if ( did_action(  woocommerce_before_calculate_totals  ) >= 2 ) {
        return;
    }

    foreach ( $cart->get_cart() as $cart_item ) {
        $product = $cart_item[ data ];

        if ( $product->is_on_sale() && $product->get_sale_price() == 0 ) {
            $product->set_price( $product->get_regular_price() );
        }
    }
}

Also add this hook in functions file. I will replace the sale price to regular price if sale price is zero

挂入 Woocommerce_get_price_html : 这个过滤器允许我们修改产品价格的 HTML 。

检查产品是否在销售中, 销售价格为零: 如果产品在销售中, 是_ on_ sale () 方法检查, 而获得_ sale_ price () 方法取回销售价格。 如果销售价格为零, 代码将执行 。

显示正则价格 : 如果销售价格为零, 函数会使用“ 正常价格 () ”, 以 wc_ price () 格式格式获取常规价格, 并将其设定为产品价格 。

By adding this code to your theme’s functions.php file, WooCommerce will display the regular price instead of the sale price if the sale price is set to zero. Additional Customization

如果您想要添加一些自定义样式或消息以表示销售价为零, 您可以修改 spripf 部分, 以根据需要添加 HTML 或文字





相关问题
Wrap stray text in <p> tags

Wordpress issue.. how do I wrap stray text in P tags? Example: Before- <div class = "content"> <img src = "hello.jpg"/> <h1>Introduction</h1> Hello! this is ...

Using jQuery Plugins with Wordpress

Having a bit of trouble using jQuery plugins (Superfish, jQuery UI, etc) using Wordpress. Everything works fine in my plain non-Wordpress site, but Wordpress seems to conflict with JQuery. There must ...

WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Why can t I properly style a blockquote in Wordpress?

On the design I just created for my website, I have a blockquote styled with two quote images using the span technique in css: blockquote { background-image: url(images/openquote.jpg); background-...

How does the WordPress <!--nextpage--> tag actually work?

What happens? I m guessing that somehow the post or page is parsed before displaying, and then just split into two methods? I can t seem to find any documentation on how the underlying <?php ...

Wordpress Plug-ins: How-to add custom URL Handles

I m trying to write a Wordpress Plug-in but can t seem to figure out how you would modify how a URL gets handled, so for example: any requests made for: <url>/?myplugin=<pageID> will ...

热门标签