Reputation: 125
I am looking to display each woocommerce product variation's price and stock status within the variation drop down like this: Price - Variation Name - Stock Status.
The following code gives me the Price and Variation Name within the dropdown, but I'm not sure of the additional code needed to add each variations current stock status?
// Add Variation Price to Drop Down
add_filter( 'woocommerce_variation_option_name',
'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( empty( $term ) ) return $term;
if ( empty( $product->id ) ) return $term;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
return wp_kses( woocommerce_price( $_product->get_price() ), array() ). ' - ' . $term . '';
}
return $term;
}
Thanks in advance for all your help.
Upvotes: 1
Views: 2957
Reputation: 125
Just incase anyone else wants to add a stock status with custom wording to the variation dropdown, here is the code I used based on the answer provided by Kashalo.
add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');
function display_price_in_variation_option_name( $term ) {
global $product;
if ( empty( $term ) ) {
return $term;
}
if ( empty( $product->id ) ) {
return $term;
}
$variation_id = $product->get_children();
foreach ( $variation_id as $id ) {
$_product = new WC_Product_Variation( $id );
$variation_data = $_product->get_variation_attributes();
$stock_status = $_product->get_stock_status();
$stock_status = str_replace( array('instock','outofstock','onbackorder'), array('In Stock','Out of Stock','Please allow a few extra days for delivery'), $stock_status );
foreach ( $variation_data as $key => $data ) {
if ( $data == $term ) {
$html = wp_kses( woocommerce_price( $_product->get_price() ), array() );
$html .= ' - ' . $term;
$html .= ( $stock_status ) ? ' - ' . $stock_status : '';
return $html;
}
}
}
return $term;
}
Upvotes: 2
Reputation: 3562
i have modified your function by removing the custom sql query and use WooCommerce built in functions to achieve the target goal.
add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');
function display_price_in_variation_option_name( $term ) {
global $product;
if ( empty( $term ) ) {
return $term;
}
if ( empty( $product->id ) ) {
return $term;
}
$variation_id = $product->get_children();
foreach ( $variation_id as $id ) {
$_product = new WC_Product_Variation( $id );
$variation_data = $_product->get_variation_attributes();
foreach ( $variation_data as $key => $data ) {
if ( $data == $term ) {
$html = wp_kses( woocommerce_price( $_product->get_price() ), array() );
$html .= ' - ' . $term;
$html .= ( $_product->get_stock_quantity() ) ? ' - ' . $_product->get_stock_quantity() : '';
return $html;
}
}
}
return $term;
}
Note : this function will work correctly if each product have one attribute not combined one.
OutPut:
Upvotes: 5