ProductTypes

Type
Class
Namespace
craft\commerce\services
Inherits
craft\commerce\services\ProductTypes » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
2.0

Product type service.

View source

# Public Properties

Property Description
allProductTypeIds array – An array of all the product types’ IDs.
allProductTypes craft\commerce\models\ProductType[] – An array of all product types.
behaviors yii\base\Behavior – List of behaviors attached to this component
editableProductTypeIds array – 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
Access
Read-only

An array of all the product types’ IDs.

View source

# allProductTypes

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

An array of all product types.

View source

# editableProductTypeIds

Type
array
Access
Read-only

An array of all the editable product types’ IDs.

View source

# editableProductTypes

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

An array of all the editable product types.

View source

# Public Methods

Method Description
__call() Calls the named method which is not a class method.
__clone() This method is called after the object is created by cloning an existing one.
__construct() Constructor.
__get() Returns the value of a component property.
__isset() Checks if a property is set, i.e. defined and not null.
__set() Sets the value of a component property.
__unset() Sets a component property to be null.
afterSaveSiteHandler() Adds a new product type setting row when a Site is added to Craft.
attachBehavior() Attaches a behavior to this component.
attachBehaviors() Attaches a list of behaviors to the component.
behaviors() Returns a list of behaviors that this component should behave as.
canGetProperty() Returns a value indicating whether a property can be read.
canSetProperty() Returns a value indicating whether a property can be set.
className() Returns the fully qualified name of this class.
deleteProductTypeById() Deletes a product type by its ID.
detachBehavior() Detaches a behavior from the component.
detachBehaviors() Detaches all behaviors from the component.
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component.
getAllProductTypeIds() Returns all of the product type IDs.
getAllProductTypes() Returns all product types.
getBehavior() Returns the named behavior object.
getBehaviors() 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() Returns a value indicating whether there is any handler attached to the named event.
hasMethod() Returns a value indicating whether a method is defined.
hasProperty() Returns a value indicating whether a property is defined for this component.
init() Initializes the object.
isProductTypeTemplateValid() Returns whether a product type’s products have URLs, and if the template path is valid.
off() Detaches an existing event handler from this component.
on() 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() Triggers an event.

# afterSaveSiteHandler()

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

View source

Arguments

# deleteProductTypeById()

Deletes a product type by its ID.

View source

Arguments

  • $id (integer) – The product type's ID

Returns

boolean – Whether the product type was deleted successfully.

Throws

# getAllProductTypeIds()

Returns all of the product type IDs.

View source

Returns

array – An array of all the product types’ IDs.

# getAllProductTypes()

Returns all product types.

View source

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

Returns

array – An array of all the editable product types’ IDs.

# getEditableProductTypes()

Returns all editable product types.

View source

Returns

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

# getProductTypeByHandle()

Returns a product type by its handle.

View source

Arguments

  • $handle (string) – The product type's handle.

Returns

craft\commerce\models\ProductType, null – The product type or null.

# getProductTypeById()

Returns a product type by its ID.

View source

Arguments

  • $productTypeId (integer) – The product type's ID

Returns

craft\commerce\models\ProductType, null – Either the product type or null

# getProductTypeByUid()

Returns a product type by its UID.

View source

Arguments

  • $uid (string) – The product type's UID

Returns

craft\commerce\models\ProductType, null – Either the product type or null

# getProductTypeSites()

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

View source

Arguments

  • $productTypeId (integer) – The product type ID

Returns

array – The product type settings.

# getProductTypesByShippingCategoryId()

Returns all product types by a shipping category id.

View source

Arguments

  • $shippingCategoryId

Returns

array

# getProductTypesByTaxCategoryId()

Returns all product types by a tax category id.

View source

Arguments

  • $taxCategoryId

Returns

array

# handleChangedProductType()

Handle a product type change.

View source

Arguments

Returns

void

Throws

# handleDeletedProductType()

Handle a product type getting deleted.

View source

Arguments

Returns

void

Throws

# isProductTypeTemplateValid()

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

View source

Arguments

Returns

boolean – Whether the template is valid.

Throws

# pruneDeletedField()

Prune a deleted field from category group layouts.

View source

Arguments

# pruneDeletedSite()

Prune a deleted site from category group site settings.

View source

Arguments

# saveProductType()

Saves a product type.

View source

Arguments

Returns

boolean – 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 has been saved.

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

Event::on(
    ProductTypes::class,
    ProductTypes::EVENT_AFTER_SAVE_PRODUCTTYPE,
    function(ProductTypeEvent $event) {
        // @var ProductType|null $productType
        $productType = $event->productType;

        // Prepare some third party system for a new product type
        // ...
    }
);

# EVENT_BEFORE_SAVE_PRODUCTTYPE

Type
craft\commerce\events\ProductTypeEvent

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

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

Event::on(
    ProductTypes::class,
    ProductTypes::EVENT_BEFORE_SAVE_PRODUCTTYPE,
    function(ProductTypeEvent $event) {
        // @var ProductType|null $productType
        $productType = $event->productType;
        
        // Create an audit trail of this action
        // ...
    }
);