Show image depending on the number in the custom fields


#1

Discuss WordPress related topics hereI am currently using Wordpress and I have a problem. I’m trying to develop a code that shows an image or another image based on the value of the custom field

In each product, for example, I have a variable called store_1 or store_2 apart from the main stock of the product.

What I want to do is a custom function for wordpress in the custom fields of the product in which it shows one image or another depending on the total amount of the value that custom field has. In the following code that I provide, it is for the wordpress stock itself, where it shows 3 images depending on the amount that there is.

For example, if there are more than five, it shows an image in green, if there are less than five, it shows an image in orange and if there is 0 or less than zero it shows an image in red.

This same code, I would like to customize it in each of those variables that I will embed in the product.

add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);

function wcs_custom_get_availability( $availability, $_product ) { global $product;

// Stock greater than 5 or enough stock
if ( $_product->is_in_stock() ) {
$availability[‘availability’] = __(’’,‘woocommerce’);

}

// Low stock < 5
if ( $_product->is_in_stock() && $product->get_stock_quantity() <= 5 ) {
$availability[‘availability’] = sprintf( __(’’, ‘woocommerce’), $product->get_stock_quantity());
}

// No stock
if ( ! $_product->is_in_stock() ) {
$availability[‘availability’] = __(’’, ‘woocommerce’);
}

return $availability;

}

And this is my problem, I do not know what code should be developed in the functions file of my wordpress but customized for that variable.

Thank you I await your response


#2

Use get_post_meta() function to fetch and output your custom field data.

$postid = $wp_query->post->ID;
echo get_post_meta($postid, ‘Custom_field_value’, true);
wp_reset_query();

The above query get the value from the custom field.
you can save get_post_meta value in a variable then you can call the value under the function wcs_custom_get_availability

$product->get_stock_quantity() <= 5

you can replace the <=5 with Custom_field_value.