WooCommerce Get Product Attribute IDs with WPML

jam, fruit jar, label, fruit, jam, jam, jam, jam, label, label, label, label, label

In a recent Business Bloomer Club Slack thread, a developer was struggling to retrieve attribute IDs from WooCommerce product variations—something made more complex by the use of WPML and the Show Single Variations plugin by Iconic.

While getting the attribute slug was straightforward, converting that into a term ID or accessing the actual attribute ID for translated products wasn’t working as expected.

With multiple workarounds tested and a fair amount of debugging, the solution ended up being a mix of PHP functions and a deeper understanding of how WPML interacts with variable products and their translations. Here’s how it all unfolded.

The Problem: Attribute Slugs Instead of IDs

The original question was how to extract attribute IDs from product variations instead of just the slugs. This was essential due to a multilingual WooCommerce setup where queries based on attribute ID were more reliable than those based on slugs.

The variation object would only return slugs, for example:

$variation_attributes = $variation->get_attributes();
// Output: ['attribute_pa_style' => 'pink-party']

The Suggested Fix: Use get_term_by()

A workaround was proposed to convert the slugs into term IDs:

$variation_id = 123;
$variation = wc_get_product($variation_id);
if ( $variation ) {
    $variation_attributes = $variation->get_attributes();
    foreach ( $variation_attributes as $attribute_slug => $attribute_value ) {
        $taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_', '', $attribute_slug ) );
        $term = get_term_by( 'slug', $attribute_value, $taxonomy );
        if ( $term ) {
            echo 'Attribute: ' . $taxonomy . ' - Term ID: ' . $term->term_id . '<br>';
        }
    }
}

This successfully returned the term IDs—but only for non-translated products.

The Twist: WPML Gets in the Way

The user then discovered that using the wpml_object_id() filter to fetch the base language version of a variation was corrupting the product object. Instead of accessing variation-specific data, the code ended up retrieving attributes from the parent variable product, causing inconsistent results.

For example:

$baseProductId = apply_filters( 'wpml_object_id', $product->id, 'product', TRUE, 'en-gb' );
$baseProduct = wc_get_product( $baseProductId );
print_r( $baseProduct->get_variation_attributes() );

This returned all variation options of the parent product, not the variation’s actual attributes.

The Final Solution: Target Just the Term, Not the Product

The fix involved skipping the wpml_object_id() call for the whole variation and applying WPML logic only to the attribute term. That way, the variation remains untouched, and only the attribute’s ID gets translated.

Conclusion

Working with WooCommerce variations and WPML is not always straightforward, especially when you need precise data like attribute term IDs.

In this case, combining get_term_by() with careful WPML filtering—applied to attributes and not the product object—proved to be the winning strategy.

As always, digging into the nuances of WooCommerce and WPML pays off when building robust multilingual stores.

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Reply

Your email address will not be published. Required fields are marked *