Order item title is being set two times when adding the product to cart.
One from OrderItemStorage::createFromPurchasableEntity and one from AddToCartForm::buildEntity.

Now if for example we want to alter OrderItem->title using the create (or maybe more suitable fields_init_values event), the value is being overwriten from the AddToCartForm::buildEntity set.

So I believe that the second set should go away...

Comments

petros created an issue. See original summary.

sagesolutions’s picture

I agree.

I tried creating an order item manually and overriding the title.

//Create the order item
    $order_item = OrderItem::create([
      'type' => 'default',
      'purchased_entity' => 1,
      'quantity' => 1,
      'uid' => \Drupal::currentUser()->id(),
      'title' => "Permit for: " . $node->getTitle(),
    ]);

However, because I'm using the purchased entity, the title gets overridden, most likely from:

    $entity->setTitle($purchased_entity->getOrderItemTitle());

if I create an OrderItem without setting the purchased_entity, the Order Item title stays set, but I lose the price info from the purchased entity.

skdrupal88’s picture

Status: Active » Needs review
StatusFileSize
new851 bytes

Had the same problem, attached patch should fix it.

Status: Needs review » Needs work

The last submitted patch, 3: commerce-commerce_item_title-3008173-3.patch, failed testing. View results

roblog’s picture

Hi. I'm trying to use the CartEntityAddEvent event to overwrite the title after it is added to the cart, but it is not working, even after applying the patch. Any ideas?

roblog’s picture

UPDATE: My bad, it is working. The Cart view was displaying the purchased Entity title rather than the OrderItem title. I tweaked the view and everything is good again.

TylerMarshall’s picture

It looks like the tests are failing because we are changing the site language to French, which updates the title to the French version of it. If we leave the current patch in #3 in, this functionality breaks, and we get the English version on the french site.

I am not sure if we want to change the title here, or in a different area, or handle this different all together. I will try and get a test up with the current patch that respects the translation.

anas_maw’s picture

Patch in 3 worked for me as expected

no sssweat’s picture

StatusFileSize
new66.42 KB

Should be noted that order item title field should be added to the form cart view & the summary view, and the purchased entity item field should be removed.

shuhiab’s picture

The fix still not available, I am facing the same issue

jglynn’s picture

This is also causing me issues with trying to programmatically update order item title in the cart. Seems like this would be a fairly common use case?

johnpitcairn’s picture

It looks like the tests are failing because we are changing the site language to French, which updates the title to the French version of it. If we leave the current patch in #3 in, this functionality breaks, and we get the English version on the french site.

If an implementation is customizing the order item title, then wouldn't it also be up to that implementation to handle translating the customized title if appropriate? And perhaps the test should accommodate this possibility?

Maybe what is required here is a flag similar to overridden_unit_price. You'd set overridden_title to TRUE, and if that's set the cart shouldn't mess with it and tests should not check translated titles. You'd need to write a test for that yourself, if you had translation functionality in place for overridden titles.

Some maintainer input would be good.

johnpitcairn’s picture

Title: Order item title is being set two times when adding to cart. » Custom order item title is overwritten when adding to cart.
narendra.rajwar27’s picture

Version: 8.x-2.9 » 8.x-2.x-dev
Status: Needs work » Needs review
StatusFileSize
new904 bytes

Re-rolled patch from comment #3

Status: Needs review » Needs work

The last submitted patch, 14: 3008173-14.patch, failed testing. View results

facine’s picture

Status: Needs work » Needs review
StatusFileSize
new4.58 KB

I'm attaching a new approach as recommended by @John Pitcairn

facine’s picture

The errors in the tests do not appear to be related to our changes.

rgeerolf’s picture

StatusFileSize
new4.6 KB

I updated the latest patch from facine because the database update collided with the address 2.0 database update added after #16 was created.

jsacksick’s picture

hm... Feels a bit overkill but probably the only way to preserve a custom order item title on refresh...
We need to update the order item kernel test as well to test the new method.

jsacksick’s picture

Status: Needs review » Fixed

Updated the order item kernel test and committed the patch. Thanks everyone!

  • jsacksick committed a0d1b918 on 8.x-2.x authored by facine
    Issue #3008173 by facine, skorzh, narendra.rajwar27, rgeerolf, John...

  • jsacksick committed bf7f46ad on 3.0.x authored by facine
    Issue #3008173 by facine, skorzh, narendra.rajwar27, rgeerolf, John...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.