Support

Account

Home Forums ACF PRO Getting WC product SKU from relationship field

Solved

Getting WC product SKU from relationship field

  • Hi there,

    I’m using the relationship field to get a cross-connection between various products on my webshop.

    This successfully returns the title and permalink etc, and even successfully returns custom fields from ACF. However, I can’t seem to get (for example) the default SKU from the product.

    I’m inside the $posts as $p loop, but I can’t seem to query the products by the ID.

    I’ve tried echo get_sku( $p->ID ); and many variations on that, but to no avail.

    Any suggestions?

  • Figured it out 🙂

    $product_id = $p->ID;
    $product = wc_get_product( $product_id );
    echo $product->get_sku();

    Find the post/product ID inside the loop, look for the SKU.

    I did notice however that it spits out all the data about the product, which doesn’t seem very optimised? Would there be any way to only get the SKU without getting the whole product data?

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Getting WC product SKU from relationship field’ is closed to new replies.