Skip to content

Commit

Permalink
Order.coupon_savings and Order.promotion_applied may have multipl…
Browse files Browse the repository at this point in the history
…e values, sum if so.
  • Loading branch information
alexdlaird committed Feb 24, 2025
1 parent be55e95 commit 0535029
Show file tree
Hide file tree
Showing 5 changed files with 6,358 additions and 7 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/alexdlaird/amazon-orders/compare/3.2.11...HEAD)
## [Unreleased](https://github.com/alexdlaird/amazon-orders/compare/3.2.12...HEAD)

## [3.2.12](https://github.com/alexdlaird/amazon-orders/compare/3.2.11...3.2.12) - 2025-02-24

### Fixed

- `Order.coupon_savings` and `Order.promotion_applied` may have multiple values, sum if so.

## [3.2.11](https://github.com/alexdlaird/amazon-orders/compare/3.2.10...3.2.11) - 2025-02-23

Expand Down
2 changes: 1 addition & 1 deletion amazonorders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__copyright__ = "Copyright (c) 2024-2025 Alex Laird"
__license__ = "MIT"
__version__ = "3.2.11"
__version__ = "3.2.12"
18 changes: 13 additions & 5 deletions amazonorders/entity/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ def __init__(self,
#: The Order shipping total. Only populated when ``full_details`` is ``True``.
self.shipping_total: Optional[float] = self._if_full_details(self._parse_currency("shipping"))
#: The Order promotion applied. Only populated when ``full_details`` is ``True``.
self.promotion_applied: Optional[float] = self._if_full_details(self._parse_currency("promotion"))
self.promotion_applied: Optional[float] = self._if_full_details(
self._parse_currency("promotion", combine_multiple=True))
#: The Order coupon savings. Only populated when ``full_details`` is ``True``.
self.coupon_savings: Optional[float] = self._if_full_details(self._parse_currency("coupon"))
self.coupon_savings: Optional[float] = self._if_full_details(
self._parse_currency("coupon", combine_multiple=True))
#: The Order Subscribe & Save discount. Only populated when ``full_details`` is ``True``.
self.subscription_discount: Optional[float] = self._if_full_details(self._parse_currency("subscribe"))
#: The Order total before tax. Only populated when ``full_details`` is ``True``.
Expand Down Expand Up @@ -182,7 +184,7 @@ def _parse_recipient(self) -> Optional[Recipient]:

return Recipient(value, self.config)

def _parse_currency(self, contains) -> Optional[float]:
def _parse_currency(self, contains, combine_multiple=False) -> Optional[float]:
value = None

for tag in util.select(self.parsed, self.config.selectors.FIELD_ORDER_SUBTOTALS_TAG_ITERATOR_SELECTOR):
Expand All @@ -191,8 +193,14 @@ def _parse_currency(self, contains) -> Optional[float]:
self.config.selectors.FIELD_ORDER_SUBTOTALS_TAG_POPOVER_PRELOAD_SELECTOR)):
inner_tag = util.select_one(tag, self.config.selectors.FIELD_ORDER_SUBTOTALS_INNER_TAG_SELECTOR)
if inner_tag:
value = self.to_currency(inner_tag.text)
break
currency = self.to_currency(inner_tag.text)
if currency is not None:
if value is None:
value = 0.0
value += currency

if not combine_multiple:
break

return value

Expand Down
13 changes: 13 additions & 0 deletions tests/entity/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,16 @@ def test_order_coupon_savings(self):

# THEN
self.assertEqual(order.coupon_savings, -3.89)

def test_order_coupon_savings_multiple(self):
# GIVEN
with open(os.path.join(self.RESOURCES_DIR, "orders", "order-details-coupon-savings-multiple.html"),
"r",
encoding="utf-8") as f:
parsed = BeautifulSoup(f.read(), self.test_config.bs4_parser)

# WHEN
order = Order(parsed, self.test_config, full_details=True)

# THEN
self.assertEqual(order.coupon_savings, -1.29)
Loading

0 comments on commit 0535029

Please # to comment.