Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi currency #3

Merged
merged 2 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions nxtbn/core/currency_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.utils.deprecation import MiddlewareMixin
from django.conf import settings

class CurrencyMiddleware(MiddlewareMixin):
def process_request(self, request):
# Get the currency from the X-Currency header
currency = request.headers.get('Accept-Currency', settings.BASE_CURRENCY) # BASE_CURRENCY is fallback

# Store the currency in the request object
request.currency = currency
10 changes: 10 additions & 0 deletions nxtbn/product/api/storefront/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from django.db import transaction

from nxtbn.core.models import CurrencyExchange
from nxtbn.product.api.dashboard.serializers import RecursiveCategorySerializer
from nxtbn.filemanager.api.dashboard.serializers import ImageSerializer
from nxtbn.product.models import Product, Collection, Category, ProductVariant
Expand All @@ -21,6 +23,14 @@ class Meta:
model = ProductVariant
fields = '__all__'

def get_price_in_customer_currency(self, obj): # TODO: Implement logic for taxclass in future
if not settings.IS_MULTI_CURRENCY: # If site is in single currency, no currenyc required
return obj.price
else:
currency_code = self.context.get('request').currency
rate = CurrencyExchange.objects.get(base_currency=settings.BASE_CURRENCY, target_currency=currency_code).exchange_rate
return rate

class ProductSerializer(serializers.ModelSerializer):
default_variant = ProductVariantSerializer()
class Meta:
Expand Down
3 changes: 2 additions & 1 deletion nxtbn/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def get_env_var(key, default=None, var_type=str):

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware', # TODO: Do we need this?
'nxtbn.core.currency_middleware.CurrencyMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
"corsheaders.middleware.CorsMiddleware",
'django.middleware.common.CommonMiddleware',
Expand Down