]> git.imager.perl.org - bse.git/commitdiff
allow importing custom fields for product option values
authorTony Cook <tony@develop-help.com>
Mon, 20 Apr 2020 08:43:40 +0000 (18:43 +1000)
committerTony Cook <tony@develop-help.com>
Mon, 20 Apr 2020 08:43:40 +0000 (18:43 +1000)
site/cgi-bin/modules/BSE/Importer.pm
site/cgi-bin/modules/BSE/Importer/Target/Product.pm
t/130-importer/030-product.t

index d7d12f721bc1275d1fa98d52aa2f20ab707635ad..21a1b257b8fb06eefef7803fed58fc4a2db21673 100644 (file)
@@ -2,7 +2,7 @@ package BSE::Importer;
 use strict;
 use Config;
 
-our $VERSION = "1.008";
+our $VERSION = "1.009";
 
 =head1 NAME
 
@@ -147,7 +147,7 @@ sub new {
     or die "No entries found for profile $profile\n";
   
   my %map;
-  for my $map (grep /^map_\w+$/, keys %ids) {
+  for my $map (grep /^map_[\w.-]+$/, keys %ids) {
     (my $out = $map) =~ s/^map_//;
     my $in = $ids{$map};
     $in =~ /^\d+$/
@@ -157,14 +157,14 @@ sub new {
   $self->{map} = \%map;
 
   my %set;
-  for my $set (grep /^set_\w+$/, keys %ids) {
+  for my $set (grep /^set_[\w.-]+$/, keys %ids) {
     (my $out = $set) =~ s/^set_//;
     $set{$out} = $ids{$set};
   }
   $self->{set} = \%set;
 
   my %xform;
-  for my $xform (grep /^xform_\w+$/, keys %ids) {
+  for my $xform (grep /^xform_[\w.-]+$/, keys %ids) {
     (my $out = $xform) =~ s/^xform_//;
     $map{$out}
       or die "Xform for $out but no mapping\n";
index 7aff144104bb8c431257a3a1420115308abe00f0..0b78b1ac1ed8a71f224d8941a7b5bb7260bd6f86 100644 (file)
@@ -8,7 +8,7 @@ use BSE::TB::ProductOptions;
 use BSE::TB::ProductOptionValues;
 use BSE::TB::PriceTiers;
 
-our $VERSION = "1.011";
+our $VERSION = "1.012";
 
 =head1 NAME
 
@@ -93,6 +93,10 @@ C<index> can be from 1 to 10.
 C<< prodoptI<index>_values >> - define the values for a product
 option, separated by the configured C<prodop_value_sep>.
 
+=item * C<< prodoptI<index>_custom.I<name> >> - define custom values
+called I<name> for each value specified by C<< prodoptI<index>_values
+>>, separated by the configured C<prodop_value_sep>.
+
 =item *
 
 C<< tier_price_I<tier_id> >> - set the product price for the specified
@@ -262,6 +266,13 @@ sub fill_leaf {
       or next;
     my @values = split /\Q$self->{prodopt_value_sep}/, $values
       or next;
+    my @custom = grep /^prodopt${opt_num}_custom\./, keys %entry;
+
+    my %custom_values;
+    for my $custom_name (@custom) {
+      (my $name = $custom_name) =~ s/^prodopt${opt_num}_custom\.//;
+      $custom_values{$name} = [ split /\Q$self->{prodopt_value_sep}/, $entry{$custom_name} ];
+    }
 
     my $option = BSE::TB::ProductOptions->make
       (
@@ -270,6 +281,7 @@ sub fill_leaf {
        display_order => $ordering++,
       );
 
+    my $index = 0;
     for my $value (@values) {
       my $entry = BSE::TB::ProductOptionValues->make
        (
@@ -277,6 +289,15 @@ sub fill_leaf {
         value => $value,
         display_order => $ordering++,
        );
+
+      for my $name (keys %custom_values) {
+       if ($index < @{$custom_values{$name}}) {
+         $entry->set_custom($name => $custom_values{$name}[$index]);
+       }
+      }
+      $entry->save;
+
+      ++$index;
     }
   }
 
index 99ae33640e49a8180b1c0fda6864c90f26a83064..bbfdfbc0cce2ae1c1b4d88c1947df257b76e4344 100644 (file)
@@ -58,6 +58,16 @@ sep_char=\\t
 codes=1
 code_field=product_code
 update_only=1
+
+[import profile withoption$when]
+map_linkAlias=1
+map_title=2
+map_prodopt1_name=3
+map_prodopt1_values=4
+map_prodopt1_custom.value_price=5
+map_retailPrice=6
+source=CSV
+skiplines=0
 CFG
 
 {
@@ -171,4 +181,37 @@ EOS
   }
 }
 
+{
+  my @added;
+
+  my $fh = File::Temp->new;
+  my $filename = $fh->filename;
+
+  print $fh <<EOS;
+test$when,"Test product option import $when","Colour","Red|Green|Blue","0|100|0",100
+EOS
+  close $fh;
+  my $imp = BSE::Importer->new(cfg => $cfg, profile => "withoption$when",
+                              callback => sub { note @_ });
+  $imp->process($filename);
+
+  is_deeply([ $imp->errors ], [], "check no errors");
+
+  @added = sort { $a->title cmp $b->title } $imp->leaves;
+  is(@added, 1, "Only added one");
+  my $one = $added[0];
+  is($one->title, "Test product option import $when", "check the title is set");
+  my @options = $one->db_options;
+  is(@options, 1, "should be one option");
+  is($options[0]->name, "Colour", "name of option");
+  my @values = $options[0]->values;
+  is(@values, 3, "should be three options");
+  is($values[0]->value, "Red", "first value text correct");
+  is($values[1]->get_custom("value_price"), "100", "second custom field correct");
+
+  END {
+    $_->remove($cfg) for @added;
+  }
+}
+
 done_testing();