src/Cart/ShoppingCart.php
private function findOrMakeItem(Buyable $buyable, $quantity = 1, $filter = [])
    {
        $order = $this->findOrMake();

        if (!$buyable || !$order) {
            return null;
        }

        $item = $this->get($buyable, $filter);

        if (!$item) {
            $member = Security::getCurrentUser();

            $buyable = $this->getCorrectBuyable($buyable);

            if (!$buyable->canPurchase($member, $quantity)) {
                return $this->error(
                    _t(
                        __CLASS__ . '.CannotPurchase',
                        'This {Title} cannot be purchased.',
                        '',
                        ['Title' => $buyable->i18n_singular_name()]
                    )
                );
                //TODO: produce a more specific message
            }

            $item = $buyable->createItem($quantity, $filter);
            $item->OrderID = $order->ID;
            $item->write();

            $order->Items()->add($item);

            $item->_brandnew = true; // flag as being new
        }

        return $item;
    }
private function findOrMakeItem(Buyable $buyable, $quantity = 1, $filter = [])
    {
        $order = $this->findOrMake();

        if (!$buyable || !$order) {
            return null;
        }

        $item = $this->get($buyable, $filter);

        if (!$item) {
            $member = Security::getCurrentUser();

            $buyable = $this->getCorrectBuyable($buyable);

            if (!$buyable->canPurchase($member, $quantity)) {
                return $this->error(
                    _t(
                        __CLASS__ . '.CannotPurchase',
                        'This {Title} cannot be purchased.',
                        '',
                        ['Title' => $buyable->i18n_singular_name()]
                    )
                );
                //TODO: produce a more specific message
            }

            $item = $buyable->createItem($quantity, $filter);
            $item->OrderID = $order->ID;
            $item->write();

            $order->Items()->add($item);

            $item->_brandnew = true; // flag as being new
        }

        return $item;
    }
Finds or makes an order item for a given product + filter.

@param Buyable $buyable  the buyable
@param int     $quantity quantity to add
@param array   $filter

@return OrderItem the found or created item
@throws \SilverStripe\ORM\ValidationException