# Filters and hooks - Discountline 1.x documentation

> Register your own rule types and conditions, add fields to the rule editor, and change what Discountline shows.

Discountline's rule types and conditions are registered, not hard-coded, so a plugin can add its own. Discountline Pro is built entirely on the hooks below.

## Loading after Discountline

```php
add_action( 'discountline_loaded', function () {
	// Discountline's classes are available here.
} );
```

`discountline_loaded` fires on `plugins_loaded`, only when WooCommerce is active. `DISCOUNTLINE_VERSION`, `DISCOUNTLINE_FILE` and `DISCOUNTLINE_URL` are defined.

## Rule types

```php
add_filter( 'discountline_rule_types', function ( $types ) {
	$types['half'] = array(
		'label'       => 'Half price',
		'description' => 'Every matching item at half price.',
		'icon'        => 'dashicons-star-half',
		'level'       => 'item',  // 'item', 'order' or '' (you apply it yourself)
		'badge'       => true,
	);
	return $types;
} );

add_filter( 'discountline_unit_price', function ( $price, $rule, $base, $qty ) {
	return 'half' === $rule->type() ? $base / 2 : $price;
}, 10, 4 );
```

- **Item-level** types give a price per item through `discountline_unit_price` (`null`: no change). The best price wins, as for Discountline's own types.
- **Order-level** types return what they save on the cart's items from `discountline_order_saving` (`$saving, $rule, $items`). Discountline applies it as an automatic coupon for that amount, by the rule's name; the best rule of each order-level type applies.
- `discountline_rule_summary` (`$text, $rule`) gives the one-line summary in the rule list, which is also the default badge.
- `discountline_sanitize_rule` (`$data, $raw`) cleans your type's own settings, stored in the rule's `_discountline_rule` meta.

## Conditions

```php
add_filter( 'discountline_conditions', function ( $conditions ) {
	$conditions['weekday'] = array(
		'default'  => '',                 // "off": must be empty or 'any'
		'sanitize' => 'sanitize_key',
		'check'    => function ( $day, $rule, $ctx ) {
			return strtolower( wp_date( 'D', $ctx->now ) ) === $day ? null : 'Not today.';
		},
		'cart'     => false,              // true: only judged with a cart
	);
	return $conditions;
} );
```

Conditions are checked in array order; the first one that returns a reason stops the rule, and the reason is the text a store owner sees. `section` can be `'limits'` for usage limits. The value lives in the rule's settings under `conditions` (or `limits`).

A rule whose type, or one of whose conditions, no active plugin registers is kept as it is but never applies, and is listed as needing Discountline Pro. That's why a condition's "off" value must be empty or `'any'`: it lets Discountline tell an unused condition from one in use.

## The rule editor

| Hook | Use |
|---|---|
| `discountline_editor_sections` (filter) | Boxes of the editor, id => title. Your box is a form table filled by the next hook. |
| `discountline_editor_rows` (action) | `$section, $rule`: print `<tr>` rows at the end of a box. Rows only for some types: `<tr class="discountline-only" data-for="half">`. Name fields `discountline[...]`. |
| `discountline_summary_box` (action) | `$post`: markup after the Summary box's contents. |
| `discountline_rule_status` (filter) | `$html, $rule`: a status pill for the list instead of "Active"; use `RuleEditor::pill( $text, 'later' )`. |
| `discountline_settings` (filter) | The fields of the Discount rules settings section. |
| `discountline_show_tier_table` (filter) | `$show, $product`: whether a product page shows its quantity tier table. Return false to leave it out, e.g. for shoppers who can't see prices. |

The Summary box is written by `rule-editor.js`; add to it on the `discountline-summary` jQuery event on `document`. It gets one object, `{ type, lines, rows, notes, helpers }`: push lines for your type, notes for your conditions, or splice rows.

## Styling

| Class | Element |
|---|---|
| `discountline-badge` | The badge after a price |
| `discountline-tiers` | The price table; `tr.is-current` is the tier for the chosen quantity |
| `discountline-saving` | "You save …" under the table |
| `discountline-automatic` | Automatic discounts under the Discount row in the Cart and Checkout blocks |

Source: https://extendiment.com/plugins/discountline/docs/1.x/filters-and-hooks
