File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Discounts/Actions/FreeShipping.php
<?php declare(strict_types=1);
namespace App\Discounts\Actions;
use App\Cart\ShoppingCartInterface;
use App\Checkout\CheckoutService;
use App\Discounts\DiscountableInterface;
use App\ShippingCosts\ShippingCostsService;
use App\Vat\Models\FinancialProperties;
use App\Vat\VatScenarioEnum;
use App\Vat\VatService;
class FreeShipping extends AbstractDiscountActionHandler
{
public function apply(DiscountableInterface $discountable, VatService $vatService, string $description, string $params): bool {
//Initialize some services which cannot be initialized in the construction phase. Because session isn't available in there.
$checkoutService = new CheckoutService();
/** @var ShoppingCartInterface $shoppingCart */
$shoppingCart = app(ShoppingCartInterface::class);
/** @var ShippingCostsService $shippingCostsService */
$shippingCostsService = app(ShippingCostsService::class);
$shippingAddress = $checkoutService->getShippingAddress();
$shippingCosts = $shippingCostsService->get($shoppingCart, $shippingAddress);
$vatService->calculateVatForModelWithVatScenarioEnum($shippingCosts);
$priceModification = new FinancialProperties();
$priceModification->setVatScenarioEnum(VatScenarioEnum::high_inc);
$priceModification->setPrice($shippingCosts->getPriceInc() * -1);
$financialProperties = $vatService->calculateVatForModelWithVatScenarioEnum($priceModification);
$shoppingCart->addDiscountPriceMutation($description, $financialProperties);
return true;
}
}