ShoppingCart::getCorrectBuyable PHP Method

getCorrectBuyable() public method

Ensure the proper buyable will be returned for a given buyable… This is being used to ensure a product with variations cannot be added to the cart… a Variation has to be added instead!
public getCorrectBuyable ( Buyable $buyable ) : Buyable
$buyable Buyable
return Buyable
    public function getCorrectBuyable(Buyable $buyable)
    {
        if ($buyable instanceof Product && $buyable->hasExtension('ProductVariationsExtension') && $buyable->Variations()->count() > 0) {
            foreach ($buyable->Variations() as $variation) {
                if ($variation->canPurchase()) {
                    return $variation;
                }
            }
        }
        return $buyable;
    }