Variant Price Surcharge: Extending
Extend Variant Price Surcharge: the decorated price calculators, service IDs and repository, the Sylius resource, the variant form Twig hook, setup command, routes and how it interacts with other price decorators.
Variant Price Surcharge: Extending
The plugin defines no service tags of its own and no configuration options. The extension points are the Sylius mechanisms it is wired through.
Price calculator
The class Mmd\SyliusVariantPriceSurchargePlugin\Calculator\SurchargeAwareProductVariantPriceCalculator decorates the service sylius.calculator.product_variant_price (interface ProductVariantPricesCalculatorInterface).
| Method | Behaviour |
|---|---|
calculate() |
Price with surcharge |
calculateOriginal() |
Original price with surcharge |
calculateLowestPriceBeforeDiscount() |
Passed through unchanged from the decorated calculator |
From Sylius 2.3 there is also the service sylius.calculator.product_variant_catalog_price (interface CatalogPricesCalculatorInterface). The class SurchargeAwareCatalogPricesCalculator decorates it, a subclass of the calculator above without any logic of its own. The compiler pass DecorateCatalogPricesCalculatorPass, which the bundle registers, takes care of that. If the service does not exist, for example under Sylius 2.2, the pass does nothing.
If another plugin or your project decorates the same service, the decorator priority determines the order. The plugin sets no priority of its own for its decorators, so the default applies. It calls whichever calculator sits below it for both the variant and the base variant.
The calculator reads the channel for the overrides from the context ($context['channel']). If it is missing, the global values apply.
Repository and resource
| Purpose | Identifier |
|---|---|
| Resource | mmd_sylius_variant_price_surcharge.product_variant_price_surcharge |
| Repository service | mmd_sylius_variant_price_surcharge.repository.product_variant_price_surcharge, also available as an alias through the interface ProductVariantPriceSurchargeRepositoryInterface |
| Entity | Mmd\SyliusVariantPriceSurchargePlugin\Entity\ProductVariantPriceSurcharge with the interface ProductVariantPriceSurchargeInterface |
| Per-channel override | Mmd\SyliusVariantPriceSurchargePlugin\Entity\Channel\ProductVariantPriceSurchargeChannel, a plain Doctrine entity without a resource, grid or repository of its own |
| Settings | VariantPriceSurchargeSettingsRepository with the method getSettings(), which returns an object with the switch on when no row is stored |
The repository interface offers two methods:
findOneByProductVariant()returns the surcharge of a variant including its per-channel overrides.findBaseVariantForProduct()returns the base variant of a product.
On the interface ProductVariantPriceSurchargeInterface you reach the overrides with getChannelOverrides() (a collection keyed by channel code), getChannelOverride(string $channelCode), addChannelOverride() and removeChannelOverride(). An override exposes isEnabled(), getPercentage() and getFixedSurcharge(), each null when the global value should apply.
Admin interface
| Item | Identifier |
|---|---|
| Twig hook of the form section | sylius_admin.product_variant.update.content.form.sections, entry mmd_sylius_variant_price_surcharge |
| Template of the form section | @MmdSyliusVariantPriceSurchargePlugin/admin/product_variant/form/sections/price_surcharge.html.twig |
| Form extension | ProductVariantTypeExtension on the form type ProductVariantType |
| Form types | ProductVariantPriceSurchargeType (global fields, collection of channels) and ProductVariantPriceSurchargeChannelType (one channel) |
| Overview grid | mmd_sylius_variant_price_surcharge_admin_product_variant_price_surcharge |
| Routes | mmd_variant_price_surcharge_admin_settings for the settings, the resource routes of the overview without create, update and show |
| Menu event | sylius.menu.admin.main |
Console command
mmd:variant-price-surcharge:setup (class Mmd\SyliusVariantPriceSurchargePlugin\Command\SetupCommand) is registered as the service mmd_sylius_variant_price_surcharge.command.setup with the tag console.command. What it does is described on Installation and Operation.
Limits
- The form extension saves the surcharge through form events, not through a property on
ProductVariant. You can therefore maintain surcharges only through the variant form and the delete action of the overview. The plugin ships neither an admin API nor an import for them. - A configuration tree
mmd_sylius_variant_price_surchargeexists but contains no options.