Search code examples
doctrine-ormshopwareshopware5

Round shipping cost in Shopware 5 based on condition


I need to round the shipping costs when an article is added till the order is completed based on if certain condition is active. I already tried subscribing the following events:

            'sOrder::getOrderById::after' => 'afterGetOrder',
            'Shopware_Modules_Order_SaveOrder_FilterParams' => 'filterOrderParams'

with the following implementation:

    public function filterOrderParams(\Enlight_Event_EventArgs $args)
    {
        $return = $args->getReturn();
        $return['invoice_shipping'] = ceil($return['invoice_shipping']);
        $return['invoice_shipping_net'] = ceil($return['invoice_shipping_net']);

        $subject->sShippingcostsNumeric = ceil($subject->sShippingcostsNumeric);
        $subject->sShippingcostsNumericNet = ceil($subject->sShippingcostsNumericNet);
        $args->setReturn($return);
    }

    public function afterGetOrder(\Enlight_Hook_HookArgs $args)
    {
        if (!$this->checkIfActive()) {
            return;
        }

        $return = $args->getReturn();
        $return['invoice_shipping'] = ceil($return['invoice_shipping']);
        $return['invoice_shipping_net'] = ceil($return['invoice_shipping_net']);

        $args->setReturn($return);
    }

But it does not seem to be working. I also tried

Shopware()->Modules()->Order()->sShippingcosts = ceil($sBasket['sShippingcosts']); but nothing. I know there is an event on order save, which I can hook and change the parameters, but that is too late as the cost in the cart and checkout would not be rounded up (not until the order is completed)

So is there a way to round shipping costs in shopware 5 ?

EDIT: Ideally it would also be great if the changes are stored in database directly so any further manipulation on the order shows / updates the correct rounded values.


Solution

  • For anyone looking, I ended up changing the template variable in the Frontend_Checkout event subscriber and letting it change the value in the database on Shopware_Modules_Order_SaveOrder_FilterParams. Hope it helps someone. Took me a while to figure this out. This hook sOrder::getOrderById::after was not needed at all, and doesn't seem to be working either.