[Handel] subclassing Handel::Order in Catalyst
Ryan Lauterbach
ryan at radianit.com
Tue Oct 10 23:09:23 CEST 2006
Yaaaa, its working!
Christopher H. Laco wrote:
> Ryan Lauterbach wrote:
>
>> Christopher H. Laco wrote:
>>
>>> Ryan Lauterbach wrote:
>>>
>>>
>>>> Hi,
>>>>
>>>> I have successfully subclassed Handel::Cart and Handel::Cart::Items
>>>> in .99_14 to add extra fields in cart_items. Now i need to copy the
>>>> cart_items fields over to order_items upon checkout. I believe I
>>>> need to override copy_cart_items per the Handel::Order POD so here
>>>> are my classes which don't work. Thanks for looking:
>>>>
>>>>
<bigsnip>
I'll summarize the steps I took to get custom cart_items and order_items
columns working in a catalyst/handel app (if only for my benefit):
* I used the "Better" approach from
http://search.cpan.org/~claco/Handel/lib/Handel/Manual/Cookbook/AddingColumns.pod
to create custom cart and cartitem classes:
package RMA::M::HandelCart;
use strict;
use warnings;
use base qw/Handel::Cart/;
__PACKAGE__->item_class('RMA::M::HandelItem');
1;
package RMA::M::HandelItem;
use strict;
use warnings;
use base qw/Handel::Cart::Item/;
__PACKAGE__->storage->add_columns(qw/logo logo_data logo_type photo_type
photo_data photo c
__PACKAGE__->create_accessors;
1;
* I then defined a custom Catalyst::Model::Handel::Order class,
specifying custom order_class
package RMA::M::Order;
use strict;
use warnings;
BEGIN {
use base qw/Catalyst::Model::Handel::Order/;
};
__PACKAGE__->config(
connection_info => ['dbi:mysql:db', 'user', 'pass'],
order_class => 'RMA::M::HandelOrder',
);
* And in that custom Handel::Order class, point to your custom
Handel::Cart class and define the custom order item class, and override
copy_cart_items:
package RMA::M::HandelOrder;
use strict;
use warnings;
use base qw/Handel::Order/;
__PACKAGE__->cart_class('RMA::M::HandelCart');
__PACKAGE__->item_class('RMA::M::HandelOrderItem');
sub copy_cart_items {
my ($self, $order, $cart) = @_;
foreach my $item ($cart->items) {
$order->add($item);
}
return;
}
* Create the custom Handel::Order::Item class, with same columns as your
custom Handel::Cart::Item class
package RMA::M::HandelOrderItem;
use strict;
use warnings;
use base qw/Handel::Order::Item/;
__PACKAGE__->storage->add_columns(qw/logo logo_data logo_type photo_type
photo_data photo c
__PACKAGE__->create_accessors;
1;
* Now we need the Checkout controller to use our custom Handel::Order
class, so create a custom Handel::Checkout class:
package RMA::M::CustomCheckout;
use strict;
use warnings;
use base qw/Handel::Checkout/;
__PACKAGE__->order_class('RMA::M::HandelOrder');
1;
* and then in your Checkout controller instantiate your CustomCheckout
class instead of the default Handel::Checkout:
i.e. replace Handel::Checkout->new with RMA::M::CustomCheckout->new
-----------------------------------------------------------
Ok, hope I haven't left anything out, or added too many superfluous
steps. Thanks for the help Chris, and the framework!
-Ryan
More information about the Handel
mailing list