0

I have an Application (app) node that when it gets created (hook_node_insert) I add line items (based on info in the app) to my cart. This works great.

Step 2 now is that when I add a new app, I would like to empty the cart (or remove the line items) before adding the new line item (so there is effectively only ever 1 item in the cart).

If I try to do $item->delete() or $cart->delete() before I add the new line item (in my node insert hook) then I get this error:

Drupal\Core\Entity\EntityStorageException: A cart order for type default, store 1 and account 1 already exists. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 847 of core\lib\Drupal\Core\Entity\Sql\SqlContentEntityStorage.php).

If I delete the line items after I add the new line item; then the cart almost looks correct. It shows only my new line item and the subtotal is the amount of my new line item; but the Total shown is the combined Total of the new line item added to the old line items (which have been successfully deleted).

My guess is there is something I need to do to "refresh" the cart after adding and then deleting line items?

  • This appears to be a caching issue (perhaps Views?) as a cache rebuild updates the total. Still good to know what exactly to clear in cache. – liquidcms Nov 27 at 20:24
-1

instead of using $item->delete() this works:

  $cart_manager = \Drupal::service('commerce_cart.cart_manager');
  foreach($items as $item) {
    //$item->delete();
    $cart_manager->removeOrderItem($cart, $item, TRUE);
  }

the 3rd parameter is 'save cart' and setting it to TRUE updates the cart Total.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.