Setting Up Electronic Certificate Sales in 1C-Bitrix
Gift certificates in an online store on 1C-Bitrix are not just a product with a nice picture. Behind them lies unique code generation, PDF formation, automatic delivery to the buyer, and an activation mechanism through the coupon system of the sale module. Let's cover the implementation from product creation to partial balance redemption.
Certificate as a Catalog Product
An electronic certificate is created as a regular element of a catalog infoblock, but with specific features:
- Product type — simple product without inventory tracking (quantity is unlimited, the "Quantitative accounting" checkbox is unchecked)
- Trade offers — create SKUs by denominations: 1000, 2000, 5000 rubles. Each offer has its own price equal to the denomination
- "Delivery type" property — set the value to "Electronic delivery" so the address is not requested during checkout
In the product card, add an infoblock property "Certificate Denomination" (type — number). This will be needed by the processor when generating the coupon.
Unique Code Generation
Upon successful order payment, the OnSalePayOrder event handler generates a unique certificate code. The code is formed via Bitrix\Main\Security\Random::getString(12) — 12 characters from the set A-Z, 0-9.
Handler logic:
- Check that the order contains a product from the "Certificates" section (by section ID of the infoblock)
- Generate the code
- Create a coupon via the API
Bitrix\Sale\Internals\DiscountCouponTable::add()with binding to a cart rule - Save the code and denomination in order properties (custom fields
UF_CERTIFICATE_CODE,UF_CERTIFICATE_AMOUNT)
The coupon is created with the type "For a series of orders" and maximum usage count — this ensures partial usage.
PDF Formation
To generate a PDF certificate, use the mPDF or Dompdf library connected via Composer. The certificate template is an HTML file with variable substitution:
- Certificate code
- Denomination
- Issue date and expiration date
- QR code for quick activation (generated via
chillerlan/php-qrcodeor similar)
The HTML template is stored in /local/templates/certificate/ and contains inline styles (mPDF doesn't fully support external CSS). The background image is set via CSS background-image with an absolute path to the file on the server.
The result is saved in /upload/certificates/ with a name based on the certificate code. Access to the directory is closed via .htaccess — the file is served only through a PHP script with authorization verification.
Email Delivery
The mail event CERTIFICATE_CREATED is created in the Settings → Mail events section. The letter template contains:
| Macro | Value |
|---|---|
#CODE# |
Certificate code |
#AMOUNT# |
Denomination |
#VALID_UNTIL# |
Expiration date |
#PDF_LINK# |
Link to download PDF |
Sending is initiated from the same OnSalePayOrder handler after PDF generation. The letter is sent to the buyer's email from the order. If a separate recipient email is specified in the order (the "Certificate recipient email" field) — the letter is also sent there.
Activation and Cart Rules
The certificate redemption mechanism works through the sale.discount module. When creating a coupon, a cart rule is created:
- Discount type — fixed amount equal to the certificate denomination
- Binding — a coupon whose code matches the certificate code
- Scope of application — the entire order (or specific catalog sections if the certificate is category-specific)
The buyer enters the certificate code in the "Coupon" field during checkout. The system applies a discount for the denomination amount.
Partial Usage and Balance
The standard Bitrix coupon system doesn't support coupon balance out of the box. For partial redemption, additional development is required:
- The
OnSaleOrderPaidhandler checks whether the certificate coupon was used in the paid order - If the order amount is less than the denomination — calculates the remaining balance
- Updates the cart rule: new discount amount = remaining balance
- Records usage history in a separate table (custom table
b_certificate_usage)
If the order amount is greater than or equal to the denomination — the coupon is deactivated via DiscountCouponTable::update() with the ACTIVE field = N.
The certificate balance is displayed in the buyer's personal account through a custom component that reads data from b_certificate_usage and the current cart rule.
Expiration Date
The period is set in the cart rule via the ACTIVE_FROM and ACTIVE_TO fields. The standard period is 12 months from the purchase date. After the period expires, the coupon automatically stops applying, and the certificate is displayed in the personal account as "Expired".
A working implementation of certificates will require creating a module or at least a set of handlers in init.php, a custom table for history, and a PDF template. The scope of work is 20–30 hours depending on design requirements and business logic.







