How to set licence role on commerce product programmatically in hook_update_n?

Created on 30 September 2016, almost 9 years ago
Updated 19 August 2025, 4 days ago

I want to create a commerce product with a licence role field value being set in a hook_update_n.

    function my_module_update_7000(&$sandbox) {
      $product = commerce_product_new('product');
      $product->sku = 'my_product';
      $product->title = 'My product';
      $product->uid = 1;
      $product->commerce_price[LANGUAGE_NONE][0]['amount'] = 10000;
      $product->commerce_price[LANGUAGE_NONE][0]['currency_code'] = 'EUR';
      // Set 'editor' as the role that will be assigned to a user when he buys this product
      $product->commerce_license_role[LANGUAGE_NONE][0] = array('value' => 4);
      commerce_product_save($product);
    }

The product gets created and in the ui the value is showing up correctly.
But the role is not being assigned when a user buys this product.
It isn't until I save the product via the ui that it is working.

I thought calling commerce_product_save would be sufficient to save the product correctly programmatically.

Is there something else I am overlooking?

πŸ’¬ Support request
Status

Closed: outdated

Version

1.3

Component

Code

Created by

πŸ‡³πŸ‡±Netherlands undersound3

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024