SyliusCon 2025
Early Bird Deal
LogoLogo
πŸ›£οΈ RoadmapπŸ’» Sylius DemoπŸ’¬ Community Slack
  • Sylius Documentation
  • Sylius Plugins
  • Sylius Stack
  • πŸ“–Sylius 2.0 Documentation
    • Organization
      • Release Cycle
      • Backwards Compatibility Promise
      • Sylius Team
      • Sylius Roadmap
  • Getting Started with Sylius
    • Installation
    • Basic Configuration
    • Shipping & Payment
    • First Product
    • Customizing the Shop
    • Customizing Business Logic
    • Using API
    • Installing Plugins
    • Deployment
    • Summary
  • The Book
    • Introduction to Sylius
    • Installation
      • System Requirements
      • Sylius CE Installation
        • Sylius CE Installation with Docker
      • βž•Sylius Plus Installation
      • Upgrading Sylius CE
      • Upgrading Sylius Plus
    • Architecture
      • Architecture Overview
      • Architectural Drivers
      • Resource Layer
      • State Machine
      • Translations
      • E-Mails
      • Contact
      • Fixtures
      • Events
    • Configuration
      • Channels
      • Locales
      • Currencies
    • Customers
      • Customer & ShopUser
      • βž•Customer Pools
      • AdminUser
      • Addresses
        • Countries
        • Zones
        • Addresses
        • Address Book
    • Products
      • Products
      • Product Reviews
      • Product Associations
      • Attributes
      • Pricing
      • Catalog Promotions
      • Taxons
      • Inventory
      • βž•Multi-Source Inventory
      • Search
    • Carts & Orders
      • Orders
      • Cart flow
      • Taxation
      • Adjustments
      • Cart Promotions
      • Coupons
      • Payments
      • 🧩Invoices
      • Shipments
    • Support
    • Contributing
      • Contributing Code
        • Submitting a Patch
        • ⚠️Security Issues
        • Coding Standards
        • Conventions
        • Sylius License and Trademark
      • Contributing Translations
      • Key Contributors
  • The Customization Guide
    • Customizing Models
      • How to add a custom model?
      • How to add a custom translatable model?
    • Customizing Forms
      • How to add a live form for a custom model?
    • Customizing Styles
    • Customizing Validation
    • Customizing Menus
    • Customizing Templates
    • Customizing Translations
    • Customizing Flashes
    • Customizing State Machines
    • Customizing Grids
    • Customizing Fixtures
    • Customizing API
    • Customizing Serialization of API
    • Customizing Payments
      • How to integrate a Payment Gateway as a Plugin?
  • πŸ§‘β€πŸ³The Cookbook
  • How to resize images?
  • How to add one image to an entity?
  • How to add multiple images to an entity?
  • Sylius 1.X Documentation
    • πŸ““Sylius 1.x Documentation
Powered by GitBook
LogoLogo

Developer

  • Community
  • Online Course

About

  • Team

Β© 2025 Sylius. All Rights Reserved

On this page
  • Coupon Parameters
  • Creating a Coupon-Based Promotion
  • Applying a Coupon to an Order
  • Generating Multiple Coupons

Was this helpful?

Edit on GitHub
  1. The Book
  2. Carts & Orders

Coupons

Coupons are tightly integrated with Sylius Cart Promotions, allowing promotions to be activated by unique codes. Here’s how to create, apply, and generate coupons for promotions.

Coupon Parameters

Each coupon has the following attributes:

  • Code: The unique identifier for the coupon.

  • Expiration Date: The date when the coupon expires.

  • Usage Limit: The maximum number of times it can be used.

  • Usage Count: Tracks how many times the coupon has been used.

Creating a Coupon-Based Promotion

To create a coupon-based promotion, follow these steps:

  1. Create a Promotion

    Begin by creating a new promotion and setting it as coupon-based. Only coupon-based promotions can hold multiple coupons.

    /** @var PromotionInterface $promotion */
    $promotion = $this->container->get('sylius.factory.promotion')->createNew();
    
    $promotion->setCode('free_shipping');
    $promotion->setName('Free Shipping');
    
    // Set the promotion's channel
    $promotion->addChannel($this->container->get('sylius.repository.channel')->findOneBy(['code' => 'US_Web_Store']));
    
    $promotion->setCouponBased(true);
  2. Create a Coupon and Link It to the Promotion

    Next, create a coupon and associate it with your promotion:

    /** @var CouponInterface $coupon */
    $coupon = $this->container->get('sylius.factory.promotion_coupon')->createNew();
    
    $coupon->setCode('FREESHIPPING');
    
    $promotion->addCoupon($coupon);
  3. Add a Promotion Action

    Define what action the promotion should take when applied. For a free shipping promotion, create a 100% shipping discount action:

    /** @var PromotionActionFactoryInterface $actionFactory */
    $actionFactory = $this->container->get('sylius.factory.promotion_action');
    
    // Use a float for percentage discounts (1 = 100%, 0.1 = 10%)
    $action = $actionFactory->createShippingPercentageDiscount(1);
    
    $promotion->addAction($action);
    
    // Save the promotion to the repository
    $this->container->get('sylius.repository.promotion')->add($promotion);

Applying a Coupon to an Order

To apply a promotion coupon we've just created to an order:

  1. Ensure the order has shipments (as the above coupon applies a promotion on shipping).

  2. Set the promotion coupon on the order (this simulates a customer applying the coupon code at checkout).

  3. Process the order with the OrderProcessor to apply the promotion.

$order->setPromotionCoupon($coupon);
$this->container->get('sylius.order_processing.order_processor')->process($order);

Generating Multiple Coupons

For larger promotions, manually creating codes is tedious. Sylius offers the CouponGenerator service to automatically generate coupon codes in bulk.

  1. Retrieve the Promotion

    First, find the promotion to which you want to add coupons.

    $promotion = $this->container->get('sylius.repository.promotion')->findOneBy(['code' => 'simple_promotion']);
  2. Configure the Coupon Generator

    Use PromotionCouponGeneratorInstruction to specify the number of coupons, code length, expiration date, and usage limit.

    /** @var CouponGeneratorInterface $generator */
    $generator = $this->container->get('sylius.promotion_coupon_generator');
    
    /** @var PromotionCouponGeneratorInstructionInterface $instruction */
    $instruction = new PromotionCouponGeneratorInstruction();
    
    $instruction->setAmount(10); // Generate 10 coupons
    $instruction->setPrefix('NEW_YEAR_'); // Optional prefix
    $instruction->setSuffix('_SALE'); // Optional suffix
    
    $generator->generate($promotion, $instruction);

    This example generates 10 coupons with the prefix NEW_YEAR_ and suffix _SALE for the simple_promotion promotion.

PreviousCart PromotionsNextPayments

Last updated 7 months ago

Was this helpful?