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 – An array of all the product types’ IDs.
$allProductTypescraft\commerce\models\ProductType[] – An array of all product types.
$behaviorsyii\base\Behavior – List of behaviors attached to this component
$editableProductTypeIdsarray – An array of all the editable product types’ IDs.
$editableProductTypescraft\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

Signature

public array getAllProductTypeIds ( )

$allProductTypes

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

An array of all product types.

View source

Signature

public craft\commerce\models\ProductType[] getAllProductTypes ( )

$editableProductTypeIds

Type
array
Access
Read-only

An array of all the editable product types’ IDs.

View source

Signature

public array getEditableProductTypeIds ( )

$editableProductTypes

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

An array of all the editable product types.

View source

Signature

public craft\commerce\models\ProductType[] getEditableProductTypes ( )

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 an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to 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.
getProductTypeSites()Returns an array of product type site settings for a product type by its ID.
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.
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.
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

Signature

public void afterSaveSiteHandler ( craft\events\SiteEvent $event )

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

Signature

public boolean deleteProductTypeById ( \craft\commerce\services\int $id )

getAllProductTypeIds()

Returns all of the product type IDs.

View source

Returns

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

Signature

public array getAllProductTypeIds ( )

getAllProductTypes()

Returns all product types.

View source

Returns

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

Signature

public craft\commerce\models\ProductType[] getAllProductTypes ( )

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.

Signature

public array getEditableProductTypeIds ( )

getEditableProductTypes()

Returns all editable product types.

View source

Returns

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

Signature

public craft\commerce\models\ProductType[] getEditableProductTypes ( )

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.

Signature

public craft\commerce\models\ProductType, null getProductTypeByHandle ( $handle )

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

Signature

public craft\commerce\models\ProductType, null getProductTypeById ( \craft\commerce\services\int $productTypeId )

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.

Signature

public array getProductTypeSites ( $productTypeId )

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

Signature

public boolean isProductTypeTemplateValid ( craft\commerce\models\ProductType $productType, \craft\commerce\services\int $siteId )

saveProductType()

Saves a product type.

View source

Arguments

Returns

boolean – Whether the product type was saved successfully.

Throws

Signature

public boolean saveProductType ( craft\commerce\models\ProductType $productType, \craft\commerce\services\bool $runValidation = true )

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