Add Stock Status boolean as custom attribute of WooCommerce variation selector - php

I am using the code found here, provided by LoicTheAztec. It adds stock status related text to the variation selector on WooCommerce products. It works as required.
I would, however, like to also add a custom attribute to each <option ... > in the dropdown selector.
My reason for this is that I am writing some jQuery that will be triggered by whether or not the selected option is in stock. That will be cleanest if each option has a stock attribute. For example, <option value=“1kg” class=“ ... “ stock-status=“true”>1kg - (in stock)</option>. In this example I’ve added a custom attribute called stock-status, which will either be true or false.
The code in use is:
// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ) {
$stock = $variation['is_in_stock'];
break;
}
}
return $stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';
}
// The hooked function that will add the stock status to the dropdown options elements.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute']; // The product attribute taxonomy
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $option );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
My knowledge of PHP isn’t sufficient to know the best way to go about this. One crude method would be to test if the result of get_stock_status_text contains the string (In or (No, and then add stock-status=“true” (or false) too the $html output accordingly. I can likely come up with a way to do that, but I suspect that’s not the best way to go about it. I am thinking it would be better to set a variable as true or false within the get_stock_status_text function, and then based on the value of that a stock-status custom attribute would have a value of true|false accordingly.
What would be an efficient way to add a custom attribute to the $html output, with a true|false value?

With a bit of playing around, it looks like I’ve come up with a solution. If there’s a more efficient or tidier way to achieve the result, I’d be very interested to know. This solution does at least have the desired output.
I have modified the code (from my question) to the following:
/******* ADD STOCK STATUS TO DROP DOWN SELECTOR ******/
// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ) {
$stock = $variation['is_in_stock'];
break;
}
}
return $stock == 1 ? ' - (In Stock)' : ' - (No Stock)';
}
function get_stock_status_bool( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ) {
$stock_bool = $variation['is_in_stock'];
break;
}
}
return $stock_bool == 1 ? 'true' : 'false';
}
// The hooked function that will add the stock status to the dropdown options elements.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute']; // The product attribute taxonomy
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $term->slug );
$stock_attr = get_stock_status_bool( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . ' stock-status="' . $stock_attr . '">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $option );
$stock_attr = get_stock_status_bool( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . ' stock-status="' . $stock_attr . '">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
The main addition is a new function, get_stock_status_bool. Which is a replica of get_stock_status_text except that it returns true or false.
This function is then called right after the get_stock_status_text function is called, and its value is assigned to $stock_attr.
$stock_attr has then been added into the $html value (e.g. <option value=“1 kg” selected=“selected” stock-status=“true” class=“ ...”>1kg - $150 - (In Stock)</option>

Related

trying to style woocommerce dropdown with adding a span inside php code

I have PHP code like this:
$html .= '<option value="' . esc_attr( $term->slug ) . '" '
. selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>'
. esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $price_html )
. '</option>';
I want to add <span class="XXX"> before $price_html variable and closing span tag after that, so on I can style the price. I tried the code below but no success:
$html .= '<option value="' . esc_attr( $term->slug ) . '" '
. selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>'
. esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . '<span class="XXX">' . $price_html . '</span>')
. '</option>';
Any help?
Edit: the code block i am trying to edit:
// Utility function to get the price of a variation from it's attribute value
function get_the_variation_price_html( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ){
return strip_tags( $variation['price_html'] );
}
}
}
// Add the price to the dropdown options items.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_price_in_attribute_dropdown', 10, 2);
function show_price_in_attribute_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute'];
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// Get and inserting the price
$price_html = get_the_variation_price_html( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $price_html ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// Get and inserting the price
$price_html = get_the_variation_price_html( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . ' = ' . $price_html ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
The reason why you don't see the desired output is in esc_html() function. Also, <span> tag inside a <option> tag doesn't get any CSS styles. You have to place desired class="XXX" inside the <option> tag.
For now you have something like:
<option value="123">escaped_price_html</option>
Replace lines:
'<option value="' . esc_attr( $term->slug ) . '" '
with
'<option class="XXX" value="' . esc_attr( $term->slug ) . '" '
and
. esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . '<span class="XXX">' . $price_html . '</span>')
with
. esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $price_html)
which will change the structure on:
<option class="XXX" value="123">escaped_price_html</option>
.x{
color: blue;
font-weight: bold;
}
<select>
<option value="1"><span class="x">TEXT1</span></option>
<option class="x" value="2"><span>TEXT2</span></option>
<option value="3"><span class="x">TEXT3</span></option>
</select>

display by stock quantity woocommorce

I'm preparing a shopping site with woocommerce. As in the sample link, I show the product stock information in the drop down menu. this is the code I use:
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
$stock_status = get_variation_stock_status( $product, $name, $term->slug );
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ).$stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
}
}
}
$html .= '</select>';
return $html;
}
function get_variation_stock_status( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ){
$variation_obj = wc_get_product( $variation['variation_id'] );
$stock_qty = $variation_obj->get_stock_quantity();
break;
}
}
return $stock_qty == 0 ? ' - ' . __(pll__('Stokta Yok'), 'mytheme') : ' - ' . $stock_qty . ' ' . __(pll__('adet Stokta'), 'mytheme-');
}
If the stock quantity is over 20, I don't want to show this information. how can I do that?
Just add a check to the get_variation_stock_status function:
function get_variation_stock_status( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if ( $variation['attributes'][$name] == $term_slug ) {
$variation_obj = wc_get_product( $variation['variation_id'] );
// if the stock of the product is greater than 20 it returns
if ( $variation_obj->get_stock_quantity() > 20 ) {
return '';
}
$stock_qty = $variation_obj->get_stock_quantity();
break;
}
}
return $stock_qty == 0 ? ' - (Out Of Stock)' : ' - ' . $stock_qty . ' In Stock';
}
The entire code (from the link in your comment) has been tested and works. Add it to your active theme's functions.php.

How to change "add to cart" in WooCommerce?

I wanna use <ul> <li> instead of <select> <option>
I enter this code in function.php but doesn't work:
function wc_dropdown_variation_attribute_options( $args = array() ) {
.
.
.
$html = '<div class="tt-wrapper"><div class="tt-title-options">اندازه:</div><ul id="' . esc_attr( $id ) . '" class="tt-options-swatch options-large ' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms(
$product->get_id(),
$attribute,
array(
'fields' => 'all',
)
);
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options, true ) ) {
$html .= '<li><a href="#" value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name, $term, $attribute, $product ) ) . '</a></li>';
}
}
} else {
foreach ( $options as $option ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
$html .= '<li><a href="#" value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option, null, $attribute, $product ) ) . '</a></li>';
}
}
}
$html .= '</ul></div></div>';
echo apply_filters( 'woocommerce_dropdown_variation_attribute_options_html', $html, $args ); // WPCS: XSS ok.
}
How can I change woocommerce HTML codes to my own HTML codes?
this is more detail for post this question
To override the default themes of WooCommerce you better have a "woocommerce" sub-directory on your theme folder.
for example:
/wp-content/themes/mytheme/woocommerce/
There you can specify exactly which of the themes you want to change.
Learn more about it here.
Also, you can hook to actions and filters. In the example you sent, you can hook to this filter: woocommerce_dropdown_variation_attribute_options_html to change it's final output.
Learn more about it here and here.

Add a custom class to variation options displaying "Out Of Stock" in Woocommerce

I'm using "https://stackoverflow.com/questions/47180058/how-to-add-variation-stock-status-to-woocommerce-product-variation-dropdown/47189725#47189725" answer code to show the text "In Stock" or "Out of stock" according to the stock status of the variation.
This is what I get:
I need to add a custom class only to these variants that showing text "Out of Stock".
How can this be done?
The following code will add the class outofstock to <option> html tag when the variation is "Out of Stock":
// Function that will check the stock status and display the corresponding additional text
function get_stock_status_text( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug )
$stock = $variation['is_in_stock'];
}
return $stock == 1 ? ' - (In Stock)' : ' - (Out of Stock)';
}
// The hooked function that will add the stock status to the dropdown options elements.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute']; // The product attribute taxonomy
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// HERE Added the function to get the text status
$stock_status = get_stock_status_text( $product, $name, $term->slug );
// HERE we add a custom class to <option> that are "Out of stock"
$option_class = $stock_status === ' - (Out of Stock)' ? ' class="outofstock"' : '';
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . $option_class . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $stock_status ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// HERE Added the function to get the text status
$stock_status = get_the_stock_status( $product, $name, $option );
// HERE we add a custom class to <option> that are "Out of stock"
$option_class = $stock_status === ' - (Out of Stock)' ? ' class="outofstock"' : '';
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . $option_class . '>' .
esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $stock_status ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.

Display price and stock status in dropdown variation attribute options of Woocommerce

On woocommerce on my variable products each variation has specific price.
How can I display the variation price and stock status in the attribute dropdown options?
Note: This only works when there is ONLY ONE dropdown select field (so one attribute for the variations set in the variable product). With multiple attributes (so multiple dropdown select fields) it displays something that can be wrong depending on the variations stock status attributes terms combination.
The following code will display the variation price and stock status in a unique dropdown variation attribute options:
// Utility function to get the price or the stock status of a variation from it's attribute value
function get_variation_price_stock_string( $product, $name, $term_slug ){
foreach ( $product->get_available_variations() as $variation ){
if($variation['attributes'][$name] == $term_slug ){
$stock_status = $variation['is_in_stock'] == 1 ? __('In stock') : __('Out of stock');
return ' (' . strip_tags( $variation['price_html'] ) . ') ' . $stock_status;
}
}
}
// Add the price and stock status to the dropdown options items.
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'show_stock_status_in_dropdown', 10, 2);
function show_stock_status_in_dropdown( $html, $args ) {
// Only if there is a unique variation attribute (one dropdown)
if( sizeof($args['product']->get_variation_attributes()) == 1 ) :
$options = $args['options'];
$product = $args['product'];
$attribute = $args['attribute'];
$name = $args['name'] ? $args['name'] : 'attribute_' . sanitize_title( $attribute );
$id = $args['id'] ? $args['id'] : sanitize_title( $attribute );
$class = $args['class'];
$show_option_none = $args['show_option_none'] ? true : false;
$show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __( 'Choose an option', 'woocommerce' );
if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
$attributes = $product->get_variation_attributes();
$options = $attributes[ $attribute ];
}
$html = '<select id="' . esc_attr( $id ) . '" class="' . esc_attr( $class ) . '" name="' . esc_attr( $name ) . '" data-attribute_name="attribute_' . esc_attr( sanitize_title( $attribute ) ) . '" data-show_option_none="' . ( $show_option_none ? 'yes' : 'no' ) . '">';
$html .= '<option value="">' . esc_html( $show_option_none_text ) . '</option>';
if ( ! empty( $options ) ) {
if ( $product && taxonomy_exists( $attribute ) ) {
$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( in_array( $term->slug, $options ) ) {
// Get the price and stock status
$price_stock_html = get_variation_price_stock_string( $product, $name, $term->slug );
// Insert the price and stock status
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) . $price_stock_html ) . '</option>';
}
}
} else {
foreach ( $options as $option ) {
$selected = sanitize_title( $args['selected'] ) === $args['selected'] ? selected( $args['selected'], sanitize_title( $option ), false ) : selected( $args['selected'], $option, false );
// Get the price and stock status
$price_stock_html = get_variation_price_stock_string( $product, $name, $term->slug );
// Insert the price and stock status
$html .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) . $price_stock_html ) . '</option>';
}
}
}
$html .= '</select>';
endif;
return $html;
}
Code goes in function.php file of your active child theme (or active theme). tested and works.
Based on:
Add the variation price to variable product dropdown item names in Woocommerce
How to add variation stock status to Woocommerce product variation dropdown
enter image description hereprice and stock status in dropdown
1: https://i.strong textstack.imgur.com/yvBUH.jpg
Thank you very much for this (very fast) reply. I use Flatsome theme and it works great.
As a additional question:
Is there a chance to convert "In Stock" into "Verfügbar / in Stock" and
Out of Stock into "Ausverkauft / Out of Stock"?
We have customers from Germany and other EU-countries; most of them will understand. But my older customer from Germany will face problems if I am not able to provide some short words in german.
Once again; thank you for your work.
ct

Categories