[Handel] subclassing Handel::Order in Catalyst
Ryan Lauterbach
ryan at radianit.com
Fri Oct 20 16:35:50 CEST 2006
Christopher H. Laco wrote:
> Ryan Lauterbach wrote:
>
>> Yaaaa, its working!
>>
Ok maybe that was a bit premature, but its close now, really!
When i go to checkout, the orders.total and orders.tax fields aren't
updated. The order_items table has the price and total fields updated
ok. Now i can understand the tax being empty as I haven't set this
anywhere. What is the preferred method for doing this. I read
something about default_values->() somewhere but is that what I should
be using? I was thinking I needed to load a plugin (like I did with
AssignOrderNumber) to update orders.total but I figured that wouldn't be
the case. At what point in the pipeline (CHECKOUT_PHASE_FINALIZE) would
these values be set? Maybe there is an error with my subclassing below?
Thanks for any hints!
>> 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
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Handel mailing list
> Handel at lists.rawmode.org
> http://lists.rawmode.org/cgi-bin/mailman/listinfo/handel
>
More information about the Handel
mailing list