Events Reference

To learn more about how events work, see the Craft documentation on events.

commerce_discounts.onBeforeMatchLineItem #

Raised when matching the discount to each line item.

This event will not trigger if the lineItems’ purchasable is not promotable or is excluded from sale.

Params:

  • lineItem – The Line Item Model for the currently tested line item.
  • discount – The Commerce_DiscountModel for the currently tested discount.

craft()->on('commerce_discounts.onBeforeMatchLineItem', function($event){
    if ($lineItem->purchasable instanceof Commerce_VariantModel){
            if ($lineItem->qty < 3){
                $event->performAction = false;
            }
    }
});

commerce_sales.onBeforeMatchProductAndSale #

This event will trigger if the product matches the sale. Allowing you to stop the match.

Params:

  • product – The Product Model that matches the sale.
  • sale – The Commerce_SaleModel that matches the product.
craft()->on('commerce_sales.onBeforeMatchProductAndSale', function($event){
    $cart = craft()->commerce_cart->getCart();
    if($cart->isInStoreOrder)
    {
        $event->performAction = false;
    }
});

commerce_payments.onBeforeGatewayRequestSend #

Raised before an order is saved.

Params:

  • type – The transaction type authorize,purchase,refund,capture
  • request – The Omnipay Request object (Omnipay\Common\Message\AbstractRequest)
  • transaction – The transaction for this request. (see Transaction Model)
craft()->on('commerce_payments.onBeforeGatewayRequestSend', function($event){
    $transaction = $event->params['transaction'];
    $transaction->message = "I DENIED IT";
    $event->performAction = false;
});

Event handlers can prevent the gateway request from happening by setting $event->performAction to false. If you want to give a clear reason, set a message on the transaction model.

commerce_lineItems.onPopulateLineItem #

This event is raised when a line item is being populated or repopulated from a purchasable

Params:

  • lineItem – the Line Item Model that is being populated by the line item.
  • purchasable – the Purchasable that belongs to the line item.
craft()->on('commerce_lineItems.onPopulateLineItem', function($event){

    $purchasable= $event->params['purchasable'];
    $lineItem = $event->params['lineItem'];

    if($purchasable->getPurchasableId() == 1461){
        $lineItem->price = 3;
        $lineItem->saleAmount = 0;
    }

});

OR

craft()->on('commerce_lineItems.onPopulateLineItem', function($event){

    $purchasable= $event->params['purchasable'];
    $lineItem = $event->params['lineItem'];

    if($lineItem->options['giftWrapped']){
        $lineItem->price = $lineItem->price + 1;
        $lineItem->saleAmount = 0;
    }

});

OR

craft()->on('commerce_lineItems.onPopulateLineItem', function($event)
{
    $lineItem = $event->params['lineItem'];

    $options = $lineItem->options;

    if (isset($options['customWidth']) && $options['customWidth'])
    {

        $lineItem->price = $lineItem->price * (int) $options['customWidth'];
        $lineItem->saleAmount = 0;  

    }

});

commerce_lineItems.onCreateLineItem #

This event is raised when a new line item is created from a purchasable

Params:

  • lineItem – the Line Item Model as it has been created and before it has been added to the cart.
craft()->on('commerce_lineItems.onCreateLineItem',
    [
        'MyClass',
        'onCreateLineItemHandlerMethod'
    ]
);

commerce_lineItems.onBeforeSaveLineItem #

Raised before a Line Item is saved.

Params:

  • lineItem – the Line Item Model before being saved.
  • isNewLineItem - true/false if the Line Item is new.
craft()->on('commerce_lineItems.onBeforeSaveLineItem',
    [
        'MyClass',
        'beforeSaveHandlerMethod'
    ]
);

commerce_lineItems.onSaveLineItem #

Raised when a Line Item is saved.

Params:

  • lineItem – the Line Item Model being saved.
  • isNewProduct - true/false if the Line Item is new.
craft()->on('commerce_lineItems.onSaveLineItem',
    [
        'MyClass',
        'saveHandlerMethod'
    ]
);

commerce_products.onBeforeSaveProduct #

Raised before a Product is saved.

Params:

  • product – the Commerce_ProductModel before being saved.
  • isNewProduct - true/false if the product is new.
craft()->on('commerce_products.onBeforeSaveProduct',
    [
        'MyClass',
        'beforeSaveHandlerMethod'
    ]
);

commerce_products.onSaveProduct #

Raised after a Product is saved.

Params:

  • product – the Product Model before being saved.
  • isNewProduct - true/false if the product is new.
craft()->on('commerce_products.onSaveProduct',
    [
        'MyClass',
        'saveHandlerMethod'
    ]
);

commerce_products.onBeforeDeleteProduct #

Raised before a Product is deleted.

Params:

  • product – the Commerce_ProductModel before being deleted.
craft()->on('commerce_products.onBeforeDeleteProduct',
    [
        'MyClass',
        'beforeDeleteHandlerMethod'
    ]
);

You can prevent the product from getting deleted by setting $event->performAction to false.

commerce_products.onBeforeEditProduct #

Raised after a product model has been loaded before editing.

Params:

craft()->on('commerce_products.onBeforeEditProduct',
    [
        'MyClass',
        'saveHandlerMethod'
    ]
);

commerce_products.onDeleteProduct #

Raised after a Product is deleted.

Params:

craft()->on('commerce_products.onDeleteProduct',
    [
        'MyClass',
        'saveHandlerMethod'
    ]
);

commerce_orders.onBeforeSaveOrder #

Raised before an order is saved.

Params:

craft()->on('commerce_orders.onBeforeSaveOrder',
    [
        'MyClass',
        'orderBeforeSaveHandlerMethod'
    ]
);

Event handlers can prevent the order from getting saved by setting $event->performAction to false.

commerce_orders.onSaveOrder #

Raised after an order is saved.

Params:

craft()->on('commerce_orders.onSaveOrder',
    [
        'MyClass',
        'orderSaveHandlerMethod'
    ]
);

commerce_orders.onBeforeOrderComplete #

Raised before a cart is completed and becomes an order.

Params:

  • order – the cart Order Model that is about to be marked as complete and become an order.
craft()->on('commerce_orders.onBeforeOrderComplete',
    [
        'MyClass',
        'orderCompleteHandlerMethod'
    ]
);

commerce_orders.onOrderComplete #

Raised when a cart has been turned into an order.

Params:

  • order – the Order Model of the cart, which is now a completed order.
craft()->on('commerce_orders.onOrderComplete',
    [
        'MyClass',
        'orderCompleteHandlerMethod'
    ]
);

commerce_orderHistories.onStatusChange #

Raised after an an order status on an order is changed.

Params:

craft()->on('commerce_orderHistories.onStatusChange',
    [
        'MyClass',
        'orderStatusChangeHandlerMethod'
    ]
);

commerce_cart.onBeforeAddToCart #

Raised before an item has been saved to the cart for the first time.

Params:


craft()->on('commerce_cart.onAddToCart',
    [
        'MyClass',
        'beforAddToCartHandlerMethod'
    ]
);

Event handlers can prevent the add to cart by setting $event->performAction to false.

commerce_cart.onAddToCart #

Raised after an item has been added to the cart for the first time.

Params:

  • cart – the Order Model after the item was added to the cart.
  • lineItem – the Line Item Model after being saved to the cart.
craft()->on('commerce_cart.onAddToCart',
    [
        'MyClass',
        'addToCartHandlerMethod'
    ]
);

commerce_cart.onRemoveFromCart #

Raised after a lineItem has been removed from the cart

Params:

  • cart – the Order Model after the lineItem was removed.
  • lineItemId – the line item Id (int) that was removed.
craft()->on('commerce_cart.onRemoveFromCart',
    [
        'MyClass',
        'removeFromCartHandlerMethod'
    ]
);

commerce_transactions.onSaveTransaction #

Raised after a transaction has been saved.

Params:

craft()->on('commerce_transactions.onSaveTransaction',
    [
        'MyClass',
        'savedTranactionHandlerMethod'
    ]
);

commerce_variants.onOrderVariant #

Raised after stock has been deducted from a variant.

Params:

  • variant – the Variant Model that was ordered. Called once per variant on a completed order.
craft()->on('commerce_variants.onOrderVariant', function($event){
    $variant = $event->params['variant'];
      if ($variant->stock < 5){
            Craft::log('Stock Low');
        }
});

commerce_addresses.onBeforeSaveAddress #

Raised before address has been saved.

Params:

craft()->on('commerce_addresses.onBeforeSaveAddress', function($event){
    $address = $event->params['address'];
    $address->addError('firstName','Not a cool name man.');
    $event->performAction = false;
});

Event handlers can prevent the address from getting replaced by setting $event->performAction to false.

commerce_addresses.onSaveAddress #

Raised before address has been saved.

Params:

craft()->on('commerce_addresses.onSaveAddress',
    [
        'MyClass',
        'saveAddressHandlerMethod'
    ]
);

Buy Craft Commerce

Craft Commerce site license

Includes

  • Standard tech support
  • Unlimited app updates
  • Craft Pro site license
Buy now for $999
Can I try before buying?

Absolutely. Just download it from the “Download” button in the site header and follow our installation instructions. You’ll have the full feature set to experiment with, with just one limitation: you won’t be able to charge credit cards. Until Craft Commerce has been purchased and activated, it’s limited to a single dummy gateway.

Will there be recurring/upgrade fees?

Nope, it’s just a one-time license fee per site.

Can I use this on an existing Craft site?

Yes! And if you already have a Craft Pro license (or you don’t need one), just flip that “Include a Craft Pro license” switch in the purchase form, and we’ll drop the price to $700.

Is Craft Pro required?

Craft Commerce does not require Craft Pro; it works perfectly fine on Craft Client and Personal editions as well!

Can I try before buying?

Absolutely. Just download it from the “Download” button in the site header and follow our installation instructions. You’ll have the full feature set to experiment with, with just one limitation: you won’t be able to charge credit cards. Until Craft Commerce has been purchased and activated, it’s limited to a single dummy gateway.

Will there be recurring/upgrade fees?

Nope, it’s just a one-time license fee per site.

Can I use this on an existing Craft site?

Yes! And if you already have a Craft Pro license (or you don’t need one), just flip that “Include a Craft Pro license” switch in the purchase form, and we’ll drop the price to $700.

Is Craft Pro required?

Craft Commerce does not require Craft Pro; it works perfectly fine on Craft Client and Personal editions as well!