Dancer-Plugin-Interchange6

 view release on metacpan or  search on metacpan

lib/Dancer/Plugin/Interchange6/Cart.pm  view on Meta::CPAN

    # should not be called unless user is logged in
    return unless $self->users_id;

    # find old carts and see if they have products we should move into
    # our new cart

    my $old_carts = schema( $self->database )->resultset('Cart')->search(
        {
            'me.name'        => $self->name,
            'me.users_id'    => $self->users_id,
            'me.sessions_id' => [ undef, { '!=', $self->sessions_id } ],
        },
        {
            prefetch => { cart_products => 'product' },
        }
    );

    while ( my $cart = $old_carts->next ) {

        my $cart_products = $cart->cart_products;
        while ( my $cart_product = $cart_products->next ) {

            # look for this sku in our current cart

            my $product = $self->dbic_cart_products->single(
                { 'me.sku' => $cart_product->sku } );

            if ( $product ) {

                # we have this sku in our new cart so update quantity
                my $quantity = $product->quantity + $cart_product->quantity;

                # update in DB
                $product->update( { quantity => $quantity } );

                # update Interchange6::Cart::Product object
                $self->find( $cart_product->sku )->set_quantity($quantity);
            }
            else {

                # move product into new cart
                $cart_product->update( { carts_id => $self->id } );

                # add to Interchange6::Cart
                push @{ $self->products },
                  use_module( $self->product_class )->new(
                    dbic_product  => $cart_product->product,
                    id            => $cart_product->id,
                    sku           => $cart_product->sku,
                    canonical_sku => $cart_product->product->canonical_sku,
                    name          => $cart_product->product->name,
                    quantity      => $cart_product->quantity,
                    price         => $cart_product->product->price,
                    uri           => $cart_product->product->uri,
                    weight        => $cart_product->product->weight,
                  );
              }
        }
    }

    # delete the old carts (cascade deletes related cart products)
    $old_carts->delete;
}

=head2 remove

Remove single product from the cart. Takes SKU of product to identify
the product.

=cut

around remove => sub {
    my ( $orig, $self, $arg ) = @_;

    execute_hook( 'before_cart_remove_validate', $self, $arg );

    my $index = $self->product_index( sub { $_->sku eq $arg } );

    die "Product sku not found in cart: $arg." unless $index >= 0;

    execute_hook( 'before_cart_remove', $self, $arg );

    my $ret = $orig->( $self, $arg );

    $self->dbic_cart_products->search( { 'me.sku' => $ret->sku } )->delete;

    execute_hook( 'after_cart_remove', $self, $arg );

    return $ret;
};

=head2 rename

Rename this cart. This is the writer method for L<Interchange6::Cart/name>.

Arguments: new name

Returns: new name

=cut

around rename => sub {
    my ( $orig, $self, $new_name ) = @_;

    my $old_name = $self->name;

    execute_hook( 'before_cart_rename', $self, $old_name, $new_name );

    my $ret = $orig->( $self, $new_name );

    $self->dbic_cart->update( { name => $ret } );

    execute_hook( 'after_cart_rename', $self, $old_name, $ret );

    return $ret;
};

sub _find_and_update {
    my ( $self, $sku, $new_product ) = @_;

    $self->dbic_cart_products->search(



( run in 0.619 second using v1.01-cache-2.11-cpan-2398b32b56e )