1

In WooCommerce, I am using the following functions to hide cart button when I set a custom stock status:

    //display the custom stock status on the pages
    add_action( 'woocommerce_after_shop_loop_item_title', 'wcs_stock_text_shop_page', 25 );
    //create our function
    function wcs_stock_text_shop_page() {
        //returns an array with 2 items availability and class for CSS
        global $product;

        $availability = $product->get_stock_status();
        
         if ( $product->get_stock_status() === 'noproduzione') {
            echo '<span style="color:#b20000;">Fuori produzione!</span>';
//hide purchase button if in "noproduzione"
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
    if ( $product->get_stock_status() === 'noproduzione' ) { 
        return false;
    }

    return $purchasable;
}
         }
    else if ( $product->get_stock_status() === 'onbackorder') {
        echo '<span style="color:#13b200;">Disponibile su ordinazione</span>';
         }
    else if ( $product->get_stock_status() === '10days') {
        echo '<span style="color:#13b200;">Disponibile in 10 giorni</span>';
         }
    else if ( $product->get_stock_status() === 'inarrivo') {
        echo '<span style="color:#e0c81d;">In arrivo</span>';
         }
    
    else if ( $product->get_stock_status() === 'outofstock') {
        echo '<span style="color:#b20000;">Terminato!</span>';
         }
    else echo '<span style="color:#53af00;">Disponibile!</span>';
 
}

But I receive the error when i try to edit the page the homepage I have:

Fatal error:
Cannot redeclare filter_is_purchasable_callback() (previously declared in functions.php)".

How to fix it?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Antonio
  • 25
  • 4

1 Answers1

1

You have inserted inside wcs_stock_text_shop_page() function the following existing code:

//hide purchase button if in "noproduzione"
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
    if ( $product->get_stock_status() === 'noproduzione' ) { 
        return false;
    }

    return $purchasable;
}

So the code block including the function filter_is_purchasable_callback() exists twice.

Look at your previous original question code.

So your related code should be instead:

//display the custom stock status on the pages
add_action( 'woocommerce_after_shop_loop_item_title', 'wcs_stock_text_shop_page', 25 );
//create our function
function wcs_stock_text_shop_page() {
    //returns an array with 2 items availability and class for CSS
    global $product;

    $availability = $product->get_stock_status();
    
     if ( $product->get_stock_status() === 'noproduzione') {
        echo '<span style="color:#b20000;">Fuori produzione!</span>';

     }
    else if ( $product->get_stock_status() === 'onbackorder') {
        echo '<span style="color:#13b200;">Disponibile su ordinazione</span>';
         }
    else if ( $product->get_stock_status() === '10days') {
        echo '<span style="color:#13b200;">Disponibile in 10 giorni</span>';
         }
    else if ( $product->get_stock_status() === 'inarrivo') {
        echo '<span style="color:#e0c81d;">In arrivo</span>';
         }
    
    else if ( $product->get_stock_status() === 'outofstock') {
        echo '<span style="color:#b20000;">Terminato!</span>';
         }
    else echo '<span style="color:#53af00;">Disponibile!</span>';
 
}

//hide purchase button if in "noproduzione"
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
    if ( $product->get_stock_status() === 'noproduzione' ) { 
        return false;
    }

    return $purchasable;
}

Checking that you have this code block only once:

//hide purchase button if in "noproduzione"
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
add_filter('woocommerce_variation_is_purchasable', 'filter_is_purchasable_callback', 10, 2 );
function filter_is_purchasable_callback( $purchasable, $product ) {
    if ( $product->get_stock_status() === 'noproduzione' ) { 
        return false;
    }

    return $purchasable;
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399