if the product is not available in the warehouse but has a price, the price is displayed
using the following code, you can remove the price when the product is not in stock but has a price
function hide_price_if_out_stock_frontend( $price, $product ) {
if ( ! $product->is_in_stock() ) {
$price = apply_filters( 'woocommerce_empty_price_html', '', $product );
}
return $price;
} add_filter( 'woocommerce_get_price_html', 'hide_price_if_out_stock_frontend', 9999, 2 );
if the product is variable, this code removes the contents of the tag span, that's mean :
<span class="price">
# remove price #
</span>
for remove <span class="price"></span>
in product variable, it is possible through the following path : /plugins/woocommerce/includes/class-wc-product-variable.php | line 382
hook : how to change that part inside the hook?
function filter_woocommerce_available_variation( $variation_get_max_purchase_quantity, $instance, $variation ) {
return $variation_get_max_purchase_quantity;
} add_filter( 'woocommerce_available_variation', 'filter_woocommerce_available_variation', 10, 3 );
if the product is simple, this code removes the contents of the tag p, that's mean :
<p class="price">
# remove price #
</p>
for remove <p class="price"></p>
in product simple, it is possible through the following path : /plugins/woocommerce/templates/single-product/price.php
<?php
/**
* Single Product Price
*
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/price.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce\Templates
* @version 3.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
?>
<?php if ( ! $product->is_type( 'variable' ) ) { ?>
<p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>"><?php echo $product->get_price_html(); ?></p>
<?php } ?>
but i also want to remove single <span class="price"></span>
in product variable
i explained the question very clearly, i hope please answer.
My Answer:
>is_in_stock() && $product->get_price() ) {
$price = '';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'hide_price_if_out_stock_frontend', 10, 2 );
Rate this post
4 of 5 based on 9835 votesComments