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