0

I have the following code which adds the price next to each variation:

//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 . ' <span>(' . woocommerce_price( $_product->get_price() ) . ')</span>';
}
return $term;

}

This code works perfectly. However I would like to have that code apply only to a specific product attribute (the slug is training-level). Is this possible with a if statement and conditional tags?

I also tried adding the following just bellow global $wpdb, $product; but didn't work:

$product_name = $product->get_attribute( 'training-level' );

Please help.

  • Can you post the output of var_dump( $product->get_attribute( 'training-level' ) );? – Xhynk Feb 28 '18 at 17:41
  • Hi... Is this what you mean? imgur.com/a/p5RCo – Miguel Gomes Feb 28 '18 at 17:50
  • Yes. So the code you added meant $product_name was set to the string Beginner, Intermediate, Advanced. You can make an if statement based on that – Xhynk Feb 28 '18 at 17:57
  • Thanks @Xhynk... I'm not quite sure how to do that... Please see the image representation here: imgur.com/a/Eu2MU . . If I wanted to show the prices only for the three variations for Training Level, and not for Flavor,. How would I do it? Do you mind providing an example? – Miguel Gomes Feb 28 '18 at 18:08
  • Sure thing, I've added an answer I think will work for you. You might need to change the string values based on what the value of $term actually is – Xhynk Feb 28 '18 at 21:02
0

You should be able to wrap the "return with price" section if the $term variable matches a certain value.

I'm unsure what $term actually contains, but this should work. If not, you can modify the values in the if statements.

//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] );

        if( $term == 'Beginner Level' || $term == 'Intermediate Level' || $term == 'Advanced Level' ){
            //this is where you can actually customize how the price is displayed
            return $term . ' <span>(' . woocommerce_price( $_product->get_price() ) . ')</span>';
        } else {
            return $term;
        }
    }
    return $term;
}
  • It worked just as I wanted it. I could not thank you enough! Wish I could buy you a beer or a coffee. CHeers – Miguel Gomes Feb 28 '18 at 22:14
  • I won't shill my ko-fi link (might be against ToS?) - but if you could hit the little check mark to mark the answer as accepted and/or upvote it, that would be excellent! Glad to help! – Xhynk Feb 28 '18 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.