当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP WHMCS CartItemsTax用法及代码示例


在为购物车和结帐计算税金时调用,这可用于操纵应用于购物车总数或相关结帐付款意图的税率。

参数

变量 类型 注意
clientData array null
cartData \ItemInterface[] 购物车中保存的所有数据的数组。

响应

返回操作的ItemInterface 项的数组。税收将从原始项目的增量计算。

示例代码

<?php

use WHMCS\View\Formatter\Price;

add_hook('CartItemsTax', '1', function ($vars) {
    $cartItems = $vars['cartData'];
    $client = $vars['clientData'];

    // Calculate your tax rate to apply
    $taxRate = 1.5; // 50%

    /** @var \WHMCS\Cart\Item\ItemInterface $item */
    foreach ($cartItems as $item) {
        if (!$item->isTaxed()) {
            continue;
        }

        /** @var Price $amountToday */
        $amountToday = $item->getAmount();

        // Set the price due today for the item
        $item->setAmount(new Price(
            ($amountToday->toNumeric() * $taxRate),
            $amountToday->getCurrency()
        ));

        if ($item->isRecurring()) {
            /** @var Price $recurringAmount */
            $recurringAmount = $item->getRecurringAmount();
            // Set the recurring price of the item
            $item->setRecurringAmount(
                new Price(
                    ($recurringAmount->toNumeric() * $taxRate),
                    $recurringAmount->getCurrency()
                )
            );
        }
    }

    return [
        'cartData' => $cartItems
    ];
});

相关用法


注:本文由纯净天空筛选整理自whmcs.com大神的英文原创作品 CartItemsTax。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。