diff --git a/lib/WebGUI/Asset/Sku/Product.pm b/lib/WebGUI/Asset/Sku/Product.pm index 0e6227c32..7154ad406 100644 --- a/lib/WebGUI/Asset/Sku/Product.pm +++ b/lib/WebGUI/Asset/Sku/Product.pm @@ -272,6 +272,38 @@ sub getAllCollateral { } +#------------------------------------------------------------------- + +=head2 getAllProducts ( $session ) + +A class method to return an iterator for getting all Products (and Product sub-classes) +as Asset objects, one at a time. The iterator will return undef when you've reached +the end of the list of products. + +It should be used like this: + +my $productIterator = WebGUI::Asset::Product->getAllProducts($session); +while (my $product = $productIterator->()) { + ##Do something useful with $product +} + +=cut + +sub getAllProducts { + my $class = shift; + my $session = shift; + my $sth = $session->db->read('select distinct(assetId) from Product'); + return sub { + my ($assetId) = $sth->array; + if (!$assetId) { + $sth->finish; + return undef; + } + return WebGUI::Asset->newPending($session, $assetId); + } +} + + #------------------------------------------------------------------- =head2 getCollateral ( tableName, keyName, keyValue ) diff --git a/lib/WebGUI/Shop/Products.pm b/lib/WebGUI/Shop/Products.pm index 2acae5de0..a59050c23 100644 --- a/lib/WebGUI/Shop/Products.pm +++ b/lib/WebGUI/Shop/Products.pm @@ -39,7 +39,8 @@ file will be named siteProductData.csv. sub exportProducts { my $session = shift; - my $productData = ''; + my @columns = qw{sku title shortdescription price weight quantity}; + my $productData = WebGUI::Text::joinCSV('mastersku', @columns) . "\n"; my $storage = WebGUI::Storage->createTemp($session); $storage->addFileFromScalar('siteProductData.csv', $productData); return $storage; diff --git a/t/Asset/Sku/Product.t b/t/Asset/Sku/Product.t new file mode 100644 index 000000000..c79604916 --- /dev/null +++ b/t/Asset/Sku/Product.t @@ -0,0 +1,69 @@ +# vim:syntax=perl +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2008 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------ +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------ + +# Write a little about what this script tests. +# +# This tests WebGUI::Asset::Sku::Product + +use FindBin; +use strict; +use lib "$FindBin::Bin/../../lib"; + +use Test::More; +use Test::Deep; + +use WebGUI::Test; # Must use this before any other WebGUI modules +use WebGUI::Session; +use WebGUI::Asset; +use WebGUI::Asset::Sku::Product; + +#---------------------------------------------------------------------------- +# Init +my $session = WebGUI::Test->session; + + +#---------------------------------------------------------------------------- +# Tests + +plan tests => 5; # Increment this number for each test you create + +#---------------------------------------------------------------------------- +# put your tests here +my $node = WebGUI::Asset::Sku::Product->getProductImportNode($session); +isa_ok($node, 'WebGUI::Asset::Wobject::Folder', 'getProductImportNode returns a Folder'); +is($node->getId, 'PBproductimportnode001', 'Product Import Node has the correct GUID'); + +my $product1 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'}); +my $product2 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'}); +my $product3 = $node->addChild({ className => 'WebGUI::Asset::Sku::Product'}); + +my $getAProduct = WebGUI::Asset::Sku::Product->getAllProducts($session); +isa_ok($getAProduct, 'CODE', 'getAllProducts returns a sub ref'); +my $counter = 0; +my $productIds = []; +while( my $product = $getAProduct->()) { + ++$counter; + push @{ $productIds }, $product->getId; +} +is($counter, 3, 'getAllProducts: returned only 3 Products'); +cmp_bag($productIds, [$product1->getId, $product2->getId, $product3->getId], 'getAllProduct returned the correct 3 products'); + +$product1->purge; +$product2->purge; +$product3->purge; + +#---------------------------------------------------------------------------- +# Cleanup +END { + +} + +1;