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?
This commit is contained in:
parent
e17c092efe
commit
d44654e98a
2 changed files with 8 additions and 10 deletions
|
@ -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} };
|
||||
|
|
|
@ -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),
|
||||
|
|
Loading…
Add table
Reference in a new issue