• Resolved auludag

    (@auludag)


    Hello the php snippet below is used for showing Variation prices in dropdown menu.

    But For WC 3+ it gives warning, properties should not be accessed directly.

    I found the because $product->id, this warning generated.

    I changed to $product->get_id(). It isn’t working. And generating warning as well.

    How can i updated it so it can continue working without warnings?

    / show prices near dropdown
    // 
    
    //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] );
    
    $itemPrice = strip_tags (wc_price( $_product->get_price() ));
    //this is where you can actually customize how the price is displayed
    return $term . ' (' . $itemPrice . ')';
    }
    return $term;
    
    }  
    
Viewing 5 replies - 1 through 5 (of 5 total)
  • What warning do you get when you use $product->get_id() ?

    Thread Starter auludag

    (@auludag)

    @soft79 thanks for reaching out.

    The same warning:
    Incorrect Use of get_id() Product properties should not be accessed directly.

    +

    it doesn’t show prices in variation in dropdown menu when get_id() used.

    I don’t think that’s the exact warning you are receiving.

    Is it

    Notice: id was called incorrectly. Product properties should not be accessed directly.

    ?

    That means $product->id is still somewhere in your code.

    Michael K

    (@mikkamp)

    Automattic Happiness Engineer

    Hi,

    Instead of running a direct database query to retrieve the prices. I’d recommend to use the available functions in the classes.

    Since you are using a global of $product. This will already give you an instance of the class WC_Product_Variable: https://docs.woocommerce.com/wc-apidocs/class-WC_Product_Variable.html

    So I’d suggest to use the function “get_available_variations” to get all the variations. You can loop through these results to get the matching variation and it’s corresponding price.

    You can also use the function “get_variation_prices” to get all the prices in one go.

    madeincosmos

    (@madeincosmos)

    Automattic Happiness Engineer

    Hi there,

    We haven’t heard back from you in a while, so I’ll mark this thread as resolved now. If you have some more questions, feel free to start a new one.

    Cheers!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘How to update this code for WC 3+’ is closed to new replies.