Skip to content

Instantly share code, notes, and snippets.

@jaymiejones86
Created September 23, 2014 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaymiejones86/7f82c951502306252164 to your computer and use it in GitHub Desktop.
Save jaymiejones86/7f82c951502306252164 to your computer and use it in GitHub Desktop.
Woocommerce Add Prices Beside Variant Items
//Add prices to variations
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$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] );
//this is where you can actually customize how the price is displayed
return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';
}
return $term;
}
@dimitrievski5
Copy link

Hello,

Before, thanks for the code.
I use the code for display price in variation name.
But, i have more variations options with more terms. I set to show price in only one attribute/variation, but the price to the name is not changed when i select additional terms.

Best Regards,
Martin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment