admin管理员组

文章数量:1323184

I 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'] = __('<img src=".png">','woocommerce');


}

// Low stock < 5
if ( $_product->is_in_stock() && $product->get_stock_quantity() <= 5 ) {
    $availability['availability'] = sprintf( __('<img src=".png">', 'woocommerce'), $product->get_stock_quantity());
}

// No stock
if ( ! $_product->is_in_stock() ) {
    $availability['availability'] = __('<img src=".png">', '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

I 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'] = __('<img src="https://myweb/images/fullstock.png">','woocommerce');


}

// Low stock < 5
if ( $_product->is_in_stock() && $product->get_stock_quantity() <= 5 ) {
    $availability['availability'] = sprintf( __('<img src="https://myweb/images/lowstock.png">', 'woocommerce'), $product->get_stock_quantity());
}

// No stock
if ( ! $_product->is_in_stock() ) {
    $availability['availability'] = __('<img src="https://myweb/images/notock.png">', '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

Share Improve this question edited Jul 13, 2018 at 14:48 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jul 13, 2018 at 14:44 FrancisFrancis 12 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

Better will be when you use css to change status colors.

WooCommerce Hooks: Actions and filters
Action and Filter Hook Reference
woocommerce/templates/content-single-product.php

function woocommerce_stock_badge() {
    global $product;

    if ( !$product->is_in_stock() ) {
        echo '<div class="stock red">no stock</div>';
    } else if ( $product->is_in_stock() && 5 >= $product->get_stock_quantity() ) {
        echo '<div class="stock orange">low stock</div>';
    } else if ( $product->is_in_stock() ) {
        echo '<div class="stock green">in stock</div>';
    }

}

// Add stock status to archive pages
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_stock_badge', 10 );

// Add stock status to single pages
add_action( 'woocommerce_before_single_product_summary', 'woocommerce_stock_badge', 10 );

I deleted the previous comment.

The code you provide is simply for the WC stock. What I need help to read that varible of the custom field and then create a condition if it is greater than 5 show an image, if it is less than 5 show another image and if it is equal to zero or empty show another image.

It would simply be to interpret the value of a custom field and if the number is greater or less show one data or another

I use this code of Michael

function woocommerce_stock_badge() {
    global $product;

    if ( !$product->is_in_stock() ) {
        echo '<div class="stock_red">Prodotto non Disponibile <br /><img src ="https://www.oasiverdegrasso/wp/wp-content/uploads/semaforo/semaforo_rosso.png" alt = "prodotto non disponibile" ></div>';
    } else if ( $product->is_in_stock() && $product->get_stock_quantity() <= 5 ) {
        echo '<div class="stock_orange">Disponibilà limitata <br /><img src ="https://www.oasiverdegrasso/wp/wp-content/uploads/semaforo/semaforo_arancio.png" alt = "prodotto poco disponibile" ></div>';
    } else if ( $product->is_in_stock() ) {
        echo '<div class="stock_green">Prodotto Disponibile <br /><img src ="https://www.oasiverdegrasso/wp/wp-content/uploads/semaforo/semaforo_verde.png" alt = "prodotto disponibile" ></div>';  
    }

}

// Add stock status to archive pages
add_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_stock_badge', 10 );

// Add stock status to single pages
add_action( 'woocommerce_before_add_to_cart_form', 'woocommerce_stock_badge', 9 );

but I have a problem:

I would like to manage the limited quantity not with a number (5) but with % of the product in stock ... so when in the stock have 30% of the inventory .... on the site display orange light traffic ...

can i do this?

本文标签: Show image depending on the number in the custom fields