I've been trying to figure out the best way to hook in and replace the title of my variations in code when they're saved. What I'd like to do is have it generate an automatic title, but change something about that title from another module in code automatically.

The problem is that we have some "N/A" attribute values, and the auto titles are ugly because they have sometimes repeated "N/A, N/A" values in their titles, and our client doesn't want those there (and I can understand why). Since their products are so varied that we can't really get around some attributes not applying to some products, and since attribute fields require a value, the N/A value was the way around that. it seems the only real way around the title issue is to override the variation titles and remove those N/A values.

I've tried ProductEvents::PRODUCT_VARIATION_INSERT and ProductEvents::PRODUCT_VARIATION_UPDATE but trying to set the title there simply has no effect, it gets overridden after that again if I have automatic titles enabled.

Is there a later hook I should use, or another recommended way that I could achieve the desired effect of modifying the auto-generated variation title (automatically, every time a variation is saved)?

Issue fork commerce-2846895

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bmcclure created an issue. See original summary.

replicaobscura’s picture

Issue summary: View changes
replicaobscura’s picture

Status: Active » Needs review

Here's a simple approach that dispatches a new event after generating the title, before saving it, to allow it to be overridden:

https://github.com/drupalcommerce/commerce/pull/707

https://github.com/drupalcommerce/commerce/pull/707.patch

replicaobscura’s picture

I haven't had time to create tests for this yet, though the patch still does work well for me in the latest commerce versions.

cosolom’s picture

You can use hook_presave if you will uncheck "Generate variation titles based on attribute values" and move part code for generate title in your custom hook_presave.

replicaobscura’s picture

I think that would be a possibility, but the auto-generated titles are often useful and may just need to be customized like in my case.

scottsawyer made their first commit to this issue’s fork.

scottsawyer’s picture

Version: 8.x-2.x-dev » 3.x-dev

Updating target branch.

scottsawyer changed the visibility of the branch 2846895-overriding-product-variation2 to hidden.

scottsawyer’s picture

Hm, there is a test failure https://git.drupalcode.org/issue/commerce-2846895/-/jobs/6267257

I am not sure how that is related. There's one coding standards issue, easily fixed.

FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------------------------------------------------
 56 | ERROR | [x] Parameter comment must end with a full stop (Drupal.Commenting.FunctionComment.ParamCommentFullStop)
jsacksick’s picture

Status: Needs review » Needs work

Ok, I'm not against the idea.

Can we please address the feedbacks I gave in the MR? Let's use property promotion in the event constructor.
Let's pass the variation first, then the title and also define return types for methods.

jsacksick’s picture

Title: Overriding product variation titles » Add an event to allow overridding the generated variation title
Category: Support request » Feature request