Forums › Forums › OroCommerce › Adding custom order totals?
This topic contains 4 replies, has 3 voices, and was last updated by Andrey Yatsenko 3 years, 10 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
-
CreatorTopic
-
April 4, 2019 at 8:38 am #38440
I have a few additional fees to include to every order. They are: Handling fee, processing fee and shipping fee. I’m using setOverriddenShippingCostAmount on my Order object in order to set the custom shipping fee and it works great. However, I have no idea how to add custom fees. I’m able to easily add custom discounts, but not fees. What’s the proper way of doing this?
-
CreatorTopic
-
AuthorReplies
-
April 5, 2019 at 5:28 am #38451
You can add a custom order subtotals, like taxes or shipping.
It is required to create a subtotal provider, that implements SubtotalProviderInterface and register it at the container as a tagged service
See:
1) https://github.com/oroinc/orocommerce/blob/master/src/Oro/Bundle/TaxBundle/Provider/TaxSubtotalProvider.php#L13
2) https://github.com/oroinc/orocommerce/blob/master/src/Oro/Bundle/TaxBundle/Resources/config/services.yml#L527-L536July 19, 2019 at 4:50 am #39972Hello – I have the same requirement for adding a % of the order total as “processing fee” to each order. I have looked into Andrey’s suggested code files, but am struggling to set started on this.
Not sure if Vincent managed to implement Andrey’s suggestions and get it working – can someone please share some sample code to apply a custom processing fee (as % of the order value) and I can use it to implement my design?
Thanks
July 23, 2019 at 2:54 am #39977Hi Andrey – Can you please look at the code below and advise where I am going wrong or what have I missed?
I have tried building a new bundle with the following folder structure:
CustomBundle\CustomServiceFeeBundle.php
CustomBundle\Provider\CustomServiceFeeProvider.php
CustomBundle\Resources\config\services.yml
CustomBundle\Resources\config\oro\bundles.ymlBelow are the files:
services.ymlPHP1234567891011services:oro_tax.provider.subtotal_customservicefee:class: 'CustomFeeBundle\Provider\CustomServiceFeeProvider'public: falsearguments:- '@translator'- '@oro_tax.provider.tax_provider_registry'- '@oro_tax.factory.tax'- '@oro_tax.provider.taxation_settings_provider'tags:- { name: oro_pricing.subtotal_provider, priority: 30 }CustomServiceFeeProvider.php
PHP123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113<?php// src/CustomServiceFeeBundle/Provider/CustomServiceFeeProvider.phpnamespace CustomServiceFeeBundle\Provider;use Oro\Bundle\PricingBundle\SubtotalProcessor\Model\Subtotal;use Oro\Bundle\PricingBundle\SubtotalProcessor\Model\SubtotalProviderInterface;use Oro\Bundle\TaxBundle\Exception\TaxationDisabledException;use Oro\Bundle\TaxBundle\Factory\TaxFactory;use Oro\Bundle\TaxBundle\Model\Result;use Symfony\Component\Translation\TranslatorInterface;class CustomServiceFeeProvider implements SubtotalProviderInterface{const TYPE = 'custom_servicefee';const NAME = 'oro_tax.subtotal_customservicefee';const LABEL = 'custom.subtotal.providers.servicefee.label';const SUBTOTAL_ORDER = 600;/*** @var TranslatorInterface*/protected $translator;/*** @var TaxProviderRegistry*/protected $taxProviderRegistry;/*** @var TaxFactory*/protected $taxFactory;/*** @var TaxationSettingsProvider*/protected $taxationSettingsProvider;/*** @param TranslatorInterface $translator* @param TaxProviderRegistry $taxProviderRegistry* @param TaxFactory $taxFactory* @param TaxationSettingsProvider $taxationSettingsProvider*/public function __construct(TranslatorInterface $translator,TaxProviderRegistry $taxProviderRegistry,TaxFactory $taxFactory,TaxationSettingsProvider $taxationSettingsProvider) {$this->translator = $translator;$this->taxProviderRegistry = $taxProviderRegistry;$this->taxFactory = $taxFactory;$this->taxationSettingsProvider = $taxationSettingsProvider;}/*** {@inheritdoc}*/public function getName(){return self::NAME;}/*** {@inheritdoc}*/public function getSubtotal($entity){$subtotal = $this->createSubtotal();try {$tax = $this->getProvider()->getTax($entity);$this->fillSubtotal($subtotal, $tax*10/18); //converting 18% tax to 10% service fee} catch (TaxationDisabledException $e) {}return $subtotal;}/*** @return Subtotal*/protected function createSubtotal(){$subtotal = new Subtotal();$subtotal->setType(self::TYPE);$label = self::LABEL;$subtotal->setLabel($this->translator->trans($label));$subtotal->setVisible(false);$subtotal->setSortOrder(self::SUBTOTAL_ORDER);return $subtotal;}/*** @param Subtotal $subtotal* @param Result $tax* @return Subtotal*/protected function fillSubtotal(Subtotal $subtotal, Result $tax){$subtotal->setAmount($tax->getTotal()->getTaxAmount());$subtotal->setCurrency($tax->getTotal()->getCurrency());$subtotal->setVisible((bool)$tax->getTotal()->getTaxAmount());$subtotal->setData($tax->getArrayCopy());return $subtotal;}/** {@inheritdoc} */public function isSupported($entity){return $this->taxFactory->supports($entity);}/*** @return TaxProviderInterface*/private function getProvider(){return $this->taxProviderRegistry->getEnabledProvider();}}CustomServiceFeeBundle.php and bundles.yml simply have new bundle definition.
bundles.ymlPHP12bundles:- { name: CustomServiceFeeBundle\CustomServiceFeeBundle, priority: 250 }CustomServiceFeeBundle.php
PHP123456789<?phpnamespace CustomServiceFeeBundle;use Symfony\Component\HttpKernel\Bundle\Bundle;class CustomServiceFeeBundle extends Bundle{}Unfortunately, this is not working – not sure where I am going wrong. Can you please have a look and advise?
Thanks
July 23, 2019 at 3:26 am #39978Troubleshooting new bundle with the customization
First, If you have an error, read the message, it usually contains an answer to what is wrong, if doesn’t help, try to google that message text.
When you don’t have any errors follow the below steps:
1) First, check that your bundle is loaded, it can be done with the built-in Symfony commandShell1php bin/console debug:container --parameter=kernel.bundles --format=jsonIf you are doing everything right on this step the bundle will appear in the list.
Note: it is highly recommended to set the bundle priority to the high value so you will be able to override configuration from the previously loaded bundles.
If doesn’t work, check https://oroinc.com/orocrm/doc/current/dev-guide/cookbook/how-to-create-new-bundle2) If you have registered the service, check that this service is loaded. Again there is a built-in Symfony command to do that
Shell1php bin/console debug:container --show-private <service-name>where <service-name> is your service name (array key in a yaml configuration).
If something is wrong in this step, check that the YAML file with defined service is loaded in Bundle Extension, like https://github.com/oroinc/platform/blob/master/src/Oro/Bundle/TagBundle/DependencyInjection/OroTagExtension.php#L22-L26 and validate the syntax.
If it doesn’t work, check the documentation https://symfony.com/doc/3.4/service_container.html -
AuthorReplies
The forum ‘OroCommerce’ is closed to new topics and replies.