Last active
July 1, 2016 07:05
-
-
Save lukeholder/f7f495b2f272b928d181411f77d8ac59 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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