php – Add text below WooCommerce short description if metabox value is true


For WooCommerce I have a custom metabox that sets products to unavailable if ticked.

The code below removes the add to cart button if a product is set to unavailable:

// Remove add to cart button if product is set to unavailable
function bja_replace_add_to_cart_button($purchasable, $product) {
    $product_unavailable = get_post_meta($product->get_id(), '_product_unavailable', true);

    if ($product_unavailable === 'on') {
        $purchasable = false;       
    return $purchasable;
add_filter('woocommerce_is_purchasable', 'bja_replace_add_to_cart_button', 10, 2);

In addition to removing the add to cart button, I’d like to add a line of text below the short description, to indicate the product is unavailable. For that I have the following code:

// Add text below short description
function bja_product_unavailable_text( $content ) {
    $content .= '<div class="bja_product_unavailable">This product is unavailable.</div>';
    return $content;
add_filter('woocommerce_short_description', 'bja_product_unavailable_text', 10, 2);

Both pieces of code work, but now I’d like to run the second piece only if the first one runs. What would be a good way of doing that?

Dvaeer 1 week 2023-05-25T03:17:06-05:00 0 Answers 0 views 0

Leave an answer