Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading