#[Route(path: '/checkout/promotion/add', name: 'frontend.checkout.promotion.add', defaults: ['XmlHttpRequest' => true], methods: ['POST'])]
public function addPromotion(Cart
$cart, Request
$request, SalesChannelContext
$context): Response
{ return Profiler::
trace('cart::add-promotion',
function D
) use ($cart,
$request,
$context) { try { $code =
(string) $request->request->
get('code'
);
if ($code === ''
) { throw RoutingException::
missingRequestParameter('code'
);
} $lineItem =
$this->promotionItemBuilder->
buildPlaceholderItem($code);
$cart =
$this->cartService->
add($cart,
$lineItem,
$context);
// we basically show all cart errors or notices
// at the moments its not possible to show success messages with "green" color
// from the cart...thus it has to be done in the storefront level
// so if we have an promotion added notice, we simply convert this to
// a success flash message
$addedEvents =
$cart->
getErrors()->
filterInstance(PromotionCartAddedInformationError::
class);
if ($addedEvents->
count() > 0
) { $this->
addFlash(self::SUCCESS,
$this->
trans('checkout.codeAddedSuccessful'
));