How to add unit discount manually?


#1

Hello community!

I’m building an e-shop where also admin users can create orders on customer`s account.
In the cart view, admin user should be able to add absolute discount on each item in the cart, per unit.
So If the cart contains item of 10 apples each for 3€, there is a button that makes them cost each 2€ by adding 1€ discount.

To apply the discount, I tried the following code:

public function setItemUnitFixedDiscount($orderId, $orderItemId)
{
    $order = $this->orderRepository->find($orderId);
    $orderItem = $this->orderItemRepository->find($orderItemId);

    $promotion = $this->promotionFactory->createNew();
    $promotion->setCode("discount_1e");
    $promotion->setName("1e discount");

    $action = $this->promotionActionFactory->createUnitFixedDiscount(100, 'US_WEB');
    $promotion->addAction($action);

    $this->promotionRepository->add($promotion);
    $this->promotionApplicator->apply(?$orderItem?, $promotion);
}

but in the last line, I cannot apply the promotion on ``$orderItem, because it does not implementPromotionSubjectInterface`. So then what is the proper way to apply Unit Fixed Discount, please?


#2

I think you may apply Adjustment as a promotion
$adjustment = $this->container->get(‘sylius.factory.adjustment’)->createNew();
$adjustment->setType(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT);
$orderItem->addAdjustment($adjustment);