New plugin: plus
This commit is contained in:
parent
29b3eea131
commit
a0eafa21f5
1 changed files with 45 additions and 0 deletions
45
plugins/plus
Normal file
45
plugins/plus
Normal file
|
@ -0,0 +1,45 @@
|
|||
#!perl
|
||||
|
||||
HELP "+<N>" => "Add N more items of the previous thing";
|
||||
|
||||
my $limit = 200;
|
||||
my $err_limit = "Repetition is limited at $limit items.";
|
||||
my $err_pfand = "Plugins 'pfand' and 'repeat' cannot be combined.";
|
||||
|
||||
sub command {
|
||||
my ($self, $cart, $command) = @_;
|
||||
|
||||
return ABORT, $err_pfand if $cart->entries('is_pfand');
|
||||
|
||||
my ($post) = $command =~ /^\+(\d+)?$/
|
||||
or return NEXT;
|
||||
|
||||
return ABORT, "Can't modify an empty transaction." if not $cart->size;
|
||||
|
||||
my $last = ($cart->entries)[-1];
|
||||
|
||||
return REJECT, "Addition only works on products." if not $last->has_attribute('product_id');
|
||||
|
||||
if ($post) {
|
||||
return REJECT, $err_limit if $last->quantity + $post > $limit;
|
||||
|
||||
$last->quantity($last->quantity + $post);
|
||||
return ACCEPT;
|
||||
}
|
||||
|
||||
return "Add to previous product", \&add;
|
||||
}
|
||||
|
||||
sub add {
|
||||
my ($self, $cart, $arg) = @_;
|
||||
|
||||
$arg =~ /^\d+$/ and $arg > 0
|
||||
or return REJECT, "Invalid value.";
|
||||
|
||||
my $last = ($cart->entries)[-1];
|
||||
return REJECT, $err_limit if $last->quantity + $arg > $limit;
|
||||
|
||||
$last->quantity($last->quantity + $arg);
|
||||
return ACCEPT;
|
||||
}
|
||||
|
Loading…
Add table
Reference in a new issue