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
  • Association Types
  • How to create a new Association Type?
  • How to add a new Association to a Product?

Was this helpful?

Edit on GitHub
  1. The Book
  2. Products

Product Associations

Associations of products can be used as a marketing tool for suggesting to your customers, what products to buy together with the one they are currently considering. Associations can increase your shop’s efficiency. You choose what strategy you prefer. They are fully configurable.

Association Types

The type of association can be different. If you sell food you can suggest inspiring ingredients, if you sell products for automotive you can suggest buying some tools that may be useful for a home car mechanic. Exemplary association types can be: up-sell, cross-sell, accessories, alternatives and whatever you imagine.

How to create a new Association Type?

Create a new Association Type using a dedicated factory. Give the association a code and a name to easily recognize the type.

/** @var ProductAssociationTypeInterface $associationType */
$associationType = $this->container->get('sylius.factory.product_association_type')->createNew();

$associationType->setCode('accessories');
$associationType->setName('Accessories');

To have the new association type in the system add it to the repository.

$this->container->get('sylius.repository.product_association_type')->add($associationType);

How to add a new Association to a Product?

Find in your system a product to which you would like to add an association. We will use a Go Pro camera as an example.

$product = $this->container->get('sylius.repository.product')->findOneBy(['code' => 'go-pro-camera']);

Next, create a new Association which will connect our camera with its accessories. Such an association needs the AssociationType we have created in the previous step above.

/** @var ProductAssociationInterface $association */
$association = $this->container->get('sylius.factory.product_association')->createNew();

/** @var ProductAssociationTypeInterface $associationType */
$associationType = $this->container->get('sylius.repository.product_association_type')->findOneBy(['code' => 'accessories']);

$association->setType($associationType);

Let’s add all products from a certain taxon to the association we have created. To do that find a desired taxon by code and get all its products. Perfect accessories for a camera will be SD cards.

/** @var TaxonInterface $taxon */
$taxon = $this->container->get('sylius.repository.taxon')->findOneBy(['code' => 'sd-cards']);

$associatedProducts = $this->container->get('sylius.repository.product')->findByTaxon($taxon);

Having a collection of products from the SD cards taxon iterate over them and add them one by one to the association.

foreach ($associatedProducts as $associatedProduct) {
    $association->addAssociatedProduct($associatedProduct);
}

Finally, add the created association with SD cards to our Go Pro camera product.

$product->addAssociation($association);

And to save everything in the database you need to add the created association to the repository.

$this->container->get('sylius.repository.product_association')->add($association);

In the previous example we used a custom query in the product repository, here is the implementation:

use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;

class ProductRepository extends BaseProductRepository
{
    public function findByTaxon(Taxon $taxon): array
    {
        return $this->createQueryBuilder('p')
            ->join('p.productTaxons', 'pt')
            ->where('pt.taxon = :taxon')
            ->setParameter('taxon', $taxon)
            ->getQuery()
            ->getResult()
         ;
    }
}
PreviousProduct ReviewsNextAttributes

Last updated 8 months ago

Was this helpful?