Interchange6/Routes/Cart.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 118 CC 14 SCAR 49.9
CC
14
Cov
0%
cart_route
210.0
BEGIN
2.0
BEGIN
2.0
CRAP 147.6
1 package Dancer::Plugin::Interchange6::Routes::Cart;
2  
3 use Data::Dumper::Concise;
4 use Dancer ':syntax';
5 use Dancer::Plugin;
6 use Dancer::Plugin::Interchange6;
7 use Dancer::Plugin::Auth::Extensible;
8 use Try::Tiny;
9  
10 =head1 NAME
11  
12 Dancer::Plugin::Interchange6::Routes::Cart - Cart routes for Interchange6 Shop Machine
13  
14 =cut
15  
16 register_hook 'before_cart_display';
17  
18 =head1 FUNCTIONS
19  
20 =head2 cart_route
21  
22 Returns the cart route based on the passed routes configuration.
23  
24 =cut
25  
26 sub cart_route {
27 my $routes_config = shift;
28  
29 return sub {
30 my %values;
31 my ($input, $product, $cart, $cart_name, $cart_input,
32 $cart_product, $roles, @errors);
33  
34 $cart_name = param('cart');
35 $cart = $cart_name ? cart($cart_name) : cart;
36  
37 debug "cart_route cart name: " . $cart->name;
38  
39 if ( param('remove') ) {
40  
41 # removing item from cart
42  
43 try {
44 $cart->remove( param('remove') );
45 # if GET then URL now contains ugly query params so redirect
46 return redirect '/cart' if request->is_get;
47 }
48 catch {
49 warning "Cart remove error: $_";
50 push @errors, "Failed to remove product from cart: $_";
51 };
52 }
53 elsif ( param('update') && defined param('quantity') ) {
54  
55 # update existing cart product
56  
57 debug "Update "
58 . param('update')
59 . " with quantity "
60 . param('quantity');
61  
62 try {
63 $cart->update( param('update') => param('quantity') );
64 }
65 catch {
66 warning "Update cart product error: $_";
67 push @errors, "Failed to update product in cart: $_";
68 };
69 }
70  
71 if ( $input = param('sku') ) {
72  
73 # add new product
74  
75 # we currently only support one product at a time so check that
76 # the param is a scalar
77  
78 if ( ref($input) eq '' ) {
79  
80 $product = shop_product($input);
81  
82 unless ( defined $product ) {
83 warning "sku not found in POST /cart: $input";
84 session shop_cart_error =>
85 { message => "Product not found with sku: $input" };
86 return redirect '/';
87 }
88  
89 # store params in hash
90 my %params = params;
91  
92 if ( defined $product->canonical_sku ) {
93  
94 # this is a variant so we need to add in variant info
95 # into %params if missing
96  
97 my $rset = $product->product_attributes->search(
98 {
99 'attribute.type' => 'variant',
100 },
101 {
102 prefetch => [
103 'attribute',
104 {
105 product_attribute_values =>
106 'attribute_value'
107 }
108 ],
109 }
110 );
111 while ( my $result = $rset->next ) {
112 my $name = $result->attribute->name;
113 # WTF! why do we get a resultset of pavs? Surely there
114 # should be only one related pav for pa?
115 my $value = $result->product_attribute_values->first
116 ->attribute_value->value;
117 $params{$name} = $value unless defined $params{$name};
118  
119 }
120 }
121 # retrieve product attributes for possible variants
122 my $attr_ref = $product->attribute_iterator( hashref => 1 );
123 my %user_input;
124  
125 if ( keys %$attr_ref ) {
126  
127 for my $name ( keys %$attr_ref ) {
128 $user_input{$name} = $params{$name};
129 }
130  
131 debug "Attributes for $input: ", $attr_ref,
132 ", user input: ", \%user_input;
133 my %match_info;
134  
135 unless ( $cart_product =
136 $product->find_variant( \%user_input, \%match_info ) )
137 {
138 warning "Variant not found for ", $product->sku;
139 session shop_cart_error => {
140 message => 'Variant not found.',
141 info => \%match_info
142 };
143 return redirect $product->uri;
144 }
145 }
146 else {
147 # product without variants
148 $cart_product = $product;
149 }
150  
151 my $quantity = 1;
152 if ( param('quantity') ) {
153 $quantity = param('quantity');
154 }
155  
156 try {
157 $cart->add(
158 {
159 dbic_product => $cart_product,
160 sku => $cart_product->sku,
161 quantity => $quantity
162 }
163 );
164 }
165 catch {
166 warning "Cart add error: $_";
167 push @errors, "Failed to add product to cart: $_";
168 };
169 }
170 }
171  
172 # add stuff useful for cart display
173 $values{cart_subtotal} = $cart->subtotal;
174 $values{cart_total} = $cart->total;
175 $values{cart} = $cart->products;
176 $values{cart_error} = join(". ", @errors) if scalar @errors;
177  
178 # call before_cart_display route so template tokens
179 # can be injected
180 execute_hook('before_cart_display', \%values);
181  
182 template $routes_config->{cart}->{template}, \%values;
183 }
184 }
185  
186 1;