When creating a product variation, I would like to auto generate the SKU using a field in the referencing product display node.

The Node Token subtree is not shown in the available replacement patterns. I added the subtree to them by adding 'node' to the tree with this line:

$autosku['pattern']['#token_type'] = array('commerce-product','node');

However, when I reference a token such as, node:field-sku-component, the replacement doesn't occur and my skus look something like T100-nodefield-sku-component-001.

I'm not sure what I'm missing here. Maybe the referencing node is not available when the replacement occurs?

Comments

rbayliss’s picture

Issue summary: View changes
Status: Active » Closed (works as designed)

Yeah, the referencing node isn't available to the product. It makes sense when you think about it - the same product can be used in multiple product displays, so the product display node has the reference to the product, not the other way around.

jaylotta’s picture

The thing that is confusing me about your reply is that Commerce core uses the title field of the display node to create the title of the product when automatic title generation is enabled. So, it seems to me, at some point during the creation of the product the referencing product display node is available.

I see what you are saying about multiple displays for products but when creating product variations in the display node editor, perhaps you can get this information. The inline entity form module code which does this is as follows:

 // Generate the product title. Take the parent entity title as the base.
    if ($this->settings['autogenerate_title']) {
      $entity->title = entity_label($context['parent_entity_type'], $context['parent_entity']);
      $attributes = $this->attributes($entity->type);
      if (!empty($attributes)) {
        $wrapper = entity_metadata_wrapper('commerce_product', $entity);
        $attribute_values = array();
        foreach ($attributes as $field_name => $attribute) {
          $attribute_label = $wrapper->{$field_name}->label();
          if (!empty($attribute_label)) {
            $attribute_values[] = $attribute_label;
          }
        }

        if (!empty($attribute_values)) {
          $entity->title .= ' (' . implode(', ', $attribute_values) . ')';
        }
      }

Any comment will be greatly appreciated.

jaylotta’s picture

Status: Closed (works as designed) » Active
sic’s picture

How would you do this then? I'd like to have the referencing node to the SKU aswell. Strangely, there is a token for that but it doesnt work, this fella has the same problem but using :0: doesnt work either

jaylotta’s picture

I would use this because my SKUs are generated based upon a value in the product display node, the SKU_Prefix.

All product variations have SKU components that I would like to get from the referenced taxonomy and the variation itself.

So, all product variations for a specific node would have a SKU like so,

SKU_Prefix - Variation Component - Taxonomy 1 SKU Component - Taxonomy 2 SKU Component.

For example, a product variation which is for the display node, Basket, with a Finish of Brushed Nickel, and an Installation type of Stone would have the SKU

T100-001-BN-S

Hopefully this clarifies my specific use case.

maxplus’s picture

Hi,

did you find a solution for using a token of a field of the display node inside the sku?

Like already said, using [commerce-product:field-product-node:0:... does not work.

Thanks

UksusoFF’s picture

If you creates products on nodes edit page via https://www.drupal.org/project/inline_entity_form
You can use this token for get referenced node nid: [current-page:url:args:value:1]
Yes, this is very crooked but I can't find another way.
Also you need uncheck "Always regenerate SKU" option on product type page.

UksusoFF’s picture

nvahalik’s picture

Status: Active » Closed (works as designed)

It's worth noting that in many cases, products are generated without being referenced by a SKU until later on in the content/data creation process. As the author module stated, it's also possible for product entities to be related to many product displays (so which would it be?)