Skip to content

PurchasableInterface

Type
Interface
Namespace
craft\commerce\base
Extends
craft\base\ElementInterface
Implemented by
craft\commerce\base\Purchasable, craft\commerce\elements\Donation, craft\commerce\elements\Variant, craft\commerce\test\mockclasses\Purchasable
Since
2.0

Interface Purchasable

View source

Public Methods

MethodDescription
__toString()Returns the string representation of the element.
actions()Returns the available bulk element actions for a given source.
addInvalidNestedElementIds()Registers invalid nested element IDs with the element, so an error class can be added on their cards.
afterDelete()Performs actions after an element is deleted.
afterDeleteForSite()Performs actions after an element is deleted for a site.
afterMoveInStructure()Performs actions after an element is moved within a structure.
afterOrderComplete()Runs any logic needed for this purchasable after it was on an order that was just completed (not when an order was paid, although paying an order will complete it).
afterPropagate()Performs actions after an element is fully saved and propagated to other sites.
afterRestore()Performs actions after an element is restored.
afterSave()Performs actions after an element is saved.
attributePreviewHtml()Return HTML for the attribute in the card preview.
beforeDelete()Performs actions before an element is deleted.
beforeDeleteForSite()Performs actions before an element is deleted for a site.
beforeMoveInStructure()Performs actions before an element is moved within a structure.
beforeRestore()Performs actions before an element is restored.
beforeSave()Performs actions before an element is saved.
canCreateDrafts()Returns whether the given user is authorized to create drafts for this element.
canDelete()Returns whether the given user is authorized to delete this element.
canDeleteForSite()Returns whether the given user is authorized to delete this element for its current site.
canDuplicate()Returns whether the given user is authorized to duplicate this element.
canDuplicateAsDraft()Returns whether the given user is authorized to duplicate this element as an unpublished draft.
canSave()Returns whether the given user is authorized to save this element in its current form.
canView()Returns whether the given user is authorized to view this element’s edit page.
cardAttributes()Defines all the available attributes that can be shown in card views.
createAnother()Creates a new element (without saving it) based on this one.
createCondition()Returns an element condition for the element type.
defaultCardAttributes()Returns the list of card attribute keys that should be shown by default, if the field layout hasn't been customised.
defaultTableAttributes()Returns the list of table attribute keys that should be shown by default.
displayName()Returns the display name of this class.
eagerLoadingMap()Returns an array that maps source-to-target element IDs based on the given sub-property handle.
exporters()Returns the available export options for a given source.
extraFields()Returns the list of additional fields that can be returned by toArray() in addition to those listed in fields().
fieldLayouts()Returns all the field layouts associated with elements from the given source.
fields()Returns the list of fields that should be returned by default by toArray() when no specific fields are specified.
find()Creates an craft\elements\db\ElementQueryInterface instance for query purpose.
findAll()Returns a list of elements that match the specified ID(s) or a set of element criteria parameters.
findOne()Returns a single element instance by a primary key or a set of element criteria parameters.
findSource()Returns a source definition by a given source key/path and context.
get()Returns a component by its ID.
getActionMenuItems()Returns action menu items for the component.
getAdditionalButtons()Returns additional buttons that should be shown at the top of the element’s edit page.
getAncestors()Returns the element’s ancestors.
getAttributeHtml()Returns the HTML that should be shown for a given attribute in table and card views.
getAttributeStatus()Returns the status of a given attribute.
getCacheTags()Returns the cache tags that should be cleared when this element is saved.
getCanonical()Returns the canonical version of the element.
getCanonicalId()Returns the element’s canonical ID.
getCanonicalUid()Returns the element’s canonical UUID.
getCardBodyHtml()Returns the body HTML for element cards.
getChildren()Returns the element’s children.
getChipLabelHtml()Returns the label HTML for element chips.
getCpEditUrl()Returns the URL to the component’s edit page in the control panel.
getCpRevisionsUrl()Returns the element’s revisions index URL in the control panel.
getCrumbs()Returns the breadcrumbs that lead up to the element.
getCurrentRevision()Returns the element’s current revision, if one exists.
getDescendants()Returns the element’s descendants.
getDescription()Returns your element's title or any additional descriptive information.
getDirtyAttributes()Returns a list of attribute names that have changed since the element was first loaded.
getDirtyFields()Returns a list of custom field handles that have changed since the element was first loaded.
getEagerLoadedElementCount()Returns the count of eager-loaded elements for a given handle.
getEagerLoadedElements()Returns the eager-loaded elements for a given handle.
getEnabledForSite()Returns whether the element is enabled for the current site.
getFieldContext()Returns the field context this element’s content uses.
getFieldLayout()Returns the field layout used by this element.
getFieldParamNamespace()Returns the namespace used by custom field params on the request.
getFieldValue()Returns the value for a given field.
getFieldValues()Returns the element’s normalized custom field values, indexed by their handles.
getGqlTypeName()Returns the GraphQL type name for this element type.
getHasDescendants()Returns whether the element has descendants.
getHtmlAttributes()Returns any attributes that should be included in the element’s chips and cards.
getId()Returns the ID of the component, which should be used as the value of hidden inputs.
getInlineAttributeInputHtml()Returns the HTML that should be shown for a given attribute's inline editing input.
getInvalidNestedElementIds()Returns the element’s invalid nested element IDs.
getIsAvailable()Returns whether the purchasable is currently available for purchase.
getIsCanonical()Returns whether this is the canonical element.
getIsDerivative()Returns whether this is a derivative element, such as a draft or revision.
getIsDraft()Returns whether this is a draft.
getIsFresh()Returns whether the element is "fresh" (not yet explicitly saved, and without validation errors).
getIsHomepage()Returns whether this element represents the site homepage.
getIsPromotable()Returns whether this purchasable can be subject to discounts or sales.
getIsRevision()Returns whether this is a revision.
getIsShippable()Returns whether this purchasable can be shipped and whether it is counted in shipping calculations.
getIsSlugTranslatable()Returns whether the Slug field should be shown as translatable in the UI.
getIsTaxable()Returns whether this purchasable is exempt from taxes.
getIsTitleTranslatable()Returns whether the Title field should be shown as translatable in the UI.
getIsUnpublishedDraft()Returns whether the element is an unpublished draft.
getLanguage()Returns the language of the element.
getLineItemRules()Returns any validation rules this purchasable required the line item to have.
getLink()Returns an anchor pre-filled with this element’s URL and title.
getLocalized()Returns the same element in other locales.
getMetadata()Returns element metadata that should be shown within the editor sidebar.
getModifiedAttributes()Returns the attribute names that have changed for this element.
getModifiedFields()Returns the field handles that have changed for this element.
getNext()Returns the next element relative to this one, from a given set of criteria.
getNextSibling()Returns the element’s next sibling.
getOutdatedAttributes()Returns the attribute names that have been updated on the canonical element since the last time it was merged into this element.
getOutdatedFields()Returns the field handles that have been updated on the canonical element since the last time it was merged into this element.
getParent()Returns the element’s parent.
getParentUri()Returns the parent element’s URI, if there is one.
getPostEditUrl()Returns the URL that users should be redirected to after editing the element.
getPrev()Returns the previous element relative to this one, from a given set of criteria.
getPrevSibling()Returns the element’s previous sibling.
getPreviewTargets()Returns the additional locations that should be available for previewing the element, besides its primary URL.
getPrice()Returns the live price including catalog rule pricing.
getPromotionRelationSource()Returns the source param used for knowing if a promotion category is related to this purchasable.
getPromotionalPrice()Returns the live promotional price including the catalog rule pricing.
getRef()Returns the reference string to this element.
getRootOwner()Returns the root owner element.
getRoute()Returns the route that should be used when the element’s URI is requested.
getSalePrice()Returns the actual price the purchasable will be sold for.
getSearchKeywords()Returns the search keywords for a given search attribute.
getSerializedFieldValues()Returns an array of the element’s serialized custom field values, indexed by their handles.
getShippingCategory()Returns the purchasable's shipping category.
getSiblings()Returns all of the element’s siblings.
getSidebarHtml()Returns the HTML for any fields/info that should be shown within the editor sidebar.
getSite()Returns the site the element is associated with.
getSku()Returns a unique code. Unique as per the commerce_purchasables table.
getSlugTranslationDescription()Returns the description of the Slug field’s translation support.
getSlugTranslationKey()Returns the Slug’s translation key.
getSnapshot()Returns an array of data that is serializable to json for storing a line item at time of adding to the cart or order.
getStatus()Returns the component’s status.
getStore()Returns the store for the current instance of the purchasable.
getStoreId()Returns the store ID for the current instance of the purchasable.
getSupportedSites()Returns the sites this element is associated with.
getTaxCategory()Returns the purchasable's tax category.
getThumbHtml()Returns the HTML for the component’s thumbnail, if it has one.
getTitleTranslationDescription()Returns the description of the Title field’s translation support.
getTitleTranslationKey()Returns the Title’s translation key.
getTotalDescendants()Returns the total number of descendants that the element has.
getUiLabel()Returns what the component should be called within the control panel.
getUiLabelPath()Returns any path segment labels that should be prepended to the element’s UI label.
getUriFormat()Returns the URI format used to generate this element’s URI.
getUrl()Returns the element’s full URL.
gqlScopesByContext()Returns the GraphQL scopes required by element’s context.
hasDrafts()Returns whether element indexes should show the “Drafts” status option.
hasEagerLoadedElements()Returns whether elements have been eager-loaded with a given handle.
hasFreeShipping()Returns whether this purchasable has free shipping.
hasRevisions()Returns whether revisions should be created when this element is saved.
hasStatuses()Returns whether elements of this type have statuses.
hasThumbs()Returns whether element indexes should include a thumbnail view by default.
hasTitles()Returns whether elements of this type have traditional titles.
hasUris()Returns whether elements of this type can have their own slugs and URIs.
indexElementCount()Returns the total number of elements that will be shown on an element index, for the given element query.
indexHtml()Returns the element index HTML.
indexViewModes()Returns the view modes available for the element type.
instance()Returns static class instance, which can be used to obtain meta information.
isAncestorOf()Returns whether this element is an ancestor of another one.
isAttributeDirty()Returns whether an attribute has changed since the element was first loaded.
isAttributeModified()Returns whether an attribute value has changed for this element.
isAttributeOutdated()Returns whether an attribute value has fallen behind the canonical element’s value.
isChildOf()Returns whether this element is a direct child of another one.
isDescendantOf()Returns whether this element is a descendant of another one.
isFieldDirty()Returns whether a custom field value has changed since the element was first loaded.
isFieldEmpty()Returns whether a field is empty.
isFieldModified()Returns whether a field value has changed for this element.
isFieldOutdated()Returns whether a field value has fallen behind the canonical element’s value.
isLocalized()Returns whether elements of this type store content on a per-site basis.
isNextSiblingOf()Returns whether this element is the direct next sibling of another one.
isParentOf()Returns whether this element is a direct parent of another one.
isPrevSiblingOf()Returns whether this element is the direct previous sibling of another one.
isSelectable()Returns whether the component should be selectable in component Type selects.
isSiblingOf()Returns whether this element is a sibling of another one.
lowerDisplayName()Returns the lowercase version of displayName().
markAsClean()Resets the record of dirty attributes and fields.
markAsDirty()Marks all fields and attributes as dirty.
mergeCanonicalChanges()Merges changes from the canonical element into this one.
modifyCustomSource()Modifies a custom source’s config, before it’s returned by craft\services\ElementSources::getSources()
offsetExists()Treats custom fields as array offsets.
pluralDisplayName()Returns the plural version of displayName().
pluralLowerDisplayName()Returns the plural, lowercase version of displayName().
populateLineItem()Populates the line item when this purchasable is found on it. Called when Purchasable is added to the cart and when the cart recalculates.
prepareEditScreen()Prepares the response for the element’s Edit screen.
refHandle()Returns the handle that should be used to refer to this element type from reference tags.
searchableAttributes()Defines which element attributes should be searchable.
setAttributesFromRequest()Sets the element’s attributes from an element editor submission.
setCanonical()Sets the canonical version of the element.
setCanonicalId()Sets the element’s canonical ID.
setDirtyAttributes()Sets the list of dirty attribute names.
setDirtyFields()Sets the list of dirty field handles.
setEagerLoadedElementCount()Sets the count of eager-loaded elements for a given handle.
setEagerLoadedElements()Sets some eager-loaded elements on a given handle.
setEnabledForSite()Sets whether the element is enabled for the current site.
setFieldParamNamespace()Sets the namespace used by custom field params on the request.
setFieldValue()Sets the value for a given field.
setFieldValueFromRequest()Sets the value for a given field. The value should have originated from post data.
setFieldValues()Sets the element’s custom field values.
setFieldValuesFromRequest()Sets the element’s custom field values, when the values have come from post data.
setIsFresh()Sets whether the element is "fresh" (not yet explicitly saved, and without validation errors).
setLazyEagerLoadedElements()Sets whether the given eager-loaded element handles were eager-loaded lazily.
setNext()Sets the default next element.
setParent()Sets the element’s parent.
setPrev()Sets the default previous element.
setRevisionCreatorId()Sets the revision creator ID to be saved.
setRevisionNotes()Sets the revision notes to be saved.
setUiLabel()Defines what the element should be called within the control panel.
setUiLabelPath()Defines any path segment labels that should be prepended to the element’s UI label.
showStatusIndicator()Returns whether chips and cards for this element should include a status indicator.
sortOptions()Returns the sort options for the element type.
sourcePath()Returns the source path for a given source key, step key, and context.
sources()Returns the source definitions that elements of this type may belong to.
statuses()Returns all the possible statuses that components may have.
tableAttributes()Defines all of the available columns that can be shown in table views.
toArray()Converts the object into an array.
trackChanges()Returns whether Craft should keep track of attribute and custom field changes made to this element type, including when the last time they were changed, and who was logged-in at the time.

afterOrderComplete()

Runs any logic needed for this purchasable after it was on an order that was just completed (not when an order was paid, although paying an order will complete it).

This is called for each line item the purchasable was contained within.

View source

Arguments

getDescription()

Returns your element's title or any additional descriptive information.

View source

getIsAvailable()

Returns whether the purchasable is currently available for purchase.

View source

getIsPromotable()

Returns whether this purchasable can be subject to discounts or sales.

View source

getIsShippable()

Returns whether this purchasable can be shipped and whether it is counted in shipping calculations.

View source

getIsTaxable()

Returns whether this purchasable is exempt from taxes.

View source

getLineItemRules()

Returns any validation rules this purchasable required the line item to have.

View source

Arguments

Returns

array

getPrice()

Returns the live price including catalog rule pricing.

View source

Returns

float, null – Decimal(14,4)

getPromotionRelationSource()

Returns the source param used for knowing if a promotion category is related to this purchasable.

View source

Returns

mixed

getPromotionalPrice()

Since
5.0.0

Returns the live promotional price including the catalog rule pricing.

View source

Returns

float, null – Decimal(14,4)

getSalePrice()

Returns the actual price the purchasable will be sold for.

View source

Returns

float, null – Decimal(14,4)

getShippingCategory()

Returns the purchasable's shipping category.

View source

getSku()

Returns a unique code. Unique as per the commerce_purchasables table.

View source

getSnapshot()

Returns an array of data that is serializable to json for storing a line item at time of adding to the cart or order.

View source

getStore()

Returns the store for the current instance of the purchasable.

View source

Returns

craft\commerce\models\Store

getStoreId()

Returns the store ID for the current instance of the purchasable.

View source

Returns

integer

getTaxCategory()

Returns the purchasable's tax category.

View source

hasFreeShipping()

Returns whether this purchasable has free shipping.

View source

populateLineItem()

Populates the line item when this purchasable is found on it. Called when Purchasable is added to the cart and when the cart recalculates.

This is your chance to modify the weight, height, width, length, price and saleAmount. This is called before any LineItems::EVENT_POPULATE_LINE_ITEM event listeners.

View source

Arguments

  • $lineItem