Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Optimze avalara plugin

fowczreke4f50d420136
diff --git a/saleor/checkout/fetch.py b/saleor/checkout/fetch.py
index 696f40ab54..45847cd0d7 100644
--- a/saleor/checkout/fetch.py
+++ b/saleor/checkout/fetch.py
@@ -11,6 +11,7 @@ if TYPE_CHECKING:
from ..product.models import (
Collection,
Product,
+ ProductType,
ProductVariant,
ProductVariantChannelListing,
)
@@ -24,6 +25,7 @@ class CheckoutLineInfo:
variant: "ProductVariant"
channel_listing: "ProductVariantChannelListing"
product: "Product"
+ product_type: "ProductType"
collections: List["Collection"]
@@ -60,6 +62,7 @@ def fetch_checkout_lines(checkout: "Checkout") -> Iterable[CheckoutLineInfo]:
for line in lines:
variant = line.variant
product = variant.product
+ product_type = product.product_type
collections = list(product.collections.all())
variant_channel_listing = None
@@ -78,6 +81,7 @@ def fetch_checkout_lines(checkout: "Checkout") -> Iterable[CheckoutLineInfo]:
variant=variant,
channel_listing=variant_channel_listing,
product=product,
+ product_type=product_type,
collections=collections,
)
)
diff --git a/saleor/graphql/checkout/dataloaders.py b/saleor/graphql/checkout/dataloaders.py
index ed85ed5d86..c2b50d7563 100644
--- a/saleor/graphql/checkout/dataloaders.py
+++ b/saleor/graphql/checkout/dataloaders.py
@@ -10,6 +10,7 @@ from ..core.dataloaders import DataLoader
from ..product.dataloaders import (
CollectionsByVariantIdLoader,
ProductByVariantIdLoader,
+ ProductTypeByVariantIdLoader,
ProductVariantByIdLoader,
VariantChannelListingByVariantIdAndChannelIdLoader,
)
@@ -42,9 +43,16 @@ class CheckoutLinesInfoByCheckoutTokenLoader(DataLoader):
channel_pks = [checkout.channel_id for checkout in checkouts]
def with_variants_products_collections(results):
- variants, products, collections, channel_listings = results
+ (
+ variants,
+ products,
+ product_types,
+ collections,
+ channel_listings,
+ ) = results
variants_map = dict(zip(variants_pks, variants))
products_map = dict(zip(variants_pks, products))
+ product_types_map = dict(zip(variants_pks, product_types))
collections_map = dict(zip(variants_pks, collections))
channel_listings_map = dict(
zip(variant_ids_channel_ids, channel_listings)
@@ -61,6 +69,7 @@ class CheckoutLinesInfoByCheckoutTokenLoader(DataLoader):
(line.variant_id, checkout.channel_id)
],
product=products_map[line.variant_id],
+ product_type=product_types_map[line.variant_id],
collections=collections_map[line.variant_id],
)
for line in lines
@@ -70,6 +79,9 @@ class CheckoutLinesInfoByCheckoutTokenLoader(DataLoader):
variants = ProductVariantByIdLoader(self.context).load_many(variants_pks)
products = ProductByVariantIdLoader(self.context).load_many(variants_pks)
+ product_types = ProductTypeByVariantIdLoader(self.context).load_many(
+ variants_pks
+ )
collections = CollectionsByVariantIdLoader(self.context).load_many(
variants_pks
)
@@ -84,7 +96,7 @@ class CheckoutLinesInfoByCheckoutTokenLoader(DataLoader):
self.context
).load_many(variant_ids_channel_ids)
return Promise.all(
- [variants, products, collections, channel_listings]
+ [variants, products, product_types, collections, channel_listings]
).then(with_variants_products_collections)
checkouts = CheckoutByTokenLoader(self.context).load_many(keys)
diff --git a/saleor/plugins/avatax/__init__.py b/saleor/plugins/avatax/__init__.py
index 03d87cf670..c31c30a677 100644
--- a/saleor/plugins/avatax/__init__.py
+++ b/saleor/plugins/avatax/__init__.py
@@ -17,6 +17,7 @@ from ...checkout import base_calculations
from ...checkout.utils import is_shipping_required
from ...core.taxes import TaxError
from ...order.utils import get_total_order_discount
+from ...shipping.models import ShippingMethodChannelListing
if TYPE_CHECKING:
from ...checkout.fetch import CheckoutInfo, CheckoutLineInfo
@@ -211,14 +212,15 @@ def append_line_to_data(
)
-def append_shipping_to_data(data: List[Dict], shipping_method, channel_id):
+def append_shipping_to_data(
+ data: List[Dict],
+ shipping_method_channel_listings: Optional["ShippingMethodChannelListing"],
+):
charge_taxes_on_shipping = (
Site.objects.get_current().settings.charge_taxes_on_shipping
)
- if charge_taxes_on_shipping and shipping_method:
- shipping_price = shipping_method.channel_listings.get(
- channel_id=channel_id
- ).price
+ if charge_taxes_on_shipping and shipping_method_channel_listings:
+ shipping_price = shipping_method_channel_listings.price
append_line_to_data(
data,
quantity=1,
@@ -238,7 +240,7 @@ def get_checkout_lines_data(
for line_info in lines_info:
product = line_info.product
name = product.name
- product_type = product.product_type
+ product_type = line_info.product_type
tax_code = retrieve_tax_code_from_meta(product, default=None)
tax_code = tax_code or retrieve_tax_code_from_meta(product_type)
append_line_to_data(
@@ -255,7 +257,8 @@ def get_checkout_lines_data(
)
append_shipping_to_data(
- data, checkout_info.shipping_method, checkout_info.channel.id
+ data,
+ checkout_info.shipping_method_channel_listings,
)
return data
@@ -307,7 +310,10 @@ def get_order_lines_data(
name="Order discount",
tax_included=True, # Voucher should be always applied as a gross amount
)
- append_shipping_to_data(data, order.shipping_method, order.channel_id)
+ shipping_method_channel_listing = ShippingMethodChannelListing.objects.filter(
+ shipping_method=order.shipping_method_id, channel=order.channel_id
+ ).first()
+ append_shipping_to_data(data, shipping_method_channel_listing)
return data
diff --git a/saleor/plugins/avatax/plugin.py b/saleor/plugins/avatax/plugin.py
index 049ebe549d..7c5b5374bd 100644
--- a/saleor/plugins/avatax/plugin.py
+++ b/saleor/plugins/avatax/plugin.py
@@ -134,7 +134,7 @@ class AvataxPlugin(BasePlugin):
discounts: Iterable[DiscountInfo],
):
for line_info in lines:
- if line_info.variant.product.charge_taxes:
+ if line_info.product.charge_taxes:
continue
line_price = base_calculations.base_checkout_line_total(
line_info,