This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Shekhar Bhandari 1 month, 1 week ago.

Assigned support staff: Shekhar Bhandari.

Author Posts
September 6, 2019 at 8:08 am #4523937

michaelP-74

Hi,
I use Woocommerce and WPML.
I am trying to display the price in the variation option dropdown.
It works in the default language using the following code but the variation dropdown field is empty on products in other languages

add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );

function display_price_in_variation_option_name( $term ) {
  global $wpdb, $product;

  if( isset( $product ) ) {

    $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );

    $term_slug = ( !empty( $result ) ) ? $result[0] : $term;

    $query = sprintf( "SELECT postmeta.post_id AS product_id
      FROM {$wpdb->prefix}postmeta postmeta
      LEFT JOIN {$wpdb->prefix}posts products ON ( products.id = postmeta.post_id )
      WHERE postmeta.meta_key LIKE 'attribute_%%'
      AND postmeta.meta_value = '%s'
      AND products.post_parent = %d", $term_slug, $product->get_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 .')';
    }
  }
}

Debugging shows this notice:
Notice: Undefined offset: 0

Do you have any suggestions?
Thanks for your help!

September 6, 2019 at 8:19 am #4524013

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

Can you please use the wpml_object_id for the ids and let me know if this helps?
https://wpml.org/wpml-hook/wpml_object_id/

Thanks

September 6, 2019 at 9:22 am #4524317

michaelP-74

Thank you for you quick reply!!

I made changes in line 19

$variation_id = apply_filters( 'wpml_object_id' .... 

The dropdown is still empty but the notice is gone.

I think I know now why it is not working, but the solution is a bit to advanced for me, can you help me out a bit further?

Thanks

September 6, 2019 at 9:27 am #4524369

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

I can only suggest you things here as custom code help falls outside the scope of our support.

From what I can see you are using terms, $product->get_id() so I request you to use the wpml_object_id here too, this should solve the issue.

Let me know if this helps.

Thanks

September 6, 2019 at 9:56 am #4524589

michaelP-74

Ok, thank you! I try my best

September 6, 2019 at 9:58 am #4524617

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Sure, Let me know how it goes.