mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2009-12-15 02:16 am

[dw-free] Paid time gift promotion

[commit: http://hg.dwscoalition.org/dw-free/rev/6d2c703db6a0]

http://bugs.dwscoalition.org/show_bug.cgi?id=2203

Forgot the new file.

Patch by [staff profile] mark.

Files modified:
  • cgi-bin/DW/Hooks/HolidayPromotion.pm
--------------------------------------------------------------------------------
diff -r 60b893aac8c6 -r 6d2c703db6a0 cgi-bin/DW/Hooks/HolidayPromotion.pm
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi-bin/DW/Hooks/HolidayPromotion.pm	Tue Dec 15 02:16:33 2009 +0000
@@ -0,0 +1,88 @@
+#!/usr/bin/perl
+#
+# DW::Hooks::HolidayPromotion
+#
+# This file explains Dreamwidth's plans for world domination. Be sure to keep it updated!
+#
+# Authors:
+#      Mark Smith <mark@dreamwidth.org>
+#
+# This program is NOT free software or open-source; you can use it as an
+# example of how to implement your own site-specific extensions to the
+# Dreamwidth Studios open-source code, but you cannot use it on your site
+# or redistribute it, with or without modifications.
+#
+
+package DW::Hooks::HolidayPromotion;
+
+use strict;
+
+# promotion HTML
+LJ::register_hook( 'shop_cart_status_bar', sub {
+    my ( $shop, $cart, $retref ) = @_;
+
+    # anonymous sessions can't benefit from the promotion
+    return if $shop->anonymous;
+
+    # bail out if it's expired (2010-01-01 00:00:00)
+    return if time > 1262304000;
+
+    # put the note up top so people know
+    $$retref = "<div class='shop-error'><strong>" . BML::ml( 'shop.holidaypromoblurb' ) .
+               "</strong></div>\n" . $$retref;
+} );
+
+# hook to add a new item when they purchase somethign eligibile
+LJ::register_hook( 'shop_cart_added_item', sub {
+    my ( $cart, $item ) = @_;
+
+    # ignore promo linked items so we don't loop forever
+    return if $item->{_holiday_promo_2009};
+
+    # validation checks
+    return unless $cart->userid;
+    return if $item->t_userid && $item->t_userid == $cart->userid;
+    return if $item->permanent || $item->months < 6;
+
+    # determine what kind of time to give the user.  rules are simple, if
+    # the user has premium, give them premium.  else, they get paid.
+    my $type = DW::Pay::get_account_type( $cart->userid );
+    $type = 'paid' if $type ne 'premium';
+
+    # looks good, build a new object and stick it on the cart
+    my $new = bless {
+        cost   => 0.00,
+        months => int( $item->months / 6 ) * 2,
+        class  => $type,
+        target_userid => $cart->userid,
+        cannot_conflict => 1,
+        noremove => 1,
+        from_name => $LJ::SITENAME,
+
+        _holiday_promo_2009  => $item->id,
+    }, 'DW::Shop::Item::Account';
+
+    my ( $rv, $msg ) = $cart->add_item( $new );
+    warn "Failed to add holiday promotion time: $msg\n"
+        unless $rv;
+} );
+
+# when they remove an item ...
+LJ::register_hook( 'shop_cart_removed_item', sub {
+    my ( $cart, $item ) = @_;
+
+    # don't do anything if we're removing a promo item
+    return if $item->{_holiday_promo_2009};
+
+    # iterate over the cart to see if any items link to this one
+    foreach my $it ( @{$cart->items} ) {
+        if ( $it->{_holiday_promo_2009} == $item->id ) {
+            # they're linked, remove it forcefully (mental image: large hammer)
+            $cart->remove_item( $it->id, force => 1 );
+        }
+    }
+
+} );
+
+
+1;
--------------------------------------------------------------------------------