Hello i am working on a Shopware 5 project, where i am making an ajax request from my confirm.tpl page
<form id="taxer">
<input type="hidden" id="csrf-token" value="{$sCsrfToken}">
<div class="accept-zero-percent-checkbox">
{if $smarty.session.Shopware.bZeroTax|intval eq 1 }
<input type="checkbox" id="accept-zero-percent" name="accept-zero-percent" checked="checked">
{else}
<input type="checkbox" id="accept-zero-percent" name="accept-zero-percent" >
{/if}
</div>
</form>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
{/block}
in my bootstrap
$this->subscribeEvent(
'Enlight_Controller_Dispatcher_ControllerPath_Frontend_Tax',
'onGetTax'
);
public function onGetTax(Enlight_Event_EventArgs $args)
{
return __DIR__ . '/Controllers/Frontend/TaxController.php';
}
In my Controller i declare my index action method to receive my form request and then try to process based on if the user has checked the box or not. if user is checked i call a method and vice versa.
public function indexAction()
{
$basketService = $this->get('shopware.modules.sBasket');
$cart = $basketService->sGetBasket();
if ($isChecked) {
$this->addTaxToCart($cart);
} else {
$this->removeTaxFromCart($cart);
}
$response = ['status' => 'success', 'data' => $requestData];
$this->View()->loadTemplate('frontend/tax/index.tpl');
$this->View()->assign('data', $response);
}
/**
* Add 19% tax to the cart
*
* @param \Shopware\Models\Order\Basket $cart
*/
private function addTaxToCart($cart)
{
foreach ($cart['content'] as &$item) {
$price = $item['priceNumeric'];
$taxRate = 0.19;
$taxAmount = $price * $taxRate;
$item['priceNumeric'] += $taxAmount;
$item['price'] = number_format($item['priceNumeric'], 2, '.', '');
}
$this->get('shopware.modules.sBasket')->sRefreshBasket();
}
/**
*
* @param \Shopware\Models\Order\Basket $cart
*/
private function removeTaxFromCart($cart)
{
foreach ($cart['content'] as &$item) {
$price = $item['priceNumeric'];
$taxRate = 0.19;
$taxAmount = $price * $taxRate;
$item['priceNumeric'] -= $taxAmount;
$item['price'] = number_format($item['priceNumeric'], 2, '.', '');
}
$this->get('shopware.modules.sBasket')->sRefreshBasket();
}
The ajax request is made but then i get this error
[2024-01-24T10:35:55.641798+01:00] core.CRITICAL: You have requested a non-existent service "core.basket".