You are browsing the documentation for version 4.1 of OroCommerce, OroCRM and OroPlatform, which is no longer maintained. Security Support ends in January 2023. Read version 5.0 (the latest LTS version) of the Oro documentation to get the updated information.
See our Release Process documentation for more information on the currently supported and upcoming releases.
Usually, when you sell a product you also offer your customers some related products that are a tempting or useful add-on to the product. For example, if you are selling phones, it is good to show fitting accessories next to it. This is where the Related Items functionality comes handy.
Class Oro\Bundle\ProductBundle\RelatedItem\AbstractRelatedItemConfigProvider provides you with the configuration for Related Items.
You can find example in Oro\Bundle\ProductBundle\RelatedItem\RelatedProductRelatedProductsConfigProvider.
Here is the quick overview of its usage:
Returns information on whether the Related Items functionality is enabled.
Returns integer describing how many related products can be assigned for one product.
Returns information about the relation type. To illustrate the difference between uni- and bidirectional relation, let us consider the following example. Product B is a related item of Product A. If bidirectional attribute value is set to true, the product A is also considered as related item of Product B (see [Strategy](#strategy)). This does not apply when the relation is not bidirectional.
When viewing product A in the storefront, a buyer can always see the Product B in related items. When viewing product B in the storefront, the buyer may or may not see the Product A in related items (depending on the type of relationship).
isBidirectional == True and Product B is a related item of Product A
|Product A||Product B|
|Product B||Product A|
isBidirectional == False and Product B is a related item of Product A
|Product A||Product B|
For more information about configuration see OroConfigBundle.
The finder strategy (Oro\Bundle\ProductBundle\RelatedItem\FinderStrategyInterface) provides related products for a user. You can see a simple example in Oro\Bundle\ProductBundle\RelatedItem\RelatedProduct\AssignerDatabaseStrategy where related products are fetched from the database. This strategy takes into account the configuration from the Config Provider.
For more complex logic, you can create your own strategy.
For this, implement Oro\Bundle\ProductBundle\RelatedItem\Strategy\StrategyInterface, overload or decorate its definition: “oro_product.related_products.strategy”, and provide the implementation of the find method:
1 2 3 4 5
/** * @param Product $product * @return Product */ public function find(Product $product);
where $product is a Product object for which you are searching related products. The method has to return an array of Products.
Oro\Bundle\ProductBundle\RelatedItem\AssignerStrategyInterface Use AssignerStrategyInterface to store the relation between the product and its related item. An example of the implementation can be found in Oro\Bundle\ProductBundle\RelatedItem\RelatedProducts\AssignerDatabaseStrategy.
It defines two methods:
1 2 3 4 5 6 7 8 9
/** * @param Product $productFrom * @param Product $productsTo * * @throws \LogicException When functionality is disabled * @throws \InvalidArgumentException When a user tries to add a related product to itself * @throws \OverflowException When a user tries to add more products than the limit allows */ public function addRelations(Product $productFrom, array $productsTo);
The addRelations method saves the Related Items relations: from $productFrom to every product provided in $productsTo. The following exceptions may be thrown:
- LogicException should be thrown when Related Items functionality is disabled.
- InvalidArgumentException should be thrown, when a user tries to add product as a related item for itself.
- OverflowException should be thrown when a user tries to add more products that the configured limit allows.
1 2 3 4 5
/** * @param Product $productFrom * @param Product $productsTo */ public function removeRelations(Product $productFrom, array $productsTo);
The removeRelations method removes all Related Item relationships between the product provided in $productsTo and the $productFrom product. When a product in $productsTo is not a Related Item of the $productFrom, no exception should be thrown and the invalid relationship should be skipped. Other Related Items relationships should be processed.