Dancer2-Plugin-Interchange6

 view release on metacpan or  search on metacpan

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


    $self->plugin->execute_plugin_hook( 'before_cart_clear', $self );

    $orig->( $self, @_ );

    # delete all products from this cart
    $self->dbic_cart_products->delete_all;

    $self->plugin->execute_plugin_hook( 'after_cart_clear', $self );

    return;
};

=head2 load_saved_products

Pulls old cart items into current cart - used after user login.

=cut

sub load_saved_products {
    my $self = shift;

    # 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 = $self->schema->resultset('Cart')->search(
        {
            'me.name'        => $self->name,
            'me.users_id'    => $self->users_id,
            'me.sessions_id' => [ undef, { '!=', $self->sessions_id } ],
        },
        {
            prefetch => 'cart_products',
        }
    );

    my $json = JSON::MaybeXS->new;

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

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

            $self->add(
                {
                    sku      => $cart_product->sku,
                    quantity => $cart_product->quantity,
                    combine  => $cart_product->combine,
                    $cart_product->extra
                    ? ( extra => $json->decode($cart_product->extra) )
                    : (),
                }
            );

        }
    }

    # 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 ) = @_;

    $self->plugin->execute_plugin_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;

    $self->plugin->execute_plugin_hook( 'before_cart_remove', $self, $arg );

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

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

    $self->plugin->execute_plugin_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;

    $self->plugin->execute_plugin_hook( 'before_cart_rename',
        $self, $old_name, $new_name );

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

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

    $self->plugin->execute_plugin_hook( 'after_cart_rename',
        $self, $old_name, $ret );

    return $ret;
};

sub _find_and_update {



( run in 1.077 second using v1.01-cache-2.11-cpan-9169edd2b0e )