Manage which user can access certain Products by whitelisting Customer Groups for each item in your catalog
- Use your Customer Groups to filter which products your users can access
- Create a whitelist of Groups for each product
Creating a Customer Group and setting which Groups are authorized to access a product
- Add the repository to composer.json
"repositories": [
{
"type": "git",
"url": "https://github.com/asdoria/AsdoriaSyliusProductCustomerGroup.git"
}
],
-
run
composer require asdoria/sylius-product-customer-group-plugin
-
Add the bundle in
config/bundles.php
. You must put it ABOVESyliusGridBundle
Asdoria\SyliusProductCustomerGroupPlugin\AsdoriaSyliusProductCustomerGroupPlugin::class => ['all' => true],
4.In src/Entity/Product/Product.php
. Import the following classes, traits and methods.
use Sylius\Component\Core\Model\Product as BaseProduct;
use Asdoria\SyliusProductCustomerGroupPlugin\Model\Aware\CustomerGroupsAwareInterface;
use Asdoria\SyliusProductCustomerGroupPlugin\Traits\CustomerGroupsTrait;
class Product extends BaseProduct implements CustomerGroupsAwareInterface
{
use CustomerGroupsTrait;
public function __construct()
{
parent::__construct();
$this->initializeCustomerGroupsCollection();
}
...
}
5.In src/Entity/Customer/CustomerGroup.php
. Import the following classes, traits and methods.
use Asdoria\SyliusProductCustomerGroupPlugin\Model\Aware\ProductsAwareInterface;
use Asdoria\SyliusProductCustomerGroupPlugin\Traits\ProductsTrait;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Customer\Model\CustomerGroup as BaseCustomerGroup;
/**
* @ORM\Entity
* @ORM\Table(name="sylius_customer_group")
*/
class CustomerGroup extends BaseCustomerGroup implements ProductsAwareInterface
{
use ProductsTrait;
public function __construct()
{
$this->initializeProductsCollection();
}
...
}
-
run
php bin/console do:mi:mi
to update the database schema -
Add the additional Product xml mapping in
src/Resources/config/doctrine/Product.orm.xml
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<mapped-superclass name="Product">
<many-to-many field="customerGroups" target-entity="Sylius\Component\Customer\Model\CustomerGroupInterface">
<join-table name="asdoria_products_customer_groups">
<join-columns>
<join-column name="product_id" referenced-column-name="id" />
</join-columns>
<inverse-join-columns>
<join-column name="customer_group_id" referenced-column-name="id" />
</inverse-join-columns>
</join-table>
</many-to-many>
</mapped-superclass>
</doctrine-mapping>
- Copy the template override from the plugin directory
From: [shop_dir]/vendor/asdoria/sylius-product-customer-group-plugin/src/Resources/views/bundles/SyliusShopBundle/Product/Box/_content.html.twig
To: [shop_dir]/templates/bundles/SyliusShopBundle/Product/Box/_content.html.twig
Linux CMD : cp -r vendor/asdoria/sylius-product-customer-group-plugin/src/Resources/views/bundles/SyliusShopBundle/Product/Box templates/bundles/SyliusShopBundle/Product/Box
If you want to try to create pictograms, go on the admin authentication page and connect with:
Login: asdoria
Password: asdoria
Then go on "Groups" in the back office and follow usage lines below.
Note that we have developed several other open source plugins for Sylius, whose demos and documentation are listed on the following page.
- Create or use existing Customer Groups
- Go to a product's configuration page and enter the "Customer Groups" tab
- Select any Group that will gain access to the product