Home » Php » php – How to add product variation SKU and PRICE in Woocommerce?

php – How to add product variation SKU and PRICE in Woocommerce?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I try to put the price and SKU beside the variation dropdown woocommerce but the code appear the price but it came out the HTML things inside the select drop down.


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

}

Above here is the code that I have attempted to make the price appear but how about to add SKU in the same line in the variation option?enter image description here
Up here is the result of the code that I have attempted. Hopefully, somebody can help me with this code.

How to&Answers: