1

I'm creating a webshop, that imports a lot of products from a csv file. I need to link to available accessories etc., which individually is determined and imported by the respectable products SKU.

I know it is possible to get the product ID with a simple products SKU with this:

wc_get_product_id_by_sku('PRODUCT_SKU');

Is there anyway to do the same, but with a variation sku?

2 Answers 2

9

EDIT: It seems that you can now use wc_get_product_id_by_sku() to retrieve a product_variation without any problems, see the other answer below.

I was stuck on a very similar problem. I couldn't find anything in the woocommerce code that would let you directly look up the parent product from the variation sku. But, as product variations are stored in the wp_posts table with the type product_variation, and the SKUs are stored in the wp_post_meta table, it's easy to look up the product variation using a meta query with the get_posts() wordpress function.

$args = array(
    'post_type'  => 'product_variation',
    'meta_query' => array(
        array(
            'key'   => '_sku',
            'value' => 'PRODUCT_SKU',
        )
    )
);
$posts = get_posts( $args);

This should return your matching product_variation custom post type.

Once you have this then you can look up the parent product using the column post_parent that WooCommerce uses to link a variation to the parent variable product.

Turning that into a function is pretty straight forward:

function wc_get_product_id_by_variation_sku($sku) {
    $args = array(
        'post_type'  => 'product_variation',
        'meta_query' => array(
            array(
                'key'   => '_sku',
                'value' => $sku,
            )
        )
    );
    // Get the posts for the sku
    $posts = get_posts( $args);
    if ($posts) {
        return $posts[0]->post_parent;
    } else {
        return false;
    }
}

I use this in my site and it works great. Hope that helps.

2
  • Thanks. Maybe not name it as you have in case WC eventually write a function with the same name ;) Commented Nov 10, 2020 at 3:40
  • Sir, reading 'product_variation' has solved what I have been working on tonight. If we were in the same room, I would owe you a drink. THANK YOU!
    – Dadou
    Commented Dec 7, 2021 at 2:49
3

The simplest way is to get Variation ID using wc_get_product_id_by_sku function then get post parent ID to get Product ID.

$variation_id = wc_get_product_id_by_sku('VARIATION_SKU');
$product_id = wp_get_post_parent_id($variation_id);
3
  • This code doesn't work because it looks up the sku on "products" post type. You can see the implementation here: hookr.io/functions/wc_get_product_id_by_sku Variations are stored as "product_variation" post type, so it needs a custom function to find a product with a variation sku.
    – Tim
    Commented Jul 22, 2021 at 8:42
  • These are the default woocommerce functions and works fine for me. wc_get_product_id_by_sku returns product id and that's obviously of "product_variation" post type but then wp_get_post_parent_id is used to get parent product and that will be of "products" post type. I hope this clarifies your point. Commented Nov 30, 2023 at 6:23
  • You are correct Abhijit, that seems to work fine now. Maybe when I wrote the answer back in 2018 the API was slightly different. I'm sure wc_get_product_id_by_sku didn't work for me then. the hookr.io link in my last comment doesn't seem to work anymore either, so I can't check. I'll edit my original answer and point them to your response.
    – Tim
    Commented Dec 4, 2023 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.