How do I empty the cart? I want to programmatically empty the cart after these events.


Add to cart
Update quantity (in cart page)


In fact users must have only one product in their cart.
Are you sure you want to empty the cart? Or do you just want to limit the quantity of any product to one?

I recently had a similar situation. I added a hook_form_alter to hide the quantity so it can't be readily changed. There's not a specific FORM_ID you can use for an add to cart form because they get a hash added to them, so it looks like this:

function my_module_form_alter(array &amp;$form, FormStateInterface $form_state, string $form_id){
  if (strpos($form_id, 'commerce_order_item_add_to_cart_form_') === 0) {
    ... plus any other logic specific to your use case

    _my_module_specific_add_to_cart_form_alter($form, $form_state, $form_id);
  }
}

function _my_module_specific_add_to_cart_form_alter(array &amp;$form, FormStateInterface $form_state, string $form_id){
  // Doesn't make sense to buy more than one, so hide it.
  $form['quantity']['#type'] = 'hidden';

  ... plus any additional changes, e.g., I have this:
  // There's only one variation, so hide it. (
  $form['purchased_entity']['#type'] = 'hidden';
}