I am using a taxonomy vocabulary (fabrics) that has two additional fields: One for an image, one for an SKU code the store's admin will enter when creating. This field is included in the Bulk Creation module as a combination field.

When setting up the patterns for my product display, I can choose the label and/or the value of this field ([bulk_product:field_fabrics_colors-value], [bulk_product:field_fabrics_colors-label]), but I cannot access the extra fields.

My question: How would I go about extending the Tokens used by Bulk Creation so I can use those extra fields as part of the pattern? I am trying to get something like this: [bulk_product:field_fabrics_colors-sku-field]

Just looking for a hint of what sort of hooks/custom module I should modify to get this to happen.

Comments

sven.lauer’s picture

Sorry for not replying to this sooner.

You would have to implement hook_token_info_alter(), to add your token to the bulk_product type, something like

function foo_token_info_alter(&$data) {

  $data['tokens']['bulk_product']['foo'] = array(
    'name' => t("Foo"), 
    'description' => t("The foo."),
  );
}

And then, you have to implement hook_tokens(), specifying the replacement value for the new token, based on the $data parameter. $data['bulk_product']['combination'] is a map 'field_name' => field value array. You will have to extract the term ID (tid) from this array, load the term, and retrieve the value of the 'sku fragment' field.

Currently, you'll have to declare a token for each taxonomy field you are using.

I really do like the strategy you employ for getting non-tid replacements for taxonomy values, so hopefully, I can make this easier in the future.

3rdLOF’s picture

Thanks for the reply. I tried to implemented, but tokens are still one of the few things I have not quite figured out 100%, so what I ended up doing was cloning the BPC taxonomy module, remaning it accordingly (files, info, functions) and then simply use the existing token function and, loaded the term using the tid and extracted the SKu field from there , replacing it $value with its the new SKU value like this:

// this like a single value field.
      $value = $items[0]['tid'];
      $valt = taxonomy_term_load($value);
      $value2 = $valt->field_taxononmy_sku['und'][0]['value'];

      $replacements['values'][$field_name] = $value2;
      $labels = taxonomy_allowed_values($field);
      if (!empty($value2)) {
        // $value can be empty in the case of sample value generation for
        // an empty vocabulary.
        $replacements['labels'][$field_name] = $sanitize ? check_plain($labels[$value]) : $labels[$value];
      }

Not the prettiest, not the nicest, but works just the same. Of course, I do not need the stadard BPC Taxonomy reference, so I disable that as I think having both would have created some conflicts.

Looking forward to see if this idea can be implemented. I wish I had a better handle on Token so I could throw my help behind it, but I have the noose around my neck with this client.

EDITED: In case anyone sees this. I reverted the check_plain($labels[$value2]) : $labels[$value2]; to its original code as check_plain($labels[$value]) : $labels[$value]; That was not neccesary.

3rdLOF’s picture

Status: Active » Needs review

Set to needs review for now.

dwatts3624’s picture

I'm glad I've run across this thread since I was trying to do the exact same thing.

In my case, I added a field to the term called size abbreviation and would like to append it to the entered sku. Pretty much identical.

I attempted to setup the hooks and realized I was way in over my head. Do you have any suggestions on where we could find more information on doing that?

I know this isn't really a question for this thread but my efforts stopped at being able to output the data used in the hooks since dsm() doesn't seem to work in this context -- which I guess makes sense. Do you have any advice? :)

Any assistance would be greatly appreciated!

DW

dwatts3624’s picture

I started troubleshooting this again after some time and since I had the advantage of being out of it for so long I thought I'd test another route.

What I discovered is that auto_entitylabel and commerce_autosku will both override commerce_bpc's settings.

Aside from the fact that the user is entering an arbitrary title and sku in the BPC form the three work great together!

What would be really neat is if commerce_bpc could check to see if replacement patterns were set for the product type and allow the user to disable the title and sku fields in the settings -- reducing the number of steps to create a product. I'd take a stab at it myself but that goes way above my head.

swim’s picture

Your correct dwatts3624, both auto_entitylabel and commerce_autosku will override commerce_bpc's token generation.

I required a fine grain picking of tokens so quickly patched commerce_autosku, maybe this will help? http://drupal.org/node/1925850

I found myself in a similar situation requiring the sku to be auto generated but also to allow an administrator to enter a string which would be appended to the end of the sku. To achieve this you would need to perform another hook form alter maybe adding an extra submit handler and concatenating the results of both fields.

Cheers,

Balu Ertl’s picture

Title: How to extent the Tokens for taxonomy refernce fields. » How to extent the Tokens for taxonomy reference fields
Issue summary: View changes