Skovsgaard
Skovsgaard

Reputation: 369

Changing WooCommerce availability text and include the original HTML

This is what I have right now:

enter image description here

The text is not wrapped in the standard WooCommerce HTML.

This is what I wan't:

The text output is sourrounded with the HTML WooCommerce

The text here is correctly wrapped in the standard WooCommerce HTML.

This is what I've done

I have added this to functions.php. I'm using Advanced Custom Fields, so I'm storing the contents (plain text) from that field in the $availability variable.

add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
    // Change Out of Stock Text
    if ( ! $_product->is_in_stock() ) {
        $availability['availability'] = the_field("text_stock", "option");
    }
    return $availability;
}

The text is correctly displayed on the product page, but the HTML normally wrapping the stock information (found in WooCommerce/templates/single-product/stock.php) is not being output.

I have tried this:

add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
    // Change Out of Stock Text
    if ( ! $_product->is_in_stock() ) {
        $availability['availability'] = '<p class="stock ' . esc_attr( $availability['class'] ) . '">' . the_field("text_stock", "option") . '</p>';
    }
    return $availability;
}

But that leaves me with this weird output:

enter image description here

My questions

Upvotes: 2

Views: 1749

Answers (1)

japick
japick

Reputation: 26

Have you tried get_field("text_stock", "option") instead of the_field("text_stock", "option")?

Upvotes: 1

Related Questions