From 3b7dd524db09cee234d8b4dd400879d1859da026 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Tue, 2 Aug 2011 09:59:10 -0700 Subject: [PATCH] When a Sku is purged, delete it from all carts. Better handling for cartItems whose assets have been deleted. Fixes bug #12213 --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Asset/Sku.pm | 16 ++++++++++++++++ lib/WebGUI/Shop/Cart.pm | 7 +++++-- lib/WebGUI/Shop/CartItem.pm | 3 ++- t/Shop/Cart.t | 9 ++++++++- 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 2ef654eec..ac10bd8c7 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -2,6 +2,7 @@ - rfe #12207: Thingy. Field_name info returned by www_editThingDataSaveViaAjax - fixed #12206: Bad Subscription Groups in Duplicated Threads - fixed #12208: replacements don't work + - fixed #12213: Unable to view cart when an asset is deleted. 7.10.21 - added #9668 extension template variable to attachment loops for the following assets: diff --git a/lib/WebGUI/Asset/Sku.pm b/lib/WebGUI/Asset/Sku.pm index 7b0cc9995..125c5dc57 100644 --- a/lib/WebGUI/Asset/Sku.pm +++ b/lib/WebGUI/Asset/Sku.pm @@ -628,6 +628,22 @@ sub processStyle { #------------------------------------------------------------------- +=head2 purge ( ) + +Extent the base class to clean out any items using this Sku in all Carts. + +=cut + +sub purge { + my $self = shift; + my $assetId = $self->getId; + my $success = $self->SUPER::purge; + return $success unless $success; + $self->session->db->write('delete from cartItem where assetId=?',[$assetId]); +} + +#------------------------------------------------------------------- + =head2 setTaxConfiguration ($namespace, $configuration) =head3 $namespace diff --git a/lib/WebGUI/Shop/Cart.pm b/lib/WebGUI/Shop/Cart.pm index b31f12c0e..a8a31e230 100644 --- a/lib/WebGUI/Shop/Cart.pm +++ b/lib/WebGUI/Shop/Cart.pm @@ -676,8 +676,11 @@ sub requiresShipping { my $self = shift; # Look for recurring items in the cart - foreach my $item (@{ $self->getItems }) { - return 1 if $item->getSku->isShippingRequired; + ITEM: foreach my $item (@{ $self->getItems }) { + my $sku = $item->getSku; + next ITEM unless $sku; + return 1 if $sku->isShippingRequired; + } # No recurring items in cart so return false diff --git a/lib/WebGUI/Shop/CartItem.pm b/lib/WebGUI/Shop/CartItem.pm index 60af3d25a..08a350361 100644 --- a/lib/WebGUI/Shop/CartItem.pm +++ b/lib/WebGUI/Shop/CartItem.pm @@ -175,7 +175,8 @@ sub getSku { my ($self) = @_; my $asset = ''; $asset = WebGUI::Asset->newByDynamicClass($self->cart->session, $self->get("assetId")); - $asset->applyOptions($self->get("options")) if $asset; + return undef if ! $asset; + $asset->applyOptions($self->get("options")); return $asset; } diff --git a/t/Shop/Cart.t b/t/Shop/Cart.t index 32bfaef29..0e61cfea3 100644 --- a/t/Shop/Cart.t +++ b/t/Shop/Cart.t @@ -18,6 +18,7 @@ use strict; use lib "$FindBin::Bin/../lib"; use Test::More; use Test::Deep; +use Test::Exception; use Scalar::Util qw/refaddr/; use WebGUI::Test; # Must use this before any other WebGUI modules use WebGUI::Session; @@ -35,7 +36,7 @@ my $i18n = WebGUI::International->new($session, "Shop"); #---------------------------------------------------------------------------- # Tests -plan tests => 36; # Increment this number for each test you create +plan tests => 38; # Increment this number for each test you create #---------------------------------------------------------------------------- # put your tests here @@ -225,3 +226,9 @@ is($cart->delete, undef, "Can destroy cart."); $product->purge; +my $requiresShipping_ok = lives_ok { $cart->requiresShipping; } 'requiresShipping does not die if the asset in the cart has been deleted'; + +SKIP: { + skip 1, 'requiresShipping died, so skipping' unless $requiresShipping_ok; + ok !$cart->requiresShipping, 'Shipping no longer required on a cart with missing assets'; +}