Requirements
I need to display the stock status (e.g., In stock
and Not in stock
) and price on all drop-down selectors for the product variation on our store. Note, the store only has one variation, named size
.
Also, I need the price to have no "includes GST" (or related) tax text on end of it. Should just be $10.83
for example. The price should still be a tax inclusive price, as per normal, but without the tax related text suffix.
Similar solutions that are not working in my scenario
There are a number of working solutions posted on StackExchange, but I found they only worked on the drop-down selectors generated by WooCommerce. Meaning, they don't affect selectors generated by some plug-ins (such as a plug-in we are using). I also didn't find any that don't put some tax related text after the price.
For example, this answer displays the stock status. But it didn't impact the drop-down selector generated by a third party plug-in. It only impacted the selector generated by WC on the single-product page.
Another example, this answer displayed the price (along with tax text, which I don't want). Again, it only impacted the WooCommerce generated selector (e.g., on single-product page).
Unaffected drop-down selector
The drop-down selector that is generated by a plug-in, is displayed on the product grid (in Shop and Archive pages). It allows user to add products to cart from those pages. It was not affected by the code examples mentioned above.
Partial solution that works (for Price)
I have found the following code successfully displays the price, with no tax text after it, and on all relevant drop-down selects (i.e., those generated by WooCommerce, and those generated by plug-ins).
function display_price_in_variation_option_name( $term ) {
$product = wc_get_product();
$id = $product->get_id();
if ( empty( $term ) || empty( $id ) ) {
return $term;
}
if ( $product->is_type( 'variable' ) ) {
$product_variations = $product->get_available_variations();
} else {
return $term;
}
foreach($product_variations as $variation){
if(count($variation['attributes']) > 1){
return $term;
}
foreach($variation['attributes'] as $key => $slug){
if("attribute_" == mb_substr( $key, 0, 10 )){
$taxonomy = mb_substr( $key, 10 ) ;
$attribute = get_term_by('slug', $slug, $taxonomy);
if($attribute->name == $term){
$term .= " (" . wp_kses( wc_price($variation['display_price']), array()) . ")";
}
}
}
}
return $term;
My Question
How can I modify the above code to include the stock status, after the price? In a way that will still use the same approach this code takes (namely, an approach that also works on drop-down selectors generated by the plug-in on my site)?
Note: I don't know what the difference is in the above approach, in terms of why it impacts all drop-down selectors, and not just the WC generated selectors. All I know, is that it's working.
My Answer:
To modify the above code to include the stock status after the price, you can make the following changes:
php
function display_price_and_stock_status_in_variation_option_name( $term ) {
$product = wc_get_product();
$id = $product->get_id();
if ( empty( $term ) || empty( $id ) ) {
return $term;
}
if ( $product->is_type( 'variable' ) ) {
$product_variations = $product->get_available_variations();
} else {
return $term;
}
foreach($product_variations as $variation){
if(count($variation['attributes']) > 1){
return $term;
}
foreach($variation['attributes'] as $key => $slug){
if("attribute_" == mb_substr( $key, 0, 10 )){
$taxonomy = mb_substr( $key, 10 ) ;
$attribute = get_term_by('slug', $slug, $taxonomy);
if($attribute->name == $term){
$term .= " (" . wp_kses( wc_price($variation['display_price']), array()) . ")";
$term .= " - " . ($variation['is_in_stock'] ? 'In stock' : 'Not in stock');
}
}
}
}
return $term;
}
This modified code will now display the price followed by the stock status in parentheses after the price in the variation option name. The stock status will be either "In stock" or "Not in stock" based on the availability of the variation. This code should work on all drop-down selectors, including those generated by third-party plugins on your site.
Rate this post
5 of 5 based on 1883 votesComments