From d44654e98a1f4a5fdca41b99dc773cbcbcd19cd4 Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Mon, 16 Jan 2023 04:53:50 +0100 Subject: [PATCH] statiegeld_tokens: reuse code Since that logic was factored into its own sub and is basically the same as what was used here, let's use it here too. This is still somewhat duplicated functionality from products::command. Should adding addons be done in _read_products instead? --- plugins/statiegeld | 5 +++-- plugins/statiegeld_tokens | 13 +++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/plugins/statiegeld b/plugins/statiegeld index bb4c7e2..03ba88a 100644 --- a/plugins/statiegeld +++ b/plugins/statiegeld @@ -23,8 +23,9 @@ my $nope = "Sorry, no deposit on that product.\n"; our $S = ($ENV{REVBANK_STATIEGELD} // 0) == 1; -sub statiegeld_product($product_id) { - my $products = RevBank::Plugin::products::_read_products(); +sub statiegeld_product($product_id, $products = undef) { + $products ||= RevBank::Plugin::products::_read_products(); + my $product = $products->{$product_id} or return; my @addons = @{ $product->{addons} }; diff --git a/plugins/statiegeld_tokens b/plugins/statiegeld_tokens index 90be2ec..4232d91 100644 --- a/plugins/statiegeld_tokens +++ b/plugins/statiegeld_tokens @@ -104,15 +104,12 @@ sub hook_checkout_prepare($class, $cart, $username, $transaction_id, @) { my $tokens_changed = 0; # Products bought: add tokens - for my $entry ($cart->entries('addons')) { - for my $addon_id (@{ $entry->attribute('addons') }) { - my $addon = $products->{"+$addon_id"} // $products->{$addon_id}; - - next - if $addon->{percent} - or (List::Util::none { $addon->{contra} eq $_ } _addon_accounts) - or $addon->{price} <= 0; + for my $entry ($cart->entries('product_id')) { + my $id = $entry->attribute('product_id'); + my $sg = RevBank::Plugin::statiegeld::statiegeld_product($id, $products) + or next; + for my $addon (@{ $sg->{statiegeld_addons} }) { for (1 .. $entry->quantity) { my $token = join(":", join(".", time(), time() + $ttl, $transaction_id),