craftcms / digital-products

Sell digital products with Craft Commerce.
https://plugins.craftcms.com/digital-products
MIT License
18 stars 9 forks source link

Make title optional when creating product #55

Closed kevadamson closed 3 years ago

kevadamson commented 4 years ago

My use case is that I have entries that are then chosen when creating the product via the entries field type in Craft.

So with that in mind, it would be good if the title is generated from the title of the entry selected, rather than also having to duplicate it in the mandatory title field. Make sense?

More background is that each entry is details for an event, I'm using the events plugin for setting tickets and dates etc. But the event could be just content that is accessed online at anytime, or perhaps added as something to buy after the physical event (perhaps the event was filmed, and/or slides become available to access after the event), so I'm using digital products plugin for this scenario.

Thanks :)

nfourtythree commented 3 years ago

Hi @kevadamson

Thank you for your message.

It is possible for you to do this at the moment using the Product::EVENT_BEFORE_SAVE event.

In a cost module or plugin you could use the following code:

Event::on(\craft\digitalproducts\elements\Product::class, \craft\digitalproducts\elements\Product::EVENT_BEFORE_SAVE, function(ModelEvent $event) {
    // Custom logic to work out the new title.
    $newTitle = 'My New Title';

    $event->sender->title = $newTitle;
});

$event->sender is the digital product element, so you can update the title param to whatever you need. Whether it is the need to check any custom fields or whatever you might need.

At the moment I can't see a need for making the title optional, with the above code being the best way to achieve the goal. Therefore, I am going to close this ticket.

Thanks!