How can I remove everything from a Commerce cart if a certain product is added, except that product?
This is the code I ended up using:

function MYMODULE_commerce_cart_product_add($order, $product, $quantity, $line_item) {
  if ($product-&gt;type == 'directory_subscription') {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);

    // Loop through the line items
    foreach ($order_wrapper-&gt;commerce_line_items as $delta =&gt; $line_item_wrapper) {
      // If this is the product we're keeping, make sure the quantity is always 1.
      if ($line_item_wrapper-&gt;commerce_product-&gt;product_id-&gt;value() == $product-&gt;product_id) {
        $line_item_wrapper-&gt;quantity-&gt;set(1);
        $line_item_wrapper-&gt;save();
      }
      else {
        // Remove any other product from the order
        commerce_cart_order_product_line_item_delete($order, $line_item_wrapper-&gt;line_item_id-&gt;value());
      }
    }
  }
}


If anyone has any suggestions on how to improve the above I'd be grateful.