Skip to content

Commit

Permalink
add is_available_to_buy + make work for parent products
Browse files Browse the repository at this point in the history
  • Loading branch information
viggo-devries committed Apr 19, 2024
1 parent 5fa8576 commit 8608eb5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
12 changes: 7 additions & 5 deletions oscar_odin/mappings/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,21 @@ def children(self) -> Tuple[Optional[List[resources.catalogue.Product]]]:
)
return (None,)

@odin.assign_field(to_field=("price", "currency", "availability"))
@odin.assign_field(to_field=("price", "currency", "availability", "is_available_to_buy"))
def map_stock_price(self) -> Tuple[Decimal, str, int]:
"""Resolve stock price using strategy and decompose into price/currency/availability."""
stock_strategy: DefaultStrategy = self.context["stock_strategy"]

# Switch here based on if this is a parent or child product
price, availability, _ = stock_strategy.fetch_for_product(self.source)
if self.source.is_parent:
price, availability, _ = stock_strategy.fetch_for_parent(self.source)
else:
price, availability, _ = stock_strategy.fetch_for_product(self.source)

if availability.is_available_to_buy:
return price.excl_tax, price.currency, availability.num_available
return price.excl_tax, price.currency, getattr(availability, "num_available", 0), True
else:
# There is no stock record for this product.
return Decimal(0), "", 0
return Decimal(0), "", 0, False


class ProductToModel(ModelMapping):
Expand Down
1 change: 1 addition & 0 deletions oscar_odin/resources/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class Product(OscarCatalogue):
price: Decimal = DecimalField(null=True)
currency: Optional[str]
availability: Optional[int]
is_available_to_buy: bool
partner: Optional[Any]

product_class: Optional[ProductClass] = None
Expand Down

0 comments on commit 8608eb5

Please sign in to comment.