[Handel] subclassing Handel::Order in Catalyst
Christopher H. Laco
claco at chrislaco.com
Tue Oct 10 23:29:54 CEST 2006
Ryan Lauterbach wrote:
> 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
>
Glad it works. One of my goals is to put together a start script that
will auto-gen the step you describe:
$> module-starter --class=Module::Starter::Handel --module=Mycompany
which generates:
MyCompany::Cart
MyCompany::Cart::Item
MyCompany::Storage::Cart
MyCompany::Storage::Cart::Item
MyCompany::Checkout
with all of the subclassing foo already done. Seems like that will help
people get a better start if they intend to customize their usage of
Handel...
-=Chris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: OpenPGP digital signature
Url : http://lists.rawmode.org/pipermail/handel/attachments/20061010/e5b4d948/attachment.pgp
More information about the Handel
mailing list