woocommerce variations - add post_meta to dropdown

I am currently working with Woocommerce. the following code currently add's the variation price to the dropdown on the product page:

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

}

I would like to edit the following line:

return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';

Rather than it pulling in the price, i would like to show custom_meta which has already been saved.

I tried replacing the above with:

return $term . ' (' . get_post_meta( get_the_ID(), '_wholesale_price', true ) . ')';

But it returns nothing. Does anyone know the correct way to achieve this?

Answers


You need to get the data first if it's outside the loop. Try this:

global $post;
return $term . ' (' . get_post_meta($post->ID, '_wholesale_price', true) . ')';

I manged to solve this by doing the following:

return $term . ' (' .  $value['data']->variation_id, '_wholesale_price', true ) . ')';

Need Your Help

Adding cells in GridView

c# asp.net

How can i add cell in specific column for each row in gridview,i want to use RowCreated Events.

PHP OOP Database Class - Is it really needed?

php oop mysqli

I'm currently learning to code using OOP in PHP and have quite often come across database classes and abstraction layers. I'm coding a CMS as a big challenge and I started to code a database class

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.