We've discussed having Token replacement for SKUs with default SKU patterns per product type. This is a good compromise for sites that don't even use SKUs, in that every product can just use the [product:product-id] as the SKU and not have to think about it further. The way it should work is when a product form is saved, any Tokens in the SKU should be replaced so the evaluated string is stored in the database. Each product type should be able to specify a default SKU pattern that is set as the default value of the SKU field on the add form for products of that type.

When the form is submitted, it gets tricky. We need to be able to validate the SKU, which requires checking to ensure that only valid tokens are used, that the resultant SKU only contains valid characters, and that the resultant SKU is unique. The problem is that for new products, we don't have a fully populated $product object to use for Token replacement. We won't have that until the form submits, so during validation we need to fake values on the $product object and then after the product has been saved, update the SKU accordingly. This will be tricky, and my hunch is it will involve saving the SKU pattern with a unique value appended temporarily and then upon save re-loading the product and updating the sku field. :-/

I question whether or not this should be a core feature. On the one hand, it's hardly necessary for the framework. On the other, it's a feature that needs to be so tightly integrated to the product entry process and that a lot of people will need that I wouldn't consider it a bad thing for it to be core. In fact, it makes sense to be able to simply store a sku_pattern in the commerce_product_type table. If it's acceptable as a core feature, perhaps the question can become to what extent the default Product UI module allows people to configure SKU patterns or which Tokens are acceptable.

All that said, if this needs to be spun off into a custom module, I'm sure we have the necessary hooks to make it happen.

Comments

redben’s picture

One way pathauto does this (at least for uniqueness) is to accept anything then if the token-generated path exists, it ads a numeric prefix (1-2...etc)

neokrish’s picture

Assigned: Unassigned » neokrish

I will start working on a patch for this.

pcambra’s picture

Issue tags: +dcparisprintmarch
pcambra’s picture

roam2345’s picture

sub

BenK’s picture

Subscribing

rbayliss’s picture

Status: Active » Needs work
StatusFileSize
new2.18 KB

Spent some time on this today. It feels like this should be done at the entity level (hook_entity_presave()/hook_entity_insert()) rather than at the form level. I also think that the SKU validation should be moved out of the main validation handler and into an element_validate callback so it can be overridden by contrib without reimplementing the field validation. Patch for this is attached.

I agree that it would be sweet if SKUs could be auto-generated by core, but it seems like tokens would add too much (pretty hacky) code to be worth it. What about random SKUs?

rbayliss’s picture

StatusFileSize
new1.74 KB

Here is a proof of concept contrib module to do this. I'd be happy if this was put in core, but like I said, it feels a bit hacky.

rfay’s picture

Why don't you start by creating a sandbox project for it? That works a lot better than tarballs in a queue.

Thanks,
-Randy

tr’s picture

rbayliss’s picture

@rfay: Thanks for the reminder.

http://drupal.org/sandbox/rbayliss/1184312

rfay’s picture

Oh, and please add it to http://drupalcommerce.org/contrib - Thanks!

rfay’s picture

Assigned: neokrish » Unassigned

#7 should be Needs Review

rfay’s picture

Status: Needs work » Needs review

OK, trying again :)

rfay’s picture

Status: Needs review » Needs work
Issue tags: +low-hanging fruit, +dcparisprintmarch

The last submitted patch, 733332-7-token_support_product_skus.patch, failed testing.

marcin.wosinek’s picture

Status: Needs work » Closed (fixed)

Seems to be fixed by commerce_autosku

rszrama’s picture

Status: Closed (fixed) » Needs work

If I'm not mistaken, we still might need to leave this open either to merge some of Commerce AutoSKU into core or at least to remove some @todo comments from the product module.

vasike’s picture

Status: Needs work » Needs review
StatusFileSize
new2.6 KB

here is patch which removes the comments about the SKU token replacement.

i'm not sure about including Commerce AutoSKU here, even it contains a damn helpful functionality.

philsward’s picture

Retracting previous comment... Looks like my original issue has been updated in core.

chris matthews’s picture

Status: Needs review » Closed (outdated)

The 7 year old patch in #19 to commerce_product.forms.inc does not apply to the latest commerce 7.x-1.x-dev, but per #20 the original issue has been updated in core.