Magento购物车:申请折扣时如何计算税金?

时间:2015-08-13 16:33:43

标签: magento quote discount

我想给每个第x位访客提供固定折扣(例如50欧元)。在会议中,我会跟踪是否必须给予折扣。只有购买的商品才能享受折扣 - 包括他们的税(不包括运费......)

我的税务计算设置为:“应用客户税”:折扣后  并“适用价格折扣”:包括税收。

我抓住sales-quote-collect-totals-after事件并计算总订单价值:

$grandTotal = 0;
/* @var $item Mage_Sales_Model_Resource_Quote_Item */
$items = $quote->getAllItems();
foreach ($items as $item) {
    $grandTotal += $item->getRowTotal();
}
$discount = 50; // simplified for clarity, assume $grandTotal > 50
Mage::getSingleton('checkout/session')->setDiscount($discount); // for displaying info
$baseDiscount = $discount * $xRate; // simplified: $xRate has been calculated previously...
foreach ($items as $item) {
    // We apply discount amount based on the ratio between the overall total and the RowTotal
    $rowTotal = $item->getRowTotal();
    $fraction = $rowTotal / $discountable;

    $itemDiscount = $discount * $fraction;
    $itemBaseDiscount = $baseDiscount * $fraction;

    $item->setDiscountAmount($item->getDiscountAmount() + $itemDiscount);
    $item->setBaseDiscountAmount($item->getBaseDiscountAmount() + $itemBaseDiscount);
    $item->save();
}
// Do the items now contain all info for proper calculations in collectTotals?

$addresses = $quote->getAllAddresses();
/* @var $address Mage_Sales_Model_Quote_Address */
foreach ($addresses as $address) {
    // set the totals to 0
    // is this needed? must other stuff be set to 0?
    $address->setSubtotal(0);
    $address->setBaseSubtotal(0);
    $address->setGrandTotal(0);
    $address->setBaseGrandTotal(0);

    // let magento do it's work...
    $address->collectTotals();

    if ($address->getAddressType() == 'shipping') {
        $address->setSubtotalWithDiscount($address->getSubtotalWithDiscount() - $discount);
        $address->setGrandTotal($address->getGrandTotal() - $discount);

        $address->setBaseSubtotalWithDiscount($address->getBaseSubtotalWithDiscount() - $baseDiscount);
        $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseDiscount);

        if ($address->getDiscountDescription()) {
            $address->setDiscountDescription($address->getDiscountDescription() . ', 'Happy Randomizer');
            $address->setDiscountAmount($address->getDiscountAmount() - $discount);
            $address->setBaseDiscountAmount(($address->getBaseDiscountAmount - $baseDiscount));
        } else {
            $address->setDiscountAmount(-($discount));
            $address->setDiscountDescription('Happy Randomizer');
            $address->setBaseDiscountAmount(-$baseDiscount);
        }
        $address->setAddressId($address->getAddressId());
        $address->save();
    }

    // update quote totals
    $quote->setSubtotal($quote->getSubtotal() + $address->getSubtotal());
    $quote->setSubtotalWithDiscount($quote->getSubtotal() - $discount);
    $quote->setGrandTotal($quote->getGrandTotal() + $address->getGrandTotal());

    $quote->setBaseSubtotal($quote->getBaseSubtotal() + $address->getBaseSubtotal());
    $quote->setBaseSubtotalWithDiscount($quote->getBaseSubtotal() - $baseDiscount);
    $quote->setBaseGrandTotal($quote->getBaseGrandTotal() + $address->getBaseGrandTotal());

    $quote->save();
}

在此之后,在购物车中,总计正确显示,显示折扣,但税(VAT)错误:税不是根据商品的折扣价计算的,而是根据原始价值计算的。

问题:

  • 方法有效吗?
  • 如果没有,会有什么替代方案?
  • 如果是这样,为了正确计算/显示税,我缺少什么?

THX。

0 个答案:

没有答案