Skip to content

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 ​

PropertyDescription
allProductTypeIdsarray – All the product type IDs
allProductTypesarray, craft\commerce\models\ProductType[] – All product types
behaviorsyii\base\Behavior – List of behaviors attached to this component.
creatableProductTypeIdsarray
creatableProductTypesarray
editableProductTypeIdsarray – all the product type IDs that are editable by the current user
editableProductTypesarray, craft\commerce\models\ProductType[] – all editable product types

allProductTypeIds ​

Type
array
Default value
null

All the product type IDs

View source

allProductTypes ​

Type
array, craft\commerce\models\ProductType[]
Default value
null

All product types

View source

creatableProductTypeIds ​

Type
array
Default value
null
Access
Read-only

View source

creatableProductTypes ​

Type
array
Default value
null
Access
Read-only

View source

editableProductTypeIds ​

Type
array
Default value
null

all the product type IDs that are editable by the current user

View source

editableProductTypes ​

Type
array, craft\commerce\models\ProductType[]
Default value
null

all editable product types

View source

Public Methods ​

MethodDescription
__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 the product type IDs.
getAllProductTypes()Returns all product types.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getCreatableProductTypeIds()Returns all product type IDs that are creatable by the current user.
getCreatableProductTypes()Returns all creatable product types.
getEditableProductTypeIds()Returns all 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.
hasPermission()Check if user has product type permission.
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()
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 ​

Throws ​

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 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.

getCreatableProductTypeIds() ​

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

View source

Returns ​

array

Throws ​

getCreatableProductTypes() ​

Returns all creatable product types.

View source

Returns ​

array

Throws ​

getEditableProductTypeIds() ​

Returns all 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

getProductTypesByTaxCategoryId() ​

Returns all product types by a tax category id.

View source

Arguments ​

  • $taxCategoryId

handleChangedProductType() ​

Handle a product type change.

View source

Arguments ​

  • $event

Throws ​

handleDeletedProductType() ​

Handle a product type getting deleted.

View source

Arguments ​

  • $event

Throws ​

hasPermission() ​

Check if user has product type permission.

View source

Arguments ​

Returns ​

boolean

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() ​

DEPRECATED

Deprecated in 3.4.17. Unused fields will be pruned automatically as field layouts are resaved.

View source

pruneDeletedSite() ​

Prune a deleted site from category group site settings.

View source

Arguments ​

  • $event

saveProductType() ​

Saves a product type.

View source

Arguments ​

Returns ​

boolean – Whether the product type was saved successfully.

Throws ​

Constants ​

ConstantDescription
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.

php
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.

php
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
        // ...
    }
);