You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware 5更新购物车总价时出现core.basket服务不存在错误

解决Shopware 5中"core.basket"服务不存在的错误

我正在开发一个Shopware 5项目,从confirm.tpl页面发起Ajax请求,相关代码如下:

1. confirm.tpl 代码

<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}

2. Bootstrap中注册控制器路径

$this->subscribeEvent(
    'Enlight_Controller_Dispatcher_ControllerPath_Frontend_Tax',
    'onGetTax'
);
public function onGetTax(Enlight_Event_EventArgs $args)
{
    return __DIR__ . '/Controllers/Frontend/TaxController.php';
}

3. TaxController 代码

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();
}

触发的错误信息

[2024-01-24T10:35:55.641798+01:00] core.CRITICAL: You have requested a non-existent service "core.basket".

问题原因与解决方法

  1. 错误根源
    直接修改sGetBasket()返回的数组无法持久化购物车数据,且sRefreshBasket内部依赖的core.basket服务未正确初始化,这是因为你的操作不符合Shopware 5购物车修改的标准流程。

  2. 正确的购物车修改方式
    使用shopware.modules.sBasket提供的sUpdateArticle方法更新商品价格,而非直接修改数组:

private function addTaxToCart($cart)
{
    $basketService = $this->get('shopware.modules.sBasket');
    foreach ($cart['content'] as $item) {
        $currentPrice = $item['priceNumeric'];
        $taxRate = 0.19;
        // 计算含税价格(假设原价不含税)
        $newPrice = $currentPrice * (1 + $taxRate);
        
        // 更新商品价格
        $basketService->sUpdateArticle(
            $item['id'],
            $item['quantity'],
            $newPrice,
            $item['additional']
        );
    }
    $basketService->sRefreshBasket();
}

private function removeTaxFromCart($cart)
{
    $basketService = $this->get('shopware.modules.sBasket');
    foreach ($cart['content'] as $item) {
        $currentPrice = $item['priceNumeric'];
        $taxRate = 0.19;
        // 计算不含税价格(假设当前价格含税)
        $newPrice = $currentPrice / (1 + $taxRate);
        
        // 更新商品价格
        $basketService->sUpdateArticle(
            $item['id'],
            $item['quantity'],
            $newPrice,
            $item['additional']
        );
    }
    $basketService->sRefreshBasket();
}
  1. 额外修复点
  • 补充获取复选框状态的代码:$isChecked = (bool)$this->Request()->getParam('accept-zero-percent');
  • 初始化未定义的$requestData:$requestData = $this->Request()->getParams();

内容的提问来源于stack exchange,提问作者olawole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 14:22:20