Search code examples
wordpresswoocommerceproducthook-woocommerce

Issue hide product single price for variable products on WooCommerce shop and archives pages


Im using this snippet to hide single price if the product is variable. Works fine but not apply on first product variable of loop:

add_action( 'woocommerce_after_shop_loop_item_title', 'hide_single_price' );

function hide_single_price() {
    global $product;
    if ( $product->is_type( 'variable' ) ) {
        remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
    }
}

Any idea why?

enter image description here


Solution

  • I don't immediately see a problem with your current code, what you can try:

    • Adjust the priority value
    • Echo the text 'debug'. So you can check if that is displayed for the first product.
    function hide_single_price() {
        // Get the global product object
        global $product;
    
        // Is a WC product
        if ( is_a( $product, 'WC_Product' ) ) {
            if ( $product->is_type( 'variable' ) ) {
                // Debug purposes, delete afterwards!!
                echo 'DEBUG!';
                
                remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
            }
        }
    }
    add_action( 'woocommerce_after_shop_loop_item_title', 'hide_single_price', 5 );