interchange / interchange6-schema

DBIC schema for Interchange 6
8 stars 7 forks source link

find_or_create_variants #166

Closed hexfusion closed 9 years ago

hexfusion commented 9 years ago

Currently the process of creating attributes is separate from adding a product variant. I propose the following helper method.

find_or_create_variants where your attributes list is an array of hashes like this

{
  attributes => [
    {
      name => "color",
      title => "Color",
      value => "sage",
      value_title => "Sage" 
    },
    {
      name => "size",
      title => "Size",
      value => "L",
      values_title => "Large"
    }
  ],
  sku => "WY-CY07",
  gtin => "694264060xxx",
  name => "Kids Gore-Tex\x{ae} Stockingfoot",
  price => "199.95",
}

From here we can find_or_create both attributes and product variants. Eliminating a full population step that uses the same data.

racke commented 9 years ago

Two concerns:

hexfusion commented 9 years ago

I agree with control what if you can have the "option" of setting attributes lazy. So if you want to set attributes in this manner it is up to you to validate them before entry. Could even call this lazy_attributes with this caveat.

But with gtin a canonical product should never have one as the canonical product is not actually a product only a container for product variants. So in this case you have $product and are adding only variants which should either have unique gtin or undef. Also the attributes that are being used in this method are that of that actual variant so it shouldn't conflict.

racke commented 9 years ago

You need a way to add the GTINs per variant, so it is no longer so clean as your example above.

racke commented 9 years ago

I see ... but in this case it should be find_or_create_variant.

hexfusion commented 9 years ago

yes

racke commented 9 years ago

Which SKU is that? The canonical?

hexfusion commented 9 years ago

sku of variant the $product object has canonical_sku so full workflow would be.

my $product = shop_product->find({ sku => 'WBA-WY'});

$product->find_or_create_variant(
{
  lazy_attributes => [
    {
      name => "color",
      title => "Color",
      value => "sage",
      value_title => "Sage" 
    },
    {
      name => "size",
      title => "Size",
      value => "L",
      values_title => "Large"
    }
  ],
  sku => "WBA-WY-CY07",
  gtin => "690000000000",
  name => "Kids Gore-Tex\x{ae} Stockingfoot Sage Large",
  price => "199.95",
}
);
hexfusion commented 9 years ago

for this to be effective it would require defining priority for the attribute value as well. I guess the usecase for this would be limited to a small group of instances and could be created locally. I'm going to close this pending testing of this method in Angler::Populate.