1

我想对 Django Oscar 上的所有产品征收 18% 的税。实现这一目标的最佳和简单方法是什么?

我已遵循文档。

结帐/tax.py

from decimal import Decimal as D


def apply_to(submission):
    # Assume 7% sales tax on sales to New Jersey  You could instead use an
    # external service like Avalara to look up the appropriates taxes.
    STATE_TAX_RATES = {
        'NJ': D('0.07')
    }
    shipping_address = submission['shipping_address']
    rate = D('0.18')
    for line in submission['basket'].all_lines():
        line_tax = calculate_tax(
            line.line_price_excl_tax_incl_discounts, rate)
        unit_tax = (line_tax / line.quantity).quantize(D('0.01'))
        line.purchase_info.price.tax = unit_tax

    # Note, we change the submission in place - we don't need to
    # return anything from this function
    shipping_charge = submission['shipping_charge']
    if shipping_charge is not None:
        shipping_charge.tax = calculate_tax(
            shipping_charge.excl_tax, rate)


def calculate_tax(price, rate):
    tax = price * rate
    print(tax)
    return tax.quantize(D('0.01'))

结帐/session.py

from . import tax

    def build_submission(self, **kwargs):
        """
        Return a dict of data that contains everything required for an order
        submission.  This includes payment details (if any).

        This can be the right place to perform tax lookups and apply them to
        the basket.
        """
        # Pop the basket if there is one, because we pass it as a positional
        # argument to methods below
        basket = kwargs.pop('basket', self.request.basket)
        shipping_address = self.get_shipping_address(basket)
        shipping_method = self.get_shipping_method(
            basket, shipping_address)
        billing_address = self.get_billing_address(shipping_address)
        if not shipping_method:
            total = shipping_charge = None
        else:
            shipping_charge = shipping_method.calculate(basket)
            total = self.get_order_totals(
                basket, shipping_charge=shipping_charge, **kwargs)
        submission = {
            'user': self.request.user,
            'basket': basket,
            'shipping_address': shipping_address,
            'shipping_method': shipping_method,
            'shipping_charge': shipping_charge,
            'billing_address': billing_address,
            'order_total': total,
            'order_kwargs': {},
            'payment_kwargs': {}}

        # If there is a billing address, add it to the payment kwargs as calls
        # to payment gateways generally require the billing address. Note, that
        # it normally makes sense to pass the form instance that captures the
        # billing address information. That way, if payment fails, you can
        # render bound forms in the template to make re-submission easier.
        if billing_address:
            submission['payment_kwargs']['billing_address'] = billing_address

        # Allow overrides to be passed in
        submission.update(kwargs)

        # Set guest email after overrides as we need to update the order_kwargs
        # entry.
        user = submission['user']
        if (not user.is_authenticated
                and 'guest_email' not in submission['order_kwargs']):
            email = self.checkout_session.get_guest_email()
            submission['order_kwargs']['guest_email'] = email

        tax.apply_to(submission)
        return submission

现在,订单总额应该包含 18% 的税,但我只看到这适用于购物车总额而不是订单总额。即使在产品详细视图中,税也应显示为 18%。

4

1 回答 1

4

您错过了文档中提到的步骤 - 在应用税后重新计算订单总额:

tax.apply_to(submission)

# Recalculate order total to ensure we have a tax-inclusive total
submission['order_total'] = self.get_order_totals(
    submission['basket'],
    submission['shipping_charge']
)

return submission

即使在产品详细视图中,税也应显示为 18%。

这不会自动发生 - 上述逻辑仅在下订单时应用。如果您使用该DeferredTax策略,则在将产品添加到购物篮时不知道税费,因此无法显示。您需要向详细视图添加一些显示逻辑,以指示可能的税款是多少。

于 2019-05-04T02:41:32.563 回答