4

I am trying to retrieve the product IDs. I can find a product SKU by looking on the product page and using commerce_product_load_by_sku() to load it.

How can I find the actual ID? I would like to use it in commerce_cart_product_add_by_id().

5

According to the docs I found here commerce_product_load_by_sku($sku) will return a $product object and one of the properties is product_id.

$product = commerce_product_load_by_sku($sku);
drupal_set_message($product->product_id);

The product object looks something like this:

[product_id] => 5
[sku] => EX151
[type] => class
[language] => und
[title] => Example Product
[uid] => 1
[status] => 1
[created] => 1316634672
[changed] => 1316639451
[data] => 
[commerce_price] => Array
    (
        [und] => Array
            (
                [0] => Array
                    (
                        [amount] => 12500
                        [currency_code] => USD
                        [data] => Array
                            (
                                [components] => Array
                                    (
                                    )

                            )

                    )

            )

    )
0

If you do not need the id to be retrieved in a function but just for a quick check, do

  1. Install module commerce_backoffice, if it is not already part of your Drupal Commerce install.
  2. In views, find the view Commerce Backoffice: All Product variations. View this view.
  3. Hover over a product title. See the product_id as the last part of the corresponding status message in your browser, ie. example.com/admin/commerce/products/1.
  4. You may want to alter the view by adding a column showing the product_id directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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