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.
Public Properties ​
| Property | Description |
|---|---|
| allProductTypeIds | array – All the product type IDs |
| allProductTypes | array, craft\commerce\models\ProductType[] – All product types |
| behaviors | yii\base\Behavior – List of behaviors attached to this component. |
| creatableProductTypeIds | array |
| creatableProductTypes | array |
| editableProductTypeIds | array – all the product type IDs that are editable by the current user |
| editableProductTypes | array, craft\commerce\models\ProductType[] – all editable product types |
allProductTypeIds ​
- Type
- array
- Default value
null
All the product type IDs
allProductTypes ​
- Type
- array, craft\commerce\models\ProductType[]
- Default value
null
All product types
creatableProductTypeIds ​
- Type
- array
- Default value
null- Access
- Read-only
creatableProductTypes ​
- Type
- array
- Default value
null- Access
- Read-only
editableProductTypeIds ​
- Type
- array
- Default value
null
all the product type IDs that are editable by the current user
editableProductTypes ​
- Type
- array, craft\commerce\models\ProductType[]
- Default value
null
all editable product types
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 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.
Arguments ​
$event(craft\events\SiteEvent) – The event that triggered this.
Throws ​
- yii\base\Exception
- yii\base\ErrorException
- yii\base\InvalidConfigException
- yii\base\NotSupportedException
- yii\web\ServerErrorHttpException
deleteProductTypeById() ​
Deletes a product type by its ID.
Arguments ​
$id(integer) – The product type's ID
Returns ​
boolean – Whether the product type was deleted successfully.
Throws ​
- Throwable
if reasons
getAllProductTypeIds() ​
Returns all the product type IDs.
Returns ​
array – An array of all the product types’ IDs.
getAllProductTypes() ​
Returns all product types.
Returns ​
craft\commerce\models\ProductType[] – An array of all product types.
getCreatableProductTypeIds() ​
Returns all product type IDs that are creatable by the current user.
Returns ​
Throws ​
getCreatableProductTypes() ​
Returns all creatable product types.
Returns ​
Throws ​
getEditableProductTypeIds() ​
Returns all product type IDs that are editable by the current user.
Arguments ​
$anySite
Returns ​
array – An array of all the editable product types’ IDs.
getEditableProductTypes() ​
Returns all editable product types.
Returns ​
craft\commerce\models\ProductType[] – An array of all the editable product types.
getProductTypeByHandle() ​
Returns a product type by its handle.
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.
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.
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.
Arguments ​
$productTypeId(integer) – The product type ID
Returns ​
array – The product type settings.
getProductTypesByShippingCategoryId() ​
Returns all product types by a shipping category id.
Arguments ​
$shippingCategoryId
getProductTypesByTaxCategoryId() ​
Returns all product types by a tax category id.
Arguments ​
$taxCategoryId
handleChangedProductType() ​
Handle a product type change.
Arguments ​
$event
Throws ​
- Throwable
if reasons
handleDeletedProductType() ​
Handle a product type getting deleted.
Arguments ​
$event
Throws ​
- Throwable
if reasons
hasPermission() ​
Check if user has product type permission.
Arguments ​
$user(craft\elements\User)$productType(craft\commerce\models\ProductType)$checkPermissionName(string, null) – Detailed product type permission.
Returns ​
isProductTypeTemplateValid() ​
Returns whether a product type’s products have URLs, and if the template path is valid.
Arguments ​
$productType(craft\commerce\models\ProductType) – The product for which to validate the template.$siteId(integer) – The site for which to valid for
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.
pruneDeletedSite() ​
Prune a deleted site from category group site settings.
Arguments ​
$event
saveProductType() ​
Saves a product type.
Arguments ​
$productType(craft\commerce\models\ProductType) – The product type model.$runValidation(boolean) – If validation should be ran.
Returns ​
boolean – Whether the product type was saved successfully.
Throws ​
- Throwable
if reasons
Constants ​
| Constant | Description |
|---|---|
CONFIG_PRODUCTTYPES_KEY |
Events ​
EVENT_AFTER_SAVE_PRODUCTTYPE ​
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 ​
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
// ...
}
);