Search code examples
phpmysqlwoocommercemetadataproduct

Add Text to WooCommerce Product Page Based on metadata using hooks


I have products that allow for bidding, but no function that informs the customer about the minimum increment value for the actual bidding.

So, I need help in getting the meta_key and meta_value of that key and display the result of the value, unless empty.

Meaning: if the input field is set to 100, the meta_value of the meta_key is 100.

SELECT meta_value FROM `wp_postmeta` WHERE meta_key = '_bid_increment' AND meta_value = ''

That explains what I need, but the value of the meta_value is based on the field input for _bid_increment, and I do not know how to get it.

My plan is this:

add_action( 'woocommerce_before_add_to_cart_form',  'bid_increment_is' );
function bid_increment_is() {
echo '<div class="increment-for-this-product-is">The minimum increment for this product is: ????</div>';
}

Solution

  • First you need to get the product object. Then using get_meta() method with the correct meta key, you will get the desired value:

    add_action( 'woocommerce_before_add_to_cart_form',  'bid_increment_is' );
    function bid_increment_is() {
        global $product;
    
        $meta_value = $product->get_meta('_bid_increment');
    
        if ( ! empty($meta_value) ) {
            printf( '<div class="increment-for-this-product-is">The minimum increment for this product is: %s</div>', $meta_value );
        }
    }
    

    It should work.