A
A
Andrey Plyuta2021-12-15 21:43:50
WordPress
Andrey Plyuta, 2021-12-15 21:43:50

How to display a custom field on the product page if it is out of stock?

I display a custom field on the product page using the woocommerce_before_add_to_cart_form hook. Everything is displayed well when the product is in stock, but when it is not, the output of the fields disappears. How can you decide?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aricus, 2021-12-16
@Aricus

If you think logically, you write before adding to the cart. If the product is out of stock, there is no addition to the cart, and the hook does not work. You need to use some other hook.

A
Andrey Plyuta, 2021-12-16
@motor4ikkk

I figured out the output when out of stock, now I can’t place notifications about availability above my block.
61bb00bac2846495802417.png

add_action( 'woocommerce_single_product_summary', 'ruta_custom_order_view', 30 );
function ruta_custom_order_view() {
  global $post, $product;
    $custom_order_value = get_post_meta( $post->ID, '_custom_order_field', true );
  if ( $custom_order_value ) {
        $custom_order_txt = get_field('custom_order_content');
        $custom_order_title_value = get_field('custom_order_title');
    ?>
    <div class="custom_order_field">
        <div class="custom_order_title"><?php echo($custom_order_title_value); ?></div>
            <div class="custom_order_content">
      <?php 
            echo ($custom_order_txt); 
            echo  "</div></div>"; }
    else {
        //null
   
    }
}
?>

Please help me change the provisions of the information about the availability of goods.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question