Hi I had the following code that was working with old versions of Woocommerce to get an extract of the variation name and its price, which now doesn’t work anymore after the WC upgarde to 3.0

function shortcode_handler($atts, $content = "", $shortcodename = "", $meta = "")
{
    $output = "";
    $meta['el_class'];

    global $woocommerce, $product;
    if(!is_object($woocommerce) || !is_object($woocommerce->query) || empty($product)) return;

    // $product = wc_get_product();

    $output .= "<div class='av-woo-calendar-button ".$meta['el_class']."'>";
    ob_start();?>

        <table cellspacing="0" cellpadding="2">
            <thead>
                <tr>
                    <th scope="col" style="text-align:left; background-color:rgba(155, 199, 239, 0.5); vertical-align:middle;"><?php _e('Cruise', 'whale_dolphins'); ?></th>
                    <th scope="col" style="text-align:center;background-color:rgba(155, 199, 239, 0.5);"><?php _e('Places<br/>available', 'whale_dolphins'); ?></th>
                </tr>
            </thead>
            <tbody>
            <?php

            $args = array(
                'post_type'         => 'product_variation',
                'post_status'       => 'publish',
                'posts_per_page'    => -1,
                'orderby'           => 'title',
                'order'             => 'ASC',
               'meta_query' => array(
                    array(
                        'key'       => '_stock',
                        'value'     => array('', false, null),
                        'compare'   => 'NOT IN'
                    )
                )
            );

            $loop = new WP_Query( $args );

            while ( $loop->have_posts() ) : $loop->the_post();

                            $product = new WC_Product_Variation( $loop->post->ID );
                    if (get_the_title( $loop->post->post_parent ) == 'CSR Expeditions Payment' || get_the_title( $loop->post->post_parent ) == 'Pagamento Spedizioni CSR')
                    {       
                        $variation_formatted_name = $product->get_formatted_name();
                        $variation_name_array = explode("&ndash;", $variation_formatted_name);
                        $variation_name = $variation_name_array[2];
                        $variation_price = $variation_name_array[3];
                        $variation_sku = $variation_name_array[0];
                        $variation_name_only = explode(":", $variation_name);
                        $variation_name_only = $variation_name_only[1];
                ?>

                <tr>
                    <td><?php  echo $variation_name_only ."&ndash;" . $variation_price; ?></td>
                    <?php
                        if (intval ($product->stock) >0)
                        { ?>
                            <td style="text-align:center; font-size: 13px; background-color:#70C940; color:rgba(255,255,255,1);"><?php echo intval ($product->stock); ?></td>
                        <?php
                        }
                        else
                        { ?>
                            <td style="text-align:center; font-size: 13px; background-color:#D11E1B;color:rgba(255,255,255,1);"> <?php echo intval ($product->stock); ?></td>
                        <?php    
                        }

                    ?>
                </tr>
                <?php
                    }
            endwhile; 

            ?>
            </tbody>
        </table>

    <?php

    $output .= ob_get_clean();
    $output .= "</div>";

    return $output;
}

In particular I imagine that the following part of code is to be modified

$product = new WC_Product_Variation( $loop->post->ID );
                    if (get_the_title( $loop->post->post_parent ) == 'CSR Expeditions Payment' || get_the_title( $loop->post->post_parent ) == 'Pagamento Spedizioni CSR')
                    {       
                        $variation_formatted_name = $product->get_formatted_name();
                        $variation_name_array = explode("&ndash;", $variation_formatted_name);
                        $variation_name = $variation_name_array[2];
                        $variation_price = $variation_name_array[3];
                        $variation_sku = $variation_name_array[0];
                        $variation_name_only = explode(":", $variation_name);
                        $variation_name_only = $variation_name_only[1];

Any suggestion on how can I fix it? Thank you very much

Read more here: Woocommerce 3.0 get variation name


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: