Revision 816

Date:
2021/02/17 20:47:36
Author:
ahitrov
Revision Log:
Discount issues

Files:

Legend:

 
Added
 
Removed
 
Modified
  • utf8/plugins/webshop/lib/webshop/Discount.pm

     
    93 93 my $self = shift;
    94 94
    95 95 my (%opts) = @_;
    96 return 0 unless exists $opts{basket} || exists $opts{uid} && $opts{uid} || exists $opts{session} && $opts{session};
    97 return 0 unless $self->discount;
    96 unless ( exists $opts{basket} || exists $opts{uid} && $opts{uid} || exists $opts{session} && $opts{session} ) {
    97 return wantarray ? (0, []) : 0;
    98 }
    99 unless ( $self->discount ) {
    100 return wantarray ? (0, []) : 0;
    101 }
    98 102
    99 103 my $basket = delete $opts{basket};
    100 104 $basket = $keeper->{webshop}->get_basket ( %opts ) unless $basket;
    101 return 0 unless ref $basket eq 'ARRAY' && @$basket;
    105 unless ( ref $basket eq 'ARRAY' && @$basket ) {
    106 return wantarray ? (0, []) : 0;
    107 }
    102 108
    103 109 my @basket = grep { exists $_->{item} && $_->{item} } @$basket;
    104 return 0 unless @basket;
    110 unless ( @basket ) {
    111 return wantarray ? (0, []) : 0;
    112 }
    105 113
    106 114 my ($number, $sum_total) = (0, 0);
    107 115 map { $number += $_->number; $sum_total += $_->number * $_->{item}->price } @basket;
     
    110 118 my %item_props = map { $_->{attr} => $_ } $basket[0]->{item}->structure;
    111 119 if ( exists $item_props{special_price} ) {
    112 120 @basket = grep { !$_->{item}->special_price } @basket;
    113 return 0 unless @basket;
    121 unless (@basket) {
    122 return wantarray ? (0, []) : 0;
    123 }
    114 124 }
    115 125
    126 # check if any of the item are from trigger section(s)
    127 my %ts = map { $_ => 1 } $self->trigger_sections;
    128 if ( %ts ) {
    129 my $triggered;
    130 foreach my $bi ( @basket ) {
    131 my @s = ref $bi->{item} ? $bi->{item}->sections : ();
    132 next unless @s;
    133 foreach my $s ( @s ) {
    134 if ( exists $ts{$s} ) {
    135 $triggered = 1;
    136 last;
    137 }
    138 }
    139 last if $triggered;
    140 }
    141 unless ( $triggered ) {
    142 return wantarray ? (0, []) : 0;
    143 }
    144 }
    145
    116 146 my $discount_counted = 0;
    117 147 my $items;
    118 148 if ( $self->groups ) {
     
    122 152 ids => 1,
    123 153 return_mode => 'array_ref',
    124 154 );
    125 return 0 unless ref $items eq 'ARRAY' && @$items;
    155 unless ( ref $items eq 'ARRAY' && @$items ) {
    156 return wantarray ? (0, []) : 0;
    157 }
    126 158 } else {
    127 159 $items = $self->keeper->get_documents (
    128 160 class => $state->{webshop}->{item_document_class},
     
    136 168 if ( ref $items eq 'ARRAY' && @$items ) {
    137 169 my %items = map { $_ => 1 } @$items;
    138 170 @basket = grep { exists $items{$_->item_id} } @basket;
    139 return 0 unless @basket;
    171 unless ( @basket ) {
    172 return wantarray ? (0, []) : 0;
    173 }
    140 174 }
    141 175 if ( $self->mechanic eq 'two_as_one' ) {
    142 176 $discount_counted = $self->mechanic_two_as_one( \@basket );