skip to Main Content

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 WooCommerceTemplates
 * @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.

2

Answers


  1. Chosen as BEST ANSWER

    Product simple : to remove the <p class="price"></p> when a simple product is not in stock.

    path : /plugins/woocommerce/templates/single-product/price.php

    file copy to child theme : /themes/name-theme-child/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 WooCommerceTemplates
     * @version 3.0.0
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }
    
    global $product;
    
    ?>
    <?php if ( ! $product->is_type( 'variable' ) && $product->is_in_stock() ) { ?>
    <p class="<?php echo esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); ?>"><?php echo $product->get_price_html(); ?></p>
    <?php } ?>
    

    Product variable : to remove the <span class="price"></span> when a variable product is not in stock.

    function customize_woocommerce_available_variation( $data, $product, $variation ) {
        if ( $data['is_in_stock'] ) {
            $data['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
        } else {
            $data['price_html'] = '';
        }
        return $data;
    } add_filter( 'woocommerce_available_variation', 'customize_woocommerce_available_variation', 10, 3 );
    

  2. Use this code

    add_filter( 'woocommerce_get_price_html', 'remove_price_sv', 10, 2 );
    function remove_price_sv( $price, $product ) {
        if ( ! $product->is_in_stock()) {$price = '';}
        return $price;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search