From 7c6989996c94cbc2c79c915f3d667402a75cc12c Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Thu, 13 Jul 2023 11:19:10 +0100 Subject: [PATCH 1/2] Add example event code --- docs/commerce/4.x/extend/events.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/commerce/4.x/extend/events.md b/docs/commerce/4.x/extend/events.md index c739d76d6..f2ff46be7 100644 --- a/docs/commerce/4.x/extend/events.md +++ b/docs/commerce/4.x/extend/events.md @@ -1854,3 +1854,23 @@ Event::on( } ); ``` + +### `modifyPurchasablesTableQuery` + +The event that is triggered retrieving the list of purchasables for the add a line item feature on the order edit page. + +This example adds a condition to the query to only return purchasables with a SKU that contains `foo` + +```php +use craft\commerce\controllers\OrdersController; +use craft\commerce\events\ModifyPurchasablesTableQueryEvent; + +Event::on( + OrdersController::class, + OrdersController::EVENT_MODIFY_PURCHASABLES_TABLE_QUERY, + function(ModifyPurchasablesTableQueryEvent $event) { + $likeOperator = Craft::$app->getDb()->getIsPgsql() ? 'ILIKE' : 'LIKE'; + $event->query->andWhere([$likeOperator, 'sku', 'foo']); + } +); +``` From 55c675138acef12277174bad21d73f9b15faad69 Mon Sep 17 00:00:00 2001 From: August Miller Date: Tue, 18 Jul 2023 14:10:03 -0700 Subject: [PATCH 2/2] Organization/hierarchy of Commerce events page --- docs/4.x/extend/README.md | 4 + docs/commerce/4.x/extend/events.md | 314 ++++++++++++++--------------- 2 files changed, 161 insertions(+), 157 deletions(-) diff --git a/docs/4.x/extend/README.md b/docs/4.x/extend/README.md index a8f92818d..0cf0036e6 100644 --- a/docs/4.x/extend/README.md +++ b/docs/4.x/extend/README.md @@ -146,6 +146,10 @@ Any time you’re working with PHP, [PhpStorm](https://www.jetbrains.com/phpstor DDEV comes [pre-configured with xdebug](https://ddev.readthedocs.io/en/stable/users/debugging-profiling/step-debugging/), and can be connected to your IDE of choice to support breakpoints and step-debugging. +#### Debug Toolbar + +Yii’s built-in [debug toolbar](repo:yiisoft/yii2-debug) is invaluable, especially while troubleshooting database queries, [events](events.md), and other performance issues. + #### Composer If your journey with Craft so far has not involved [Composer](https://getcomposer.org), certain concepts (like namespacing and auto-loading) may present additional difficulty. Consider reviewing our article on using the [starter project](kb:using-the-starter-project), and try running [updates](../updating.md#composer) or installing a plugin with Composer. diff --git a/docs/commerce/4.x/extend/events.md b/docs/commerce/4.x/extend/events.md index f2ff46be7..7c0170150 100644 --- a/docs/commerce/4.x/extend/events.md +++ b/docs/commerce/4.x/extend/events.md @@ -1,16 +1,26 @@ +--- +sidebarDepth: 2 +--- + # Events -Craft Commerce provides a multitude of events for extending its functionality. Modules and plugins can [register event listeners](https://craftcms.com/knowledge-base/custom-module-events), typically in their `init()` methods, to modify Commerce’s behavior. +Craft Commerce provides a multitude of events for extending its functionality. Modules and plugins can [register event listeners](/4.x/extend/events.md), typically in their `init()` methods, to modify Commerce’s behavior. ## Event Code Generator -Select an event for details and a code snippet. See Craft’s [Events](/4.x/extend/events.md) page for Craft and Yii events. +Select an event for details and a code snippet, or check out the sections below for some use cases. See Craft’s [Events](/4.x/extend/events.md) page for more information about working with events. -## Variant Events +## Common Events + Workflows + +The following sections highlight a few of the events emitted by Commerce classes. It is not exhaustive—meaning you may need to enlist the help of one or more [development tools](/4.x/extend/README.md#tools) to aid in [discovering the “right” event](/4.x/extend/events.md#discovering-events)! + +### Product + Variant Events + +[Products and variants](../products-variants.md) are [elements](/4.x/extend/element-types.md), so all the standard lifecycle events (for elements and their associated query classes) apply here. -### `beforeCaptureVariantSnapshot` +#### `beforeCaptureVariantSnapshot` The event that is triggered before a variant’s field data is captured, which makes it possible to customize which fields are included in the snapshot. Custom fields are not included by default. @@ -27,7 +37,7 @@ Event::on( function(CustomizeVariantSnapshotFieldsEvent $event) { // @var Variant $variant $variant = $event->variant; - // @var array|null $fields + // @var string[]|null $fields $fields = $event->fields; // Add every custom field to the snapshot @@ -46,7 +56,7 @@ Event::on( Add with care! A huge amount of custom fields/data will increase your database size. ::: -### `afterCaptureVariantSnapshot` +#### `afterCaptureVariantSnapshot` The event that is triggered after a variant’s field data is captured. This makes it possible to customize, extend, or redact the data to be persisted on the variant instance. @@ -70,7 +80,7 @@ Event::on( ); ``` -### `beforeCaptureProductSnapshot` +#### `beforeCaptureProductSnapshot` The event that is triggered before a product’s field data is captured. This makes it possible to customize which fields are included in the snapshot. Custom fields are not included by default. @@ -107,7 +117,7 @@ Event::on( Add with care! A huge amount of custom fields/data will increase your database size. ::: -### `afterCaptureProductSnapshot` +#### `afterCaptureProductSnapshot` The event that is triggered after a product’s field data is captured, which can be used to customize, extend, or redact the data to be persisted on the product instance. @@ -132,9 +142,9 @@ Event::on( ); ``` -## Sale Events +### Sale Events -### `beforeMatchPurchasableSale` +#### `beforeMatchPurchasableSale` The event that is triggered before Commerce attempts to match a sale to a purchasable. @@ -165,7 +175,7 @@ Event::on( ); ``` -### `beforeSaveSale` +#### `beforeSaveSale` The event that is triggered before a sale is saved. @@ -188,7 +198,7 @@ Event::on( ); ``` -### `afterSaveSale` +#### `afterSaveSale` The event that is triggered after a sale is saved. @@ -211,7 +221,7 @@ Event::on( ); ``` -### `afterDeleteSale` +#### `afterDeleteSale` The event that is triggered after a sale is deleted. @@ -235,9 +245,9 @@ Event::on( ``` -## Order Events +### Order Events -### `beforeAddLineItemToOrder` +#### `beforeAddLineItemToOrder` The event that is triggered before a new line item has been added to the order. @@ -262,7 +272,7 @@ Event::on( ); ``` -### `afterAddLineItemToOrder` +#### `afterAddLineItemToOrder` The event that is triggered after a line item has been added to an order. @@ -284,7 +294,7 @@ Event::on( } ); ``` -### `afterApplyAddLineItemToOrder` +#### `afterApplyAddLineItemToOrder` The event that is triggered after a line item has been added to an order. @@ -306,7 +316,7 @@ Event::on( ); ``` -### `afterRemoveLineItemToOrder` +#### `afterRemoveLineItemToOrder` The event that is triggered after a line item has been removed from an order. @@ -329,7 +339,7 @@ Event::on( ); ``` -### `afterApplyRemoveLineItemFromOrder` +#### `afterApplyRemoveLineItemFromOrder` The event that is triggered after a line item has been removed from an order. @@ -351,7 +361,7 @@ Event::on( ); ``` -### `beforeCompleteOrder` +#### `beforeCompleteOrder` The event that is triggered before an order is completed. @@ -370,7 +380,7 @@ Event::on( ); ``` -### `afterCompleteOrder` +#### `afterCompleteOrder` The event that is triggered after an order is completed. @@ -389,7 +399,7 @@ Event::on( ); ``` -### `afterOrderAuthorized` +#### `afterOrderAuthorized` This event is raised after an order is authorized in full and completed. @@ -410,7 +420,7 @@ Event::on( ); ``` -### `afterOrderPaid` +#### `afterOrderPaid` The event that is triggered after an order is paid and completed. @@ -429,43 +439,7 @@ Event::on( ); ``` -### `registerOrderAdjusters` - -The event that is triggered for registration of additional adjusters. - -```php -use craft\events\RegisterComponentTypesEvent; -use craft\commerce\services\OrderAdjustments; -use yii\base\Event; - -Event::on( - OrderAdjustments::class, - OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS, - function(RegisterComponentTypesEvent $event) { - $event->types[] = MyAdjuster::class; - } -); -``` - -### `registerDiscountAdjusters` - -The event that is triggered for registration of additional discount adjusters. - -```php -use craft\events\RegisterComponentTypesEvent; -use craft\commerce\services\OrderAdjustments; -use yii\base\Event; - -Event::on( - OrderAdjustments::class, - OrderAdjustments::EVENT_REGISTER_DISCOUNT_ADJUSTERS, - function(RegisterComponentTypesEvent $event) { - $event->types[] = MyAdjuster::class; - } -); -``` - -### `orderStatusChange` +#### `orderStatusChange` The event that is triggered when an order status is changed. @@ -491,7 +465,7 @@ Event::on( ); ``` -### `defaultOrderStatus` +#### `defaultOrderStatus` The event that is triggered when a default order status is being fetched. @@ -519,7 +493,25 @@ Event::on( ); ``` -### `modifyCartInfo` +#### `registerOrderAdjusters` + +The event that is triggered for registration of additional adjusters. + +```php +use craft\events\RegisterComponentTypesEvent; +use craft\commerce\services\OrderAdjustments; +use yii\base\Event; + +Event::on( + OrderAdjustments::class, + OrderAdjustments::EVENT_REGISTER_ORDER_ADJUSTERS, + function(RegisterComponentTypesEvent $event) { + $event->types[] = MyAdjuster::class; + } +); +``` + +#### `modifyCartInfo` The event that’s triggered when a cart is returned as an array for Ajax cart update requests. @@ -539,9 +531,9 @@ Event::on( ); ``` -## Discount Events +### Discount Events -### `afterDiscountAdjustmentsCreated` +#### `afterDiscountAdjustmentsCreated` The event that is triggered after a discount has matched the order and before it returns its adjustments. @@ -570,7 +562,7 @@ Event::on( ); ``` -### `beforeSaveDiscount` +#### `beforeSaveDiscount` The event that is triggered before a discount is saved. @@ -595,7 +587,7 @@ Event::on( ); ``` -### `afterSaveDiscount` +#### `afterSaveDiscount` The event that is triggered after a discount is saved. @@ -620,7 +612,7 @@ Event::on( ); ``` -### `afterDeleteDiscount` +#### `afterDeleteDiscount` The event that is triggered after a discount is deleted. @@ -643,7 +635,7 @@ Event::on( ); ``` -### `discountMatchesLineItem` +#### `discountMatchesLineItem` The event that is triggered when a line item is matched with a discount. @@ -673,7 +665,7 @@ Event::on( ); ``` -### `discountMatchesOrder` +#### `discountMatchesOrder` The event that is triggered when an order is matched with a discount. @@ -701,9 +693,9 @@ Event::on( ); ``` -## Line Item Events +### Line Item Events -### `beforeSaveLineItem` +#### `beforeSaveLineItem` The event that is triggered before a line item is saved. @@ -728,7 +720,7 @@ Event::on( ); ``` -### `afterSaveLineItem` +#### `afterSaveLineItem` The event that is triggeredd after a line item is saved. @@ -753,7 +745,7 @@ Event::on( ); ``` -### `populateLineItem` +#### `populateLineItem` The event that is triggered as a line item is being populated from a purchasable. @@ -784,7 +776,7 @@ Event::on( Don’t forget to set `salePrice` accordingly since it’s the amount that gets added to the cart. ::: -### `createLineItem` +#### `createLineItem` The event that is triggered after a line item has been created from a purchasable. @@ -809,7 +801,7 @@ Event::on( ); ``` -### `defaultLineItemStatus` +#### `defaultLineItemStatus` The event that is triggered when getting a default status for a line item. @@ -839,9 +831,9 @@ Event::on( ); ``` -## Payment Events +### Payment Events -### `registerGatewayTypes` +#### `registerGatewayTypes` The event that is triggered for the registration of additional gateways. @@ -861,7 +853,7 @@ Event::on( ); ``` -### `afterPaymentTransaction` +#### `afterPaymentTransaction` The event that is triggered after a payment transaction is made. @@ -885,7 +877,7 @@ Event::on( ); ``` -### `beforeCaptureTransaction` +#### `beforeCaptureTransaction` The event that is triggered before a payment transaction is captured. @@ -908,7 +900,7 @@ Event::on( ); ``` -### `afterCaptureTransaction` +#### `afterCaptureTransaction` The event that is triggered after a payment transaction is captured. @@ -931,7 +923,7 @@ Event::on( ); ``` -### `beforeRefundTransaction` +#### `beforeRefundTransaction` The event that is triggered before a transaction is refunded. @@ -953,7 +945,7 @@ Event::on( ); ``` -### `afterRefundTransaction` +#### `afterRefundTransaction` The event that is triggered after a transaction is refunded. @@ -975,7 +967,7 @@ Event::on( ); ``` -### `beforeProcessPaymentEvent` +#### `beforeProcessPaymentEvent` The event that is triggered before a payment is processed. @@ -1009,7 +1001,7 @@ Event::on( ); ``` -### `afterProcessPaymentEvent` +#### `afterProcessPaymentEvent` The event that is triggered after a payment is processed. @@ -1041,7 +1033,7 @@ Event::on( ); ``` -### `deletePaymentSource` +#### `deletePaymentSource` The event that is triggered when a payment source is deleted. @@ -1064,7 +1056,7 @@ Event::on( ); ``` -### `beforeSavePaymentSource` +#### `beforeSavePaymentSource` The event that is triggered before a payment source is added. @@ -1086,7 +1078,7 @@ Event::on( ); ``` -### `afterSavePaymentSource` +#### `afterSavePaymentSource` The event that is triggered after a payment source is added. @@ -1109,7 +1101,7 @@ Event::on( ); ``` -### `afterSaveTransaction` +#### `afterSaveTransaction` The event that is triggered after a transaction has been saved. @@ -1132,7 +1124,7 @@ Event::on( ); ``` -### `afterCreateTransaction` +#### `afterCreateTransaction` The event that is triggered after a transaction has been created. @@ -1155,9 +1147,9 @@ Event::on( ); ``` -## Subscription Events +### Subscription Events -### `afterExpireSubscription` +#### `afterExpireSubscription` The event that is triggered after a subscription has expired. @@ -1180,7 +1172,7 @@ Event::on( ); ``` -### `beforeCreateSubscription` +#### `beforeCreateSubscription` The event that is triggered before a subscription is created. @@ -1211,7 +1203,7 @@ Event::on( ); ``` -### `afterCreateSubscription` +#### `afterCreateSubscription` The event that is triggered after a subscription is created. @@ -1243,7 +1235,7 @@ Event::on( Since a subscription may be suspended at creation due to payment issues, you may want to check subscription properties like `hasStarted` or `isSuspended` before taking further action. ::: -### `beforeReactivateSubscription` +#### `beforeReactivateSubscription` The event that is triggered before a subscription gets reactivated. @@ -1268,7 +1260,7 @@ Event::on( ); ``` -### `afterReactivateSubscription` +#### `afterReactivateSubscription` The event that is triggered after a subscription gets reactivated. @@ -1291,7 +1283,7 @@ Event::on( ); ``` -### `beforeSwitchSubscriptionPlan` +#### `beforeSwitchSubscriptionPlan` The event that is triggered before a subscription is switched to a different plan. @@ -1324,7 +1316,7 @@ Event::on( ); ``` -### `afterSwitchSubscriptionPlan` +#### `afterSwitchSubscriptionPlan` The event that is triggered after a subscription gets switched to a different plan. @@ -1355,7 +1347,7 @@ Event::on( ); ``` -### `beforeCancelSubscription` +#### `beforeCancelSubscription` The event that is triggered before a subscription is canceled. @@ -1383,7 +1375,7 @@ Event::on( ); ``` -### `afterCancelSubscription` +#### `afterCancelSubscription` The event that is triggered after a subscription gets canceled. @@ -1409,7 +1401,7 @@ Event::on( ); ``` -### `beforeUpdateSubscription` +#### `beforeUpdateSubscription` The event that is triggered before a subscription gets updated. Typically this event is fired when subscription data is updated on the gateway. @@ -1431,7 +1423,7 @@ Event::on( ); ``` -### `receiveSubscriptionPayment` +#### `receiveSubscriptionPayment` The event that is triggered when a subscription payment is received. @@ -1460,9 +1452,9 @@ Event::on( ); ``` -## Other Events +### Mail Events -### `beforeSendEmail` +#### `beforeSendEmail` The event that is triggered before an email is sent out. @@ -1497,7 +1489,7 @@ Event::on( ); ``` -### `afterSendEmail` +#### `afterSendEmail` The event that is triggered after an email has been sent out. @@ -1529,7 +1521,7 @@ Event::on( ); ``` -### `beforeSaveEmail` +#### `beforeSaveEmail` The event that is triggered before an email is saved. @@ -1553,7 +1545,7 @@ Event::on( ); ``` -### `afterSaveEmail` +#### `afterSaveEmail` The event that is triggered after an email is saved. @@ -1577,7 +1569,7 @@ Event::on( ); ``` -### `beforeDeleteEmail` +#### `beforeDeleteEmail` The event that is triggered before an email is deleted. @@ -1599,7 +1591,7 @@ Event::on( ); ``` -### `afterDeleteEmail` +#### `afterDeleteEmail` The event that is triggered after an email is deleted. @@ -1621,7 +1613,9 @@ Event::on( ); ``` -### `beforeRenderPdf` +### PDF Events + +#### `beforeRenderPdf` The event that is triggered before an order’s PDF is rendered. @@ -1636,14 +1630,14 @@ Event handlers can customize PDF rendering by modifying several properties on th | `pdf` | `null` by default, can optionally be used to supply your own PDF string rather than the one Commerce would generate | ```php -use craft\commerce\events\PdfEvent; +use craft\commerce\events\PdfRenderEvent; use craft\commerce\services\Pdfs; use yii\base\Event; Event::on( Pdfs::class, Pdfs::EVENT_BEFORE_RENDER_PDF, - function(PdfEvent $event) { + function(PdfRenderEvent $event) { // Modify `$event->order`, `$event->option`, `$event->template`, // and `$event->variables` to customize what gets rendered into a PDF; // or render your own PDF and set its string on `$event->pdf` @@ -1656,32 +1650,32 @@ Event::on( If you provide your own PDF string, Commerce will skip its own rendering and [`afterRenderPdf`](#afterrenderpdf) will not be triggered. ::: -### `afterRenderPdf` +#### `afterRenderPdf` The event that is triggered after an order’s PDF has been rendered by Commerce. Event handlers can override Commerce’s PDF generation by setting the `pdf` property on the event to a custom-rendered PDF string. The event properties will be the same as those from `beforeRenderPdf`, but `pdf` will contain a rendered PDF string and is the only one for which setting a value will make any difference for the resulting PDF output. ```php -use craft\commerce\events\PdfEvent; +use craft\commerce\events\PdfRenderEvent; use craft\commerce\services\Pdfs; use yii\base\Event; Event::on( Pdfs::class, Pdfs::EVENT_AFTER_RENDER_PDF, - function(PdfEvent $event) { + function(PdfRenderEvent $event) { // Add a watermark to the PDF or forward it to the accounting department // ... } ); ``` -### `beforeSavePdf` +#### `beforeSavePdf` -The event that is triggered before a PDF is saved. +The event that is triggered before a PDF’s configuration is saved. ```php -use craft\commerce\events\PdfSaveEvent; +use craft\commerce\events\PdfEvent; use craft\commerce\services\Pdfs; use craft\commerce\models\Pdf; use yii\base\Event; @@ -1689,7 +1683,7 @@ use yii\base\Event; Event::on( Pdfs::class, Pdfs::EVENT_BEFORE_SAVE_PDF, - function(PdfSaveEvent $event) { + function(PdfEvent $event) { // @var Pdf $pdf $pdf = $event->pdf; // @var bool $isNew @@ -1699,12 +1693,12 @@ Event::on( ); ``` -### `afterSavePdf` +#### `afterSavePdf` -The event that is triggered after a PDF is saved. +The event that is triggered after a PDF’s configuration is saved. ```php -use craft\commerce\events\PdfSaveEvent; +use craft\commerce\events\PdfEvent; use craft\commerce\services\Pdfs; use craft\commerce\models\Pdf; use yii\base\Event; @@ -1712,7 +1706,7 @@ use yii\base\Event; Event::on( Pdfs::class, Pdfs::EVENT_AFTER_SAVE_PDF, - function(PdfSaveEvent $event) { + function(PdfEvent $event) { // @var Pdf $pdf $pdf = $event->pdf; // @var bool $isNew @@ -1722,7 +1716,9 @@ Event::on( ); ``` -### `beforeSaveProductType` +### Product Types + +#### `beforeSaveProductType` The event that is triggered before a product type is saved. @@ -1745,7 +1741,7 @@ Event::on( ); ``` -### `afterSaveProductType` +#### `afterSaveProductType` The event that is triggered after a product type has been saved. @@ -1768,7 +1764,9 @@ Event::on( ); ``` -### `registerPurchasableElementTypes` +### Purchasables + +#### `registerPurchasableElementTypes` The event that is triggered for registration of additional purchasables. @@ -1788,27 +1786,7 @@ Event::on( ); ``` -### `registerAvailableShippingMethods` - -The event that is triggered for registration of additional shipping methods. - -This example adds an instance of `MyShippingMethod` to the event object’s `shippingMethods` array: - -```php -use craft\commerce\events\RegisterAvailableShippingMethodsEvent; -use craft\commerce\services\ShippingMethods; -use yii\base\Event; - -Event::on( - ShippingMethods::class, - ShippingMethods::EVENT_REGISTER_AVAILABLE_SHIPPING_METHODS, - function(RegisterAvailableShippingMethodsEvent $event) { - $event->shippingMethods[] = MyShippingMethod::class; - } -); -``` - -### `purchasableAvailable` +#### `purchasableAvailable` The event that’s triggered when determining whether a purchasable should be available for a given current user and cart. @@ -1832,7 +1810,27 @@ Event::on( If the purchasable becomes unavailable after being added to the cart, an [order notice](../orders-carts.md#order-notices) will be added to the order informing the customer. ::: -### `purchasableShippable` +#### `modifyPurchasablesTableQuery` + +The event that is triggered retrieving the list of purchasables for the add a line item feature on the order edit page. + +This example adds a condition to the query to only return purchasables with a SKU that contains `foo` + +```php +use craft\commerce\controllers\OrdersController; +use craft\commerce\events\ModifyPurchasablesTableQueryEvent; + +Event::on( + OrdersController::class, + OrdersController::EVENT_MODIFY_PURCHASABLES_TABLE_QUERY, + function(ModifyPurchasablesTableQueryEvent $event) { + $likeOperator = Craft::$app->getDb()->getIsPgsql() ? 'ILIKE' : 'LIKE'; + $event->query->andWhere([$likeOperator, 'sku', 'foo']); + } +); +``` + +#### `purchasableShippable` The event that is triggered when determining whether a purchasable may be shipped. @@ -1855,22 +1853,24 @@ Event::on( ); ``` -### `modifyPurchasablesTableQuery` +### Shipping -The event that is triggered retrieving the list of purchasables for the add a line item feature on the order edit page. +#### `registerAvailableShippingMethods` -This example adds a condition to the query to only return purchasables with a SKU that contains `foo` +The event that is triggered for registration of additional shipping methods. + +This example adds an instance of `MyShippingMethod` to the event object’s `shippingMethods` array: ```php -use craft\commerce\controllers\OrdersController; -use craft\commerce\events\ModifyPurchasablesTableQueryEvent; +use craft\commerce\events\RegisterAvailableShippingMethodsEvent; +use craft\commerce\services\ShippingMethods; +use yii\base\Event; Event::on( - OrdersController::class, - OrdersController::EVENT_MODIFY_PURCHASABLES_TABLE_QUERY, - function(ModifyPurchasablesTableQueryEvent $event) { - $likeOperator = Craft::$app->getDb()->getIsPgsql() ? 'ILIKE' : 'LIKE'; - $event->query->andWhere([$likeOperator, 'sku', 'foo']); + ShippingMethods::class, + ShippingMethods::EVENT_REGISTER_AVAILABLE_SHIPPING_METHODS, + function(RegisterAvailableShippingMethodsEvent $event) { + $event->shippingMethods[] = MyShippingMethod::class; } ); ```