The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Interchange6::Schema::Result::Product

DESCRIPTION

The products table contains three product types parent, child and single.

  • Parent Product A parent product is a container product in which variations of parent product or "child products" are linked.

  • Child Product A child product for example "Acme Pro 10lb Dumbbell" would include the canonical_sku of the parent item whose description might be something like "Acme Pro Dumbbell". In general a child product would contain attributes while a parent product would not.

  • Single Product A single product does not have child products and will become a parent product if a child product exists.

ACCESSORS

image

This simple accessor is available to resultset searches which wish to add column image to stash an image in the result.

sku

SKU used by shop.

Primary key.

manufacturer_sku

Manufacturer's sku.

Is nullable.

name

The name used to identify the product.

short_description

A brief summary of the product.

description

Full product description.

price

Numeric value representing product cost.

Defaults to 0.

When price is updated and product has related "discount" in Interchange6::Schema::Result::PriceModifier then also update the related "price" in Interchange6::Schema::Result::PriceModifier. This is done using the method update_price_modifiers.

uri

Unique product uri. Example "acme-pro-dumbbells". Is nullable.

weight

Numeric weight of the product. Defaults to zero.

priority

Display order priority.

gtin

Unique EAN or UPC type data. Is nullable.

canonical_sku

The SKU of the main product if this product is a variant of a main product. Is nullable.

active

Is this product active? Default is yes.

inventory_exempt

Is this product exempt from inventory? Default is no.

combine

Indicate whether products with the same SKU should be combined in the Cart.

Defaults to true.

created

Date and time when this record was created returned as DateTime object. Value is auto-set on insert.

last_modified

Date and time when this record was last modified returned as DateTime object. Value is auto-set on insert and update.

RELATIONS

canonical

Type: belongs_to

Related object: Interchange6::Schema::Result::Product

variants

Type: has_many

Related object: Interchange6::Schema::Result::Product

cart_products

Type: has_many

Related object: Interchange6::Schema::Result::CartProduct

price_modifiers

Type: has_many

Related object: Interchange6::Schema::Result::PriceModifier

inventory

Type: might_have

Related object: Interchange6::Schema::Result::Inventory

media_products

Type: has_many

Related object: Interchange6::Schema::Result::MediaProduct

merchandising_products

Type: has_many

Related object: Interchange6::Schema::Result::MerchandisingProduct

Type: has_many

Related object: Interchange6::Schema::Result::MerchandisingProduct

Type: has_many

Related object: Interchange6::Schema::Result::NavigationProduct

Type: many_to_many with navigation

orderlines

Type: has_many

Related object: Interchange6::Schema::Result::Orderline

product_attributes

Type: has_many

Related object: Interchange6::Schema::Result::ProductAttribute

media

Type: many_to_many with media

product_messages

Type: has_many

Related object: Interchange6::Schema::Result::ProductMessage

messages

Type: many_to_many

Accessor to related Message results.

METHODS

Attribute methods are provided by the Interchange6::Schema::Base::Attribute class.

insert

Override inherited method to call "generate_uri" method in case "name" and "sku" have been supplied as arguments but "uri" has not.

update_price_modifiers

Called when "price" is updated.

generate_uri($attrs)

Called by "new" if no uri is given as an argument.

The following steps are taken:

    1. Join $self->name and $self->uri with - and stash in $uri to allow manipulation via filters

    2. Remove leading and trailing spaces and replace remaining spaces and / with -

    3. Search for all rows in Interchange6::Schema::Result::Setting where scope is Product and name is <generate_uri_filter>

    4. For each row found eval $row->value

    5. Finally set the value of column "uri" to $uri

Filters stored in Interchange6::Schema::Result::Setting are executed via eval and have access to $uri and also the product result held in $self

Examples of filters stored in Setting might be:

    {
        scope => 'Product',
        name  => 'generate_uri_filter',
        value => '$uri =~ s/badstuff/goodstuff/gi',
    },
    {
        scope => 'Product',
        name  => 'generate_uri_filter',
        value => '$uri = lc($uri)',
    },

path

Produces navigation path for this product. Returns array reference in scalar context.

Uses $type to select specific taxonomy from navigation if present.

selling_price

Arguments should be given as a hash reference with the following keys/values:

  • quantity => $quantity

    quantity defaults to 1 if not supplied.

PriceModifier rows which have roles_id undefined are always included in the search in addition to any roles that belonging to "logger_in_user" in Schema. This enables promotional prices to be specified between fixed dates in "price" in Interchange6::Schema::Result::PriceModifier to apply to all classes of user whether logged in or not.

Returns lowest price from "price" and "price" in Interchange6::Schema::Result::PriceModifier.

Throws exception on bad arguments though unexpected keys in the hash reference will be silently discarded.

If the query was constructed using "with_lowest_selling_price" in Interchange6::Schema::ResultSet::Product then the cached value will be used rather than running a new query UNLESS arguments are supplied in which case a new query is performed.

highest_price

If this is a canonical product without variants or a variant product then this method will return undef. If highest price is the same as "selling_price" then we again return undef.

If the query was constructed using "with_highest_price" in Interchange6::Schema::ResultSet::Product then the cached value will be used rather than running a new query.

This method calls "variant_count" and "selling_price" so when constructing a resultset query consider also chaining the associated ResultSet methods.

find_variant \%input [\%match_info]

Find product variant with the given attribute values in $input.

Returns variant in case of success.

Returns undef in case of failure.

You can pass an optional hash reference \%match_info which is filled with attribute matches (only valid in case of failure).

attribute_iterator( %args )

Arguments: hashref => 1

Return a hashref of attributes keyed on attribute name instead of an arrayref.

Arguments: selected => $sku

Set the 'selected' SKU. For a child product this is set automatically.

Arguments: cond => $cond

Search condition to use. Default is:

    { 'attribute.type' => 'variant' }
Arguments: order_by => $order_by

Ordering to use in query. Default is:

    [
        { -desc => 'attribute.priority' },
        { -asc => 'attribute.title' },
        { -desc => 'attribute_value.priority' },
        { -asc => 'attribute_value.title' },
    ]

Set the 'selected' SKU. For a child product this is set automatically.

Returns: An arrayref of attributes complete with their respective attribute values.

For canonical products, it shows all the attributes of the child products.

For a child product, it shows all the attributes of the siblings.

Example of returned arrayref:

   [
     {
       attribute_values => [
         {
           priority => 2,
           selected => 0,
           title => "Pink",
           value => "pink"
         },
         {
           priority => 1,
           selected => 0,
           title => "Yellow",
           value => "yellow"
         }
       ],
       name => "color",
       priority => 2,
       title => "Color"
     },
     {
       attribute_values => [
         {
           priority => 2,
           selected => 0,
           title => "Small",
           value => "small"
         },
         {
           priority => 1,
           selected => 0,
           title => "Medium",
           value => "medium"
         },
       ],
       name => "size",
       priority => 1,
       title => "Size"
     }
   ]

add_variants @variants

Add variants from a list of hash references.

Returns product object.

Each hash reference contains attributes and column data which overrides data from the canonical product.

The canonical sku of the variant is automatically set.

Example for the hash reference (attributes in the first line):

     {color => 'yellow', size => 'small',
      sku => 'G0001-YELLOW-S',
      name => 'Six Small Yellow Tulips',
      uri => 'six-small-yellow-tulips'}

Since there is a risk that attributes names might clash with Product column names (for example "weight") an improved syntax exists to prevent such problems. This is considered to be the preferred syntax:

    {
        sku   => 'ROD00014-2-6-mid',
        uri   => 'fishingrod-weight-2-length-6-flex-mid',
        price => 355,
        attributes => [
            { weight => '2' },
            { length => '6' },
            { action => 'mid' },
        ],
    }

discount_percent

If "selling_price" is lower than "price" returns the rounded percentage discount or undef.

NOTE: for parent products (products that have variants) this will always return undef.

media_by_type

Return a Media resultset with the related media, filtered by type (e.g. video or image). On the results you can call display_uri("type") to get the actual uri.

product_reviews

Reviews should only be associated with parent products.

This method returns the related Interchange6::Schema::Result::ProductMessage records for a parent product where the related Interchange6::Schema::Result::Message has "name" in Interchange6::Schema::Result::MessageType of product_review. For a child product the ProductReview records for the parent are returned.

reviews

Reviews should only be associated with parent products. This method returns the related Message (reviews) records for a parent product. For a child product the Message records for the parent are returned.

Arguments are passed as paremeters to search the related reviews.

top_reviews

Returns the highest-rated approved public reviews for this product. Argument is max number of reviews to return which defaults to 5.

variant_count

Returns the number of variants of this product.

has_variants

Alias for "variant_count" for backwards-compatibility.

average_rating

Returns the average rating across all public and approved product reviews or undef if there are no reviews. Optional argument number of decimal places of precision must be a positive integer less than 10 which defaults to 1.

If the query was constructed using "with_average_rating" in Interchange6::Schema::ResultSet::Product then the cached value will be used rather than running a new query.

add_to_reviews

Reviews should only be associated with parent products. This method returns the related ProductReview records for a parent product. For a child product the ProductReview records for the parent are returned.

set_reviews

Arguments: (\@hashrefs_of_col_data | \@result_objs)
Return Value: not defined

Similar to "set_$rel" in DBIx::Class::Relationship::Base except that this method DOES delete objects in the table on the right side of the relation.

quantity_in_stock

Returns undef if inventory_exempt is true and otherwise returns the quantity of the product in the inventory. For a product variant the quantity returned is for the variant itself whereas for a canonical (parent) product the quantity returned is the total for all variants.

If the query was constructed using "with_quantity_in_stock" in Interchange6::Schema::ResultSet::Product then the cached value will be used rather than running a new query.

delete

Overload delete to force removal of any product reviews. Only parent products should have reviews so in the case of child products no attempt is made to delete reviews.