Skip to content

Instantly share code, notes, and snippets.

@lukeholder
Last active July 1, 2016 07:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukeholder/f7f495b2f272b928d181411f77d8ac59 to your computer and use it in GitHub Desktop.
Save lukeholder/f7f495b2f272b928d181411f77d8ac59 to your computer and use it in GitHub Desktop.
<?php
craft()->on('commerce_discounts.onBeforeMatchLineItem', function ($event)
{
/** @var Commerce_LineItemModel $currentLineItem */
$currentLineItem = $event['lineItem'];
/** @var Purchasable $currentPurchasable */
$currentPurchasable = $currentLineItem->getPurchasable();
if ($currentPurchasable instanceof Commerce_VariantModel)
{
$order = $currentLineItem->getOrder();
$otherPurchasablesIdsOrder = [];
/** @var Commerce_LineItemModel $lineItem */
foreach ($order->lineItems as $lineItem)
{
$otherPurchasablesIdsOrder[] = $lineItem->purchasableId();
}
$relatedProducts = $currentPurchasable->relatedProductForBundle->find();
foreach ($relatedProducts as $product)
{
$relatedProductDefaultPurchasableIds[] = $product->defaultVariantId;
}
if (!empty($relatedProductDefaultPurchasableIds))
{
$allOtherProductsAreInCart = count(array_intersect($relatedProductDefaultPurchasableIds, $otherPurchasablesIdsOrder)) == count($relatedProductDefaultPurchasableIds);
if (!$allOtherProductsAreInCart)
{
$event->performAction = false;
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment