Commerce: How can I dynamically create a discount for any given product?
I would turn this on it's head slightly (assuming you can express the discount itself, i.e. what it does specifically to the discount item, using normal Commerce discount rules).

If you can, then you can basically make the application of the discount conditional based on what's in the cart.

Here's some sketch code - obviously you might need to change the logic etc

public function onBeforeMatchLineItemHandler($event){

$lineItem = $event-&gt;params['lineItem'];
$discount = $event-&gt;params['discount'];

$cart = craft()-&gt;commerce_cart-&gt;getCart();

$idOfItemToPossiblyDiscount = whatever;
$idOfParentItem = whatever;
$idOfDiscount = whatever;

/First check which discount we're thinking about applying
if($discount-&gt;id == $idOfDiscount){

    $shouldDiscount = false;
    if ($lineItem-&gt;purchasable instanceof Commerce_VariantModel){
        //First check if this is the item that might get discounted
        if($lineItem-&gt;purchasable-&gt;id) == $idOfItemToPossiblyDiscount)){
            //Ok, so we have that item in the cart now loop through $cart to see if the 'parent' item is in the cart
            foreach ($cart-&gt;lineItems as $item){
                if ($item-&gt;purchasable-&gt;$id) == $idOfParentItem){
                    //do nothing, but finish here - Commerce will carry on and apply the discount as long as
                    //we dont set event-&gt;performAction = false;
                    $shouldDiscount = true;
                    break;
                }
            }

        }

    }

    if(!$shouldDiscount){
        $event-&gt;performAction = false;
    }

}


Obviously at the template end of things you can loop through what's in the cart and do similar logic to show messages etc - e.g. if parent item is in, and not the discount item, present the offer, etc....