ProductTypes

Type
Class
Namespace
craft\commerce\services
Inherits
craft\commerce\services\ProductTypes » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window)
Since
2.0

Product type service.

View source (opens new window)

# Public Properties

Property Description
allProductTypeIds array (opens new window) – An array of all the product types’ IDs.
allProductTypes craft\commerce\models\ProductType[] – An array of all product types.
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component
editableProductTypeIds array (opens new window) – An array of all the editable product types’ IDs.
editableProductTypes craft\commerce\models\ProductType[] – An array of all the editable product types.

# allProductTypeIds

Type
array (opens new window)
Access
Read-only

An array of all the product types’ IDs.

View source (opens new window)

# allProductTypes

Type
craft\commerce\models\ProductType[]
Access
Read-only

An array of all product types.

View source (opens new window)

# editableProductTypeIds

Type
array (opens new window)
Access
Read-only

An array of all the editable product types’ IDs.

View source (opens new window)

# editableProductTypes

Type
craft\commerce\models\ProductType[]
Access
Read-only

An array of all the editable product types.

View source (opens new window)

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) This method is called after the object is created by cloning an existing one.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of an object property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets value of an object property.
__unset() (opens new window) Sets an object property to null.
afterSaveSiteHandler() Adds a new product type setting row when a Site is added to Craft.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
className() (opens new window) Returns the fully qualified name of this class.
deleteProductTypeById() Deletes a product type by its ID.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
getAllProductTypeIds() Returns all of the product type IDs.
getAllProductTypes() Returns all product types.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
getEditableProductTypeIds() Returns all of the product type IDs that are editable by the current user.
getEditableProductTypes() Returns all editable product types.
getProductTypeByHandle() Returns a product type by its handle.
getProductTypeById() Returns a product type by its ID.
getProductTypeByUid() Returns a product type by its UID.
getProductTypeSites() Returns an array of product type site settings for a product type by its ID.
getProductTypesByShippingCategoryId() Returns all product types by a shipping category id.
getProductTypesByTaxCategoryId() Returns all product types by a tax category id.
handleChangedProductType() Handle a product type change.
handleDeletedProductType() Handle a product type getting deleted.
hasEventHandlers() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasProperty() (opens new window) Returns a value indicating whether a property is defined.
init() (opens new window) Initializes the object.
isProductTypeTemplateValid() Returns whether a product type’s products have URLs, and if the template path is valid.
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
pruneDeletedField() Prune a deleted field from category group layouts.
pruneDeletedSite() Prune a deleted site from category group site settings.
saveProductType() Saves a product type.
trigger() (opens new window) Triggers an event.

# afterSaveSiteHandler()

Adds a new product type setting row when a Site is added to Craft.

View source (opens new window)

Arguments

# deleteProductTypeById()

Deletes a product type by its ID.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the product type was deleted successfully.

Throws

# getAllProductTypeIds()

Returns all of the product type IDs.

View source (opens new window)

Returns

array (opens new window) – An array of all the product types’ IDs.

# getAllProductTypes()

Returns all product types.

View source (opens new window)

Returns

craft\commerce\models\ProductType[] – An array of all product types.

# getEditableProductTypeIds()

Returns all of the product type IDs that are editable by the current user.

View source (opens new window)

Returns

array (opens new window) – An array of all the editable product types’ IDs.

# getEditableProductTypes()

Returns all editable product types.

View source (opens new window)

Returns

craft\commerce\models\ProductType[] – An array of all the editable product types.

# getProductTypeByHandle()

Returns a product type by its handle.

View source (opens new window)

Arguments

Returns

craft\commerce\models\ProductType, null (opens new window) – The product type or null.

# getProductTypeById()

Returns a product type by its ID.

View source (opens new window)

Arguments

Returns

craft\commerce\models\ProductType, null (opens new window) – Either the product type or null

# getProductTypeByUid()

Returns a product type by its UID.

View source (opens new window)

Arguments

Returns

craft\commerce\models\ProductType, null (opens new window) – Either the product type or null

# getProductTypeSites()

Returns an array of product type site settings for a product type by its ID.

View source (opens new window)

Arguments

Returns

array (opens new window) – The product type settings.

# getProductTypesByShippingCategoryId()

Returns all product types by a shipping category id.

View source (opens new window)

Arguments

  • $shippingCategoryId

Returns

array (opens new window)

# getProductTypesByTaxCategoryId()

Returns all product types by a tax category id.

View source (opens new window)

Arguments

  • $taxCategoryId

Returns

array (opens new window)

# handleChangedProductType()

Handle a product type change.

View source (opens new window)

Arguments

Returns

void

Throws

# handleDeletedProductType()

Handle a product type getting deleted.

View source (opens new window)

Arguments

Returns

void

Throws

# isProductTypeTemplateValid()

Returns whether a product type’s products have URLs, and if the template path is valid.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the template is valid.

Throws

# pruneDeletedField()

Prune a deleted field from category group layouts.

View source (opens new window)

Arguments

# pruneDeletedSite()

Prune a deleted site from category group site settings.

View source (opens new window)

Arguments

# saveProductType()

Saves a product type.

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the product type was saved successfully.

Throws

# Constants

Constant Description
CONFIG_PRODUCTTYPES_KEY

# Events

# EVENT_AFTER_SAVE_PRODUCTTYPE

Type
craft\commerce\events\ProductTypeEvent

The event that is triggered after a product type is saved.

Plugins can get notified after a product type has been saved.

use craft\commerce\events\ProductTypeEvent;
use craft\commerce\services\ProductTypes;
use yii\base\Event;

Event::on(ProductTypes::class, ProductTypes::EVENT_AFTER_SAVE_PRODUCTTYPE, function(ProductTypeEvent $e) {
     // Maybe prepare some 3rd party system for a new product type
});

# EVENT_BEFORE_SAVE_PRODUCTTYPE

Type
craft\commerce\events\ProductTypeEvent

The event that is triggered before a category group is saved.

Plugins can get notified before a product type is being saved.

use craft\commerce\events\ProductTypeEvent;
use craft\commerce\services\ProductTypes;
use yii\base\Event;

Event::on(ProductTypes::class, ProductTypes::EVENT_BEFORE_SAVE_PRODUCTTYPE, function(ProductTypeEvent $e) {
     // Maybe create an audit trail of this action.
});