| 1 | 1 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | |||||||||||
| 3 | package Dancer::Plugin::Interchange6::Cart; | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Dancer::Plugin::Interchange6::Cart | ||||||||||
| 8 | |||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||
| 10 | |||||||||||
| 11 | Extends L<Interchange6::Cart> to tie cart to L<Interchange6::Schema::Result::Cart>. | ||||||||||
| 12 | |||||||||||
| 13 | =cut | ||||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | use Dancer qw(:syntax !before !after); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use Dancer::Plugin; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | use Dancer::Plugin::Auth::Extensible; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 1 | ▶ | use Dancer::Plugin::DBIC; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 1 | ▶ | use Scalar::Util 'blessed'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 1 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 1 | ▶ | use MooseX::CoverableModifiers; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 1 | ▶ | use Interchange6::Types -types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | extends 'Interchange6::Cart'; | ||||||||||
| 30 | |||||||||||
| 31 | 1 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | =head1 ATTRIBUTES | ||||||||||
| 34 | |||||||||||
| 35 | See L<Interchange6::Cart/ATTRIBUTES> for a full list of attributes | ||||||||||
| 36 | inherited by this module. | ||||||||||
| 37 | |||||||||||
| 38 | =head2 database | ||||||||||
| 39 | |||||||||||
| 40 | The database name as defined in the L<Dancer::Plugin::DBIC> configuration. | ||||||||||
| 41 | |||||||||||
| 42 | Defaults to 'default'. | ||||||||||
| 43 | |||||||||||
| 44 | =cut | ||||||||||
| 45 | |||||||||||
| 46 | has database => ( | ||||||||||
| 47 | is => 'ro', | ||||||||||
| 48 | isa => Str, | ||||||||||
| 49 | default => 'default', | ||||||||||
| 50 | ); | ||||||||||
| 51 | |||||||||||
| 52 | =head2 dbic_cart | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | has dbic_cart => ( | ||||||||||
| 57 | is => 'lazy', | ||||||||||
| 58 | isa => InstanceOf['Interchange6::Schema::Result::Cart'], | ||||||||||
| 59 | ); | ||||||||||
| 60 | |||||||||||
| 61 | sub _build_dbic_cart { | ||||||||||
| 62 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_dbic_cart: not called CC=2 SCAR=17.9
| |||||||||||
| 63 | |||||||||||
| 64 | 0 | my $cart = schema( $self->database )->resultset('Cart')->find_or_new( | |||||||||
| 65 | { | ||||||||||
| 66 | name => $self->name, | ||||||||||
| 67 | sessions_id => $self->sessions_id, | ||||||||||
| 68 | }, | ||||||||||
| 69 | { key => 'carts_name_sessions_id' } | ||||||||||
| 70 | ); | ||||||||||
| 71 | |||||||||||
| 72 | 0 | ▶ | if ( $cart->in_storage ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | debug( "Existing cart: ", $cart->carts_id, " ", $cart->name, "." ); | |||||||||
| 74 | } | ||||||||||
| 75 | else { | ||||||||||
| 76 | 0 | $cart->insert; | |||||||||
| 77 | 0 | debug( "New cart ", $cart->carts_id, " ", $cart->name, "." ); | |||||||||
| 78 | } | ||||||||||
| 79 | 0 | return $cart; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | =head2 dbic_cart_products | ||||||||||
| 83 | |||||||||||
| 84 | L</dbic_cart> related resultset C<cart_products> with prefetched C<product>. | ||||||||||
| 85 | |||||||||||
| 86 | =cut | ||||||||||
| 87 | |||||||||||
| 88 | has dbic_cart_products => ( | ||||||||||
| 89 | is => 'lazy', | ||||||||||
| 90 | isa => InstanceOf['DBIx::Class::ResultSet'], | ||||||||||
| 91 | ); | ||||||||||
| 92 | |||||||||||
| 93 | sub _build_dbic_cart_products { | ||||||||||
| 94 | 0 | ▶ | return shift->dbic_cart->related_resultset('cart_products')->search( | ||||||||
|
Subroutine
_build_dbic_cart_products: not called CC=1 SCAR=6.9
| |||||||||||
| 95 | undef, | ||||||||||
| 96 | { | ||||||||||
| 97 | prefetch => 'product' | ||||||||||
| 98 | } | ||||||||||
| 99 | ); | ||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | =head2 id | ||||||||||
| 103 | |||||||||||
| 104 | Extends inherited L<Interchange6::Cart/id> attribute. | ||||||||||
| 105 | |||||||||||
| 106 | Defaults to C<id> of L</dbic_cart>. | ||||||||||
| 107 | |||||||||||
| 108 | =cut | ||||||||||
| 109 | |||||||||||
| 110 | has '+id' => ( | ||||||||||
| 111 | is => 'lazy', | ||||||||||
| 112 | ); | ||||||||||
| 113 | |||||||||||
| 114 | sub _build_id { | ||||||||||
| 115 | return shift->dbic_cart->id, | ||||||||||
| 116 | 0 | ▶ | } | ||||||||
|
Subroutine
_build_id: not called CC=1 SCAR=6.9
| |||||||||||
| 117 | |||||||||||
| 118 | =head2 product_class | ||||||||||
| 119 | |||||||||||
| 120 | Inherited. Change default to L<Dancer::Plugin::Interchange6::Cart::Product>. | ||||||||||
| 121 | |||||||||||
| 122 | =cut | ||||||||||
| 123 | |||||||||||
| 124 | has '+product_class' => ( | ||||||||||
| 125 | default => 'Dancer::Plugin::Interchange6::Cart::Product', | ||||||||||
| 126 | ); | ||||||||||
| 127 | |||||||||||
| 128 | =head2 sessions_id | ||||||||||
| 129 | |||||||||||
| 130 | Extends inherited sessions_id attribute. | ||||||||||
| 131 | |||||||||||
| 132 | Defaults to C<< session->id >>. | ||||||||||
| 133 | |||||||||||
| 134 | =cut | ||||||||||
| 135 | |||||||||||
| 136 | has '+sessions_id' => ( | ||||||||||
| 137 | is => 'lazy', | ||||||||||
| 138 | ); | ||||||||||
| 139 | |||||||||||
| 140 | sub _build_sessions_id { | ||||||||||
| 141 | 0 | ▶ | return session->id; | ||||||||
|
Subroutine
_build_sessions_id: not called CC=1 SCAR=6.9
| |||||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | =head1 METHODS | ||||||||||
| 145 | |||||||||||
| 146 | See L<Interchange6::Cart/METHODS> for a full list of methods inherited by | ||||||||||
| 147 | this module. | ||||||||||
| 148 | |||||||||||
| 149 | =head2 BUILD | ||||||||||
| 150 | |||||||||||
| 151 | Load existing cart from the database along with any products it contains. | ||||||||||
| 152 | |||||||||||
| 153 | =cut | ||||||||||
| 154 | |||||||||||
| 155 | sub BUILD { | ||||||||||
| 156 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
BUILD: not called CC=3 SCAR=24.8
| |||||||||||
| 157 | 0 | my ( @products, $roles ); | |||||||||
| 158 | |||||||||||
| 159 | 0 | my $rset = $self->dbic_cart_products->order_by( 'cart_position', | |||||||||
| 160 | 'cart_products_id' ); | ||||||||||
| 161 | |||||||||||
| 162 | 0 | ▶ | if (logged_in_user) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | 0 | $roles = user_roles; | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | 0 | ▶ | while ( my $record = $rset->next ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | |||||||||||
| 168 | 0 | push @products, | |||||||||
| 169 | { | ||||||||||
| 170 | dbic_product => $record->product, | ||||||||||
| 171 | id => $record->cart_products_id, | ||||||||||
| 172 | sku => $record->sku, | ||||||||||
| 173 | canonical_sku => $record->product->canonical_sku, | ||||||||||
| 174 | name => $record->product->name, | ||||||||||
| 175 | quantity => $record->quantity, | ||||||||||
| 176 | price => $record->product->price, | ||||||||||
| 177 | uri => $record->product->uri, | ||||||||||
| 178 | weight => $record->product->weight, | ||||||||||
| 179 | }; | ||||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | # use seed to avoid hooks | ||||||||||
| 183 | 0 | $self->seed( \@products ); | |||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | =head1 METHODS | ||||||||||
| 187 | |||||||||||
| 188 | =head2 add | ||||||||||
| 189 | |||||||||||
| 190 | Add one or more products to the cart. | ||||||||||
| 191 | |||||||||||
| 192 | Possible arguments: | ||||||||||
| 193 | |||||||||||
| 194 | =over | ||||||||||
| 195 | |||||||||||
| 196 | =item * single product sku (scalar value) | ||||||||||
| 197 | |||||||||||
| 198 | =item * hashref with keys 'sku' and 'quantity' (quantity is optional and defaults to 1) | ||||||||||
| 199 | |||||||||||
| 200 | =item * an array reference of either of the above | ||||||||||
| 201 | |||||||||||
| 202 | =back | ||||||||||
| 203 | |||||||||||
| 204 | In list context returns an array of L<Interchange6::Cart::Product>s and in scalar context returns an array reference of the same. | ||||||||||
| 205 | |||||||||||
| 206 | =cut | ||||||||||
| 207 | |||||||||||
| 208 | around 'add' => sub { | ||||||||||
| 209 | 0 | ▶ | my ( $orig, $self, $args ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=10 SCAR=47.0
| |||||||||||
| 210 | 0 | my ( @products, @ret ); | |||||||||
| 211 | |||||||||||
| 212 | # convert to array reference if we don't already have one | ||||||||||
| 213 | 0 | ▶ | $args = [$args] unless ref($args) eq 'ARRAY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 214 | |||||||||||
| 215 | 0 | execute_hook( 'before_cart_add_validate', $self, $args ); | |||||||||
| 216 | |||||||||||
| 217 | # basic validation + add each validated arg to @args | ||||||||||
| 218 | |||||||||||
| 219 | 0 | foreach my $arg (@$args) { | |||||||||
| 220 | |||||||||||
| 221 | # make sure we have hasref | ||||||||||
| 222 | 0 | ▶ | unless ( ref($arg) eq 'HASH' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 223 | 0 | $arg = { sku => $arg }; | |||||||||
| 224 | } | ||||||||||
| 225 | |||||||||||
| 226 | die "Attempt to add product to cart without sku failed." | ||||||||||
| 227 | 0 | ▶ | unless defined $arg->{sku}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 228 | |||||||||||
| 229 | my $result = | ||||||||||
| 230 | 0 | schema( $self->database )->resultset('Product')->find( $arg->{sku} ); | |||||||||
| 231 | |||||||||||
| 232 | 0 | ▶ | die "Product with sku '$arg->{sku}' does not exist." | ||||||||
|
Branch: 1 branch
| |||||||||||
| 233 | unless defined $result; | ||||||||||
| 234 | |||||||||||
| 235 | my $product = { | ||||||||||
| 236 | dbic_product => $result, | ||||||||||
| 237 | name => $result->name, | ||||||||||
| 238 | price => $result->price, | ||||||||||
| 239 | sku => $result->sku, | ||||||||||
| 240 | canonical_sku => $result->canonical_sku, | ||||||||||
| 241 | uri => $result->uri, | ||||||||||
| 242 | weight => $result->weight, | ||||||||||
| 243 | 0 | ▶ | quantity => defined $arg->{quantity} ? $arg->{quantity} : 1, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 244 | }; | ||||||||||
| 245 | |||||||||||
| 246 | 0 | push @products, $product; | |||||||||
| 247 | } | ||||||||||
| 248 | |||||||||||
| 249 | 0 | execute_hook( 'before_cart_add', $self, \@products ); | |||||||||
| 250 | |||||||||||
| 251 | # add products to cart | ||||||||||
| 252 | |||||||||||
| 253 | 0 | foreach my $product ( @products ) { | |||||||||
| 254 | |||||||||||
| 255 | # bubble up the add | ||||||||||
| 256 | 0 | my $ret = $orig->( $self, $product ); | |||||||||
| 257 | |||||||||||
| 258 | # update or create in db | ||||||||||
| 259 | |||||||||||
| 260 | 0 | my $cart_product = | |||||||||
| 261 | $self->dbic_cart_products->search( { 'me.sku' => $ret->sku }, | ||||||||||
| 262 | { rows => 1 } )->single; | ||||||||||
| 263 | |||||||||||
| 264 | 0 | ▶ | if ( $cart_product ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 265 | 0 | $cart_product->update({ quantity => $ret->quantity }); | |||||||||
| 266 | } | ||||||||||
| 267 | else { | ||||||||||
| 268 | 0 | $cart_product = $self->dbic_cart_products->create( | |||||||||
| 269 | { | ||||||||||
| 270 | sku => $ret->sku, | ||||||||||
| 271 | quantity => $ret->quantity, | ||||||||||
| 272 | cart_position => 0, | ||||||||||
| 273 | } | ||||||||||
| 274 | ); | ||||||||||
| 275 | } | ||||||||||
| 276 | |||||||||||
| 277 | 0 | push @ret, $ret; | |||||||||
| 278 | } | ||||||||||
| 279 | |||||||||||
| 280 | 0 | execute_hook( 'after_cart_add', $self, \@ret ); | |||||||||
| 281 | |||||||||||
| 282 | 0 | ▶ | return wantarray ? @ret : \@ret; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 283 | }; | ||||||||||
| 284 | |||||||||||
| 285 | =head2 clear | ||||||||||
| 286 | |||||||||||
| 287 | Removes all products from the cart. | ||||||||||
| 288 | |||||||||||
| 289 | =cut | ||||||||||
| 290 | |||||||||||
| 291 | around clear => sub { | ||||||||||
| 292 | 0 | ▶ | my ( $orig, $self ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 293 | |||||||||||
| 294 | 0 | execute_hook( 'before_cart_clear', $self ); | |||||||||
| 295 | |||||||||||
| 296 | 0 | $orig->( $self, @_ ); | |||||||||
| 297 | |||||||||||
| 298 | # delete all products from this cart | ||||||||||
| 299 | 0 | $self->dbic_cart_products->delete_all; | |||||||||
| 300 | |||||||||||
| 301 | 0 | execute_hook( 'after_cart_clear', $self ); | |||||||||
| 302 | |||||||||||
| 303 | 0 | return; | |||||||||
| 304 | }; | ||||||||||
| 305 | |||||||||||
| 306 | =head2 load_saved_products | ||||||||||
| 307 | |||||||||||
| 308 | Pulls old cart items into current cart - used after user login. | ||||||||||
| 309 | |||||||||||
| 310 | =cut | ||||||||||
| 311 | |||||||||||
| 312 | sub load_saved_products { | ||||||||||
| 313 | 0 | ▶ | my ( $self, %args ) = @_; | ||||||||
|
Subroutine
load_saved_products: not called CC=5 SCAR=34.0
| |||||||||||
| 314 | 0 | my ( $uid, $result, $code ); | |||||||||
| 315 | |||||||||||
| 316 | # should not be called unless user is logged in | ||||||||||
| 317 | 0 | ▶ | return unless $self->users_id; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 318 | |||||||||||
| 319 | # grab the resultset for current cart so we can update products easily if | ||||||||||
| 320 | # we find old saved cart products | ||||||||||
| 321 | |||||||||||
| 322 | 0 | my $current_cart_rs = schema( $self->database )->resultset('Cart')->search( | |||||||||
| 323 | { | ||||||||||
| 324 | 'me.name' => $self->name, | ||||||||||
| 325 | 'me.users_id' => $self->users_id, | ||||||||||
| 326 | 'me.sessions_id' => $self->sessions_id, | ||||||||||
| 327 | } | ||||||||||
| 328 | )->search_related( 'cart_products', {}, ); | ||||||||||
| 329 | |||||||||||
| 330 | # find old carts and see if they have products we should move into | ||||||||||
| 331 | # our new cart + remove the old carts as we go | ||||||||||
| 332 | |||||||||||
| 333 | 0 | $result = schema( $self->database )->resultset('Cart')->search( | |||||||||
| 334 | { | ||||||||||
| 335 | 'me.name' => $self->name, | ||||||||||
| 336 | 'me.users_id' => $self->users_id, | ||||||||||
| 337 | 'me.sessions_id' => [ undef, { '!=', $self->sessions_id } ], | ||||||||||
| 338 | } | ||||||||||
| 339 | ); | ||||||||||
| 340 | |||||||||||
| 341 | 0 | ▶ | while ( my $cart = $result->next ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 342 | |||||||||||
| 343 | 0 | my $related = $cart->search_related( | |||||||||
| 344 | 'cart_products', | ||||||||||
| 345 | {}, | ||||||||||
| 346 | { | ||||||||||
| 347 | join => 'product', | ||||||||||
| 348 | prefetch => 'product', | ||||||||||
| 349 | } | ||||||||||
| 350 | ); | ||||||||||
| 351 | 0 | ▶ | while ( my $record = $related->next ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 352 | |||||||||||
| 353 | # look for this sku in our current cart | ||||||||||
| 354 | |||||||||||
| 355 | 0 | my $product = | |||||||||
| 356 | $self->dbic_cart_products->search( { 'me.sku' => $record->sku }, | ||||||||||
| 357 | { rows => 1 } )->single; | ||||||||||
| 358 | |||||||||||
| 359 | 0 | ▶ | if ( $product ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 360 | |||||||||||
| 361 | # we have this sku in our new cart so update quantity | ||||||||||
| 362 | 0 | $product->update( | |||||||||
| 363 | { | ||||||||||
| 364 | quantity => $product->quantity + $record->quantity | ||||||||||
| 365 | } | ||||||||||
| 366 | ); | ||||||||||
| 367 | } | ||||||||||
| 368 | else { | ||||||||||
| 369 | |||||||||||
| 370 | # move product into new cart | ||||||||||
| 371 | 0 | $record->update( { carts_id => $self->id } ); | |||||||||
| 372 | } | ||||||||||
| 373 | } | ||||||||||
| 374 | |||||||||||
| 375 | # delete the old cart (cascade deletes related cart products) | ||||||||||
| 376 | 0 | $cart->delete; | |||||||||
| 377 | } | ||||||||||
| 378 | |||||||||||
| 379 | } | ||||||||||
| 380 | |||||||||||
| 381 | =head2 remove | ||||||||||
| 382 | |||||||||||
| 383 | Remove single product from the cart. Takes SKU of product to identify | ||||||||||
| 384 | the product. | ||||||||||
| 385 | |||||||||||
| 386 | =cut | ||||||||||
| 387 | |||||||||||
| 388 | around remove => sub { | ||||||||||
| 389 | 0 | ▶ | my ( $orig, $self, $arg ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 390 | |||||||||||
| 391 | 0 | execute_hook( 'before_cart_remove_validate', $self, $arg ); | |||||||||
| 392 | |||||||||||
| 393 | 0 | ▶ | my $index = $self->product_index( sub { $_->sku eq $arg } ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 394 | |||||||||||
| 395 | 0 | ▶ | die "Product sku not found in cart: $arg." unless $index >= 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 396 | |||||||||||
| 397 | 0 | execute_hook( 'before_cart_remove', $self, $arg ); | |||||||||
| 398 | |||||||||||
| 399 | 0 | my $ret = $orig->( $self, $arg ); | |||||||||
| 400 | |||||||||||
| 401 | 0 | $self->dbic_cart_products->search( { 'me.sku' => $ret->sku } )->delete; | |||||||||
| 402 | |||||||||||
| 403 | 0 | execute_hook( 'after_cart_remove', $self, $arg ); | |||||||||
| 404 | |||||||||||
| 405 | 0 | return $ret; | |||||||||
| 406 | }; | ||||||||||
| 407 | |||||||||||
| 408 | =head2 rename | ||||||||||
| 409 | |||||||||||
| 410 | Rename this cart. This is the writer method for L<Interchange6::Cart/name>. | ||||||||||
| 411 | |||||||||||
| 412 | Arguments: new name | ||||||||||
| 413 | |||||||||||
| 414 | Returns: new name | ||||||||||
| 415 | |||||||||||
| 416 | =cut | ||||||||||
| 417 | |||||||||||
| 418 | around rename => sub { | ||||||||||
| 419 | my ( $orig, $self, $new_name ) = @_; | ||||||||||
| 420 | |||||||||||
| 421 | my $old_name = $self->name; | ||||||||||
| 422 | |||||||||||
| 423 | execute_hook( 'before_cart_rename', $self, $old_name, $new_name ); | ||||||||||
| 424 | |||||||||||
| 425 | my $ret = $orig->( $self, $new_name ); | ||||||||||
| 426 | |||||||||||
| 427 | $self->dbic_cart->update( { name => $ret } ); | ||||||||||
| 428 | |||||||||||
| 429 | execute_hook( 'after_cart_rename', $self, $old_name, $ret ); | ||||||||||
| 430 | |||||||||||
| 431 | return $ret; | ||||||||||
| 432 | }; | ||||||||||
| 433 | |||||||||||
| 434 | sub _find_and_update { | ||||||||||
| 435 | 0 | ▶ | my ( $self, $sku, $new_product ) = @_; | ||||||||
|
Subroutine
_find_and_update: not called CC=1 SCAR=6.9
| |||||||||||
| 436 | |||||||||||
| 437 | 0 | $self->dbic_cart_products->search( | |||||||||
| 438 | { | ||||||||||
| 439 | 'me.sku' => $sku | ||||||||||
| 440 | } | ||||||||||
| 441 | )->update($new_product); | ||||||||||
| 442 | } | ||||||||||
| 443 | |||||||||||
| 444 | =head2 set_sessions_id | ||||||||||
| 445 | |||||||||||
| 446 | Writer method for L<Interchange6::Cart/sessions_id>. | ||||||||||
| 447 | |||||||||||
| 448 | =cut | ||||||||||
| 449 | |||||||||||
| 450 | around set_sessions_id => sub { | ||||||||||
| 451 | my ( $orig, $self, $arg ) = @_; | ||||||||||
| 452 | |||||||||||
| 453 | execute_hook( 'before_cart_set_sessions_id', $self, $arg ); | ||||||||||
| 454 | |||||||||||
| 455 | my $ret = $orig->( $self, $arg ); | ||||||||||
| 456 | |||||||||||
| 457 | debug( "Change sessions_id of cart " . $self->id . " to: ", $arg ); | ||||||||||
| 458 | |||||||||||
| 459 | $self->dbic_cart->update({ sessions_id => $arg }); | ||||||||||
| 460 | |||||||||||
| 461 | execute_hook( 'after_cart_set_sessions_id', $ret, $arg ); | ||||||||||
| 462 | |||||||||||
| 463 | return $ret; | ||||||||||
| 464 | }; | ||||||||||
| 465 | |||||||||||
| 466 | =head2 set_users_id | ||||||||||
| 467 | |||||||||||
| 468 | Writer method for L<Interchange6::Cart/users_id>. | ||||||||||
| 469 | |||||||||||
| 470 | =cut | ||||||||||
| 471 | |||||||||||
| 472 | around set_users_id => sub { | ||||||||||
| 473 | my ( $orig, $self, $arg ) = @_; | ||||||||||
| 474 | |||||||||||
| 475 | execute_hook( 'before_cart_set_users_id', $self, $arg ); | ||||||||||
| 476 | |||||||||||
| 477 | debug("Change users_id of cart " . $self->id . " to: $arg"); | ||||||||||
| 478 | |||||||||||
| 479 | my $ret = $orig->( $self, $arg ); | ||||||||||
| 480 | |||||||||||
| 481 | $self->dbic_cart->update( { users_id => $arg } ); | ||||||||||
| 482 | |||||||||||
| 483 | execute_hook( 'after_cart_set_users_id', $ret, $arg ); | ||||||||||
| 484 | |||||||||||
| 485 | return $ret; | ||||||||||
| 486 | }; | ||||||||||
| 487 | |||||||||||
| 488 | =head2 update | ||||||||||
| 489 | |||||||||||
| 490 | Update quantity of products in the cart. | ||||||||||
| 491 | |||||||||||
| 492 | Parameters are pairs of SKUs and quantities, e.g. | ||||||||||
| 493 | |||||||||||
| 494 | $cart->update(9780977920174 => 5, | ||||||||||
| 495 | 9780596004927 => 3); | ||||||||||
| 496 | |||||||||||
| 497 | Triggers before_cart_update and after_cart_update hooks. | ||||||||||
| 498 | |||||||||||
| 499 | A quantity of zero is equivalent to removing this product, | ||||||||||
| 500 | so in this case the remove hooks will be invoked instead | ||||||||||
| 501 | of the update hooks. | ||||||||||
| 502 | |||||||||||
| 503 | Returns updated products that are still in the cart. Products removed | ||||||||||
| 504 | via quantity 0 or products for which quantity has not changed will not | ||||||||||
| 505 | be returned. | ||||||||||
| 506 | |||||||||||
| 507 | =cut | ||||||||||
| 508 | |||||||||||
| 509 | around update => sub { | ||||||||||
| 510 | my ( $orig, $self, @args ) = @_; | ||||||||||
| 511 | my ( @products, $product, $new_product, $count ); | ||||||||||
| 512 | |||||||||||
| 513 | ARGS: while ( @args > 0 ) { | ||||||||||
| 514 | |||||||||||
| 515 | my $sku = shift @args; | ||||||||||
| 516 | my $qty = shift @args; | ||||||||||
| 517 | |||||||||||
| 518 | die "Bad quantity argument to update: $qty" unless $qty =~ /^\d+$/; | ||||||||||
| 519 | |||||||||||
| 520 | if ( $qty == 0 ) { | ||||||||||
| 521 | |||||||||||
| 522 | # do remove instead of update | ||||||||||
| 523 | $self->remove($sku); | ||||||||||
| 524 | next ARGS; | ||||||||||
| 525 | } | ||||||||||
| 526 | |||||||||||
| 527 | execute_hook( 'before_cart_update', $self, $sku, $qty ); | ||||||||||
| 528 | |||||||||||
| 529 | my ($ret) = $orig->( $self, $sku => $qty ); | ||||||||||
| 530 | |||||||||||
| 531 | $self->_find_and_update( $sku, { quantity => $qty } ); | ||||||||||
| 532 | |||||||||||
| 533 | execute_hook( 'after_cart_update', $ret, $sku, $qty ); | ||||||||||
| 534 | } | ||||||||||
| 535 | }; | ||||||||||
| 536 | |||||||||||
| 537 | =head1 HOOKS | ||||||||||
| 538 | |||||||||||
| 539 | The following hooks are available: | ||||||||||
| 540 | |||||||||||
| 541 | =over 4 | ||||||||||
| 542 | |||||||||||
| 543 | =item before_cart_add_validate | ||||||||||
| 544 | |||||||||||
| 545 | Executed in L</add> before arguments are validated as being valid. Hook | ||||||||||
| 546 | receives the following arguments: | ||||||||||
| 547 | |||||||||||
| 548 | Receives: $cart, \%args | ||||||||||
| 549 | |||||||||||
| 550 | The args are those that were passed to L<add>. | ||||||||||
| 551 | |||||||||||
| 552 | Example: | ||||||||||
| 553 | |||||||||||
| 554 | hook before_cart_add_validate => sub { | ||||||||||
| 555 | my ( $cart, $args ) = @_; | ||||||||||
| 556 | foreach my $arg ( @$args ) { | ||||||||||
| 557 | my $sku = ref($arg) eq 'HASH' ? $arg->{sku} : $arg; | ||||||||||
| 558 | die "bad product" if $sku eq "bad sku"; | ||||||||||
| 559 | } | ||||||||||
| 560 | } | ||||||||||
| 561 | |||||||||||
| 562 | =item before_cart_add | ||||||||||
| 563 | |||||||||||
| 564 | Called in L</add> immediately before the products are added to the cart. | ||||||||||
| 565 | |||||||||||
| 566 | Receives: $cart, \@products | ||||||||||
| 567 | |||||||||||
| 568 | The products arrary ref contains simple hash references that will be passed | ||||||||||
| 569 | to L<Interchange6::Cart::Product/new>. | ||||||||||
| 570 | |||||||||||
| 571 | =item after_cart_add | ||||||||||
| 572 | |||||||||||
| 573 | Called in L</add> after products have been added to the cart. | ||||||||||
| 574 | |||||||||||
| 575 | Receives: $cart, \@products | ||||||||||
| 576 | |||||||||||
| 577 | The products arrary ref contains L<Interchange6::Cart::Product>s. | ||||||||||
| 578 | |||||||||||
| 579 | =item before_cart_remove_validate | ||||||||||
| 580 | |||||||||||
| 581 | Called at start of L</remove> before arg has been validated. | ||||||||||
| 582 | |||||||||||
| 583 | Receives: $cart, $sku | ||||||||||
| 584 | |||||||||||
| 585 | =item before_cart_remove | ||||||||||
| 586 | |||||||||||
| 587 | Called in L</remove> before validated product is removed from cart. | ||||||||||
| 588 | |||||||||||
| 589 | Receives: $cart, $sku | ||||||||||
| 590 | |||||||||||
| 591 | =item after_cart_remove | ||||||||||
| 592 | |||||||||||
| 593 | Called in L</remove> after product has been removed from cart. | ||||||||||
| 594 | |||||||||||
| 595 | Receives: $cart, $sku | ||||||||||
| 596 | |||||||||||
| 597 | =item before_cart_update | ||||||||||
| 598 | |||||||||||
| 599 | Executed for each pair of sku/quantity passed to L<update> before the update is performed. | ||||||||||
| 600 | |||||||||||
| 601 | Receives: $cart, $sku, $quantity | ||||||||||
| 602 | |||||||||||
| 603 | A quantity of zero is equivalent to removing this product, | ||||||||||
| 604 | so in this case the remove hooks will be invoked instead | ||||||||||
| 605 | of the update hooks. | ||||||||||
| 606 | |||||||||||
| 607 | =item after_cart_update | ||||||||||
| 608 | |||||||||||
| 609 | Executed for each pair of sku/quantity passed to L<update> after the update is performed. | ||||||||||
| 610 | |||||||||||
| 611 | Receives: $product, $sku, $quantity | ||||||||||
| 612 | |||||||||||
| 613 | Where C<$product> is the L<Interchange6::Cart::Product> returned from | ||||||||||
| 614 | L<Interchange6::Cart::Product/update>. | ||||||||||
| 615 | |||||||||||
| 616 | A quantity of zero is equivalent to removing this product, | ||||||||||
| 617 | so in this case the remove hooks will be invoked instead | ||||||||||
| 618 | of the update hooks. | ||||||||||
| 619 | |||||||||||
| 620 | =item before_cart_clear | ||||||||||
| 621 | |||||||||||
| 622 | Executed in L</clear> before the clear is performed. | ||||||||||
| 623 | |||||||||||
| 624 | Receives: $cart | ||||||||||
| 625 | |||||||||||
| 626 | =item after_cart_clear | ||||||||||
| 627 | |||||||||||
| 628 | Executed in L</clear> after the clear is performed. | ||||||||||
| 629 | |||||||||||
| 630 | Receives: $cart | ||||||||||
| 631 | |||||||||||
| 632 | =item before_cart_set_users_id | ||||||||||
| 633 | |||||||||||
| 634 | Executed in L<set_users_id> before users_id is updated. | ||||||||||
| 635 | |||||||||||
| 636 | Receives: $cart, $userid | ||||||||||
| 637 | |||||||||||
| 638 | =item after_cart_set_users_id | ||||||||||
| 639 | |||||||||||
| 640 | Executed in L<set_users_id> after users_id is updated. | ||||||||||
| 641 | |||||||||||
| 642 | Receives: $new_usersid, $requested_userid | ||||||||||
| 643 | |||||||||||
| 644 | =item before_cart_set_sessions_id | ||||||||||
| 645 | |||||||||||
| 646 | Executed in L<set_sessions_id> before sessions_id is updated. | ||||||||||
| 647 | |||||||||||
| 648 | Receives: $cart, $sessionid | ||||||||||
| 649 | |||||||||||
| 650 | =item after_cart_set_sessions_id | ||||||||||
| 651 | |||||||||||
| 652 | Executed in L<set_sessions_id> after sessions_id is updated. | ||||||||||
| 653 | |||||||||||
| 654 | Receives: $cart, $sessionid | ||||||||||
| 655 | |||||||||||
| 656 | =item before_cart_rename | ||||||||||
| 657 | |||||||||||
| 658 | Executed in L</rename> before cart L<Interchange6::Cart/name> is updated. | ||||||||||
| 659 | |||||||||||
| 660 | Receives: $cart, $old_name, $new_name | ||||||||||
| 661 | |||||||||||
| 662 | =item after_cart_rename | ||||||||||
| 663 | |||||||||||
| 664 | Executed in L</rename> after cart L<Interchange6::Cart/name> is updated. | ||||||||||
| 665 | |||||||||||
| 666 | Receives: $cart, $old_name, $new_name | ||||||||||
| 667 | |||||||||||
| 668 | =back | ||||||||||
| 669 | |||||||||||
| 670 | =head1 AUTHORS | ||||||||||
| 671 | |||||||||||
| 672 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||||||||||
| 673 | Peter Mottram (SysPete), <peter@sysnix.com> | ||||||||||
| 674 | |||||||||||
| 675 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 676 | |||||||||||
| 677 | Copyright 2011-2016 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||||||||||
| 678 | |||||||||||
| 679 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 680 | under the terms of either: the GNU General Public License as published | ||||||||||
| 681 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 682 | |||||||||||
| 683 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 684 | |||||||||||
| 685 | =cut | ||||||||||
| 686 | |||||||||||
| 687 | 1; | ||||||||||