Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\BitBag\\SyliusElasticsearchPlugin\\": [
"tests/",
"tests/Application/src"
],
"Tests\\BitBag\\SyliusElasticsearchPlugin\\Behat\\": "tests/Behat",
"spec\\BitBag\\SyliusElasticsearchPlugin\\": "spec/"
},
Expand Down
8 changes: 8 additions & 0 deletions config/services/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,13 @@
<argument type="service" id="bitbag_sylius_elasticsearch_plugin.facet.registry" />
<argument type="service" id="fos_elastica.finder.bitbag_shop_product" />
</service>

<service id="bitbag_sylius_elasticsearch_plugin.form.extension.type.product_attribute" class="BitBag\SyliusElasticsearchPlugin\Form\Extension\ProductAttributeTypeExtension">
<tag name="form.type_extension" extended-type="Sylius\Bundle\AdminBundle\Form\Type\ProductAttributeType" />
</service>

<service id="bitbag_sylius_elasticsearch_plugin.form.extension.type.product_option" class="BitBag\SyliusElasticsearchPlugin\Form\Extension\ProductOptionTypeExtension">
<tag name="form.type_extension" extended-type="Sylius\Bundle\AdminBundle\Form\Type\ProductOptionType" />
</service>
</services>
</container>
16 changes: 16 additions & 0 deletions config/twig/bitbag_twig_hooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,19 @@ sylius_twig_hooks:
order_by: 'price'
sort: 'asc'
priority: 200
'sylius_admin.product_attribute.create.content.form.general':
facet_disabled:
template: '@BitBagSyliusElasticsearchPlugin/admin/product_attribute/form/general/facet_disabled.html.twig'
priority: -100
'sylius_admin.product_attribute.update.content.form.general':
facet_disabled:
template: '@BitBagSyliusElasticsearchPlugin/admin/product_attribute/form/general/facet_disabled.html.twig'
priority: -100
'sylius_admin.product_option.create.content.form.sections.general':
facet_disabled:
template: '@BitBagSyliusElasticsearchPlugin/admin/product_attribute/form/general/facet_disabled.html.twig'
priority: -100
'sylius_admin.product_option.update.content.form.sections.general':
facet_disabled:
template: '@BitBagSyliusElasticsearchPlugin/admin/product_attribute/form/general/facet_disabled.html.twig'
priority: -100
33 changes: 31 additions & 2 deletions doc/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,37 @@ fos_elastica:
```

## Entities
Use `BitBag\SyliusElasticsearchPlugin\Model\ProductVariantTrait` and `BitBag\SyliusElasticsearchPlugin\Model\ProductVariantInterface` in an overridden ProductVariant entity class.
The configuration, depending on the mapping used, may vary.
Use

`BitBag\SyliusElasticsearchPlugin\Model\ProductVariantTrait`
`BitBag\SyliusElasticsearchPlugin\Model\ProductVariantInterface`
`BitBag\SyliusElasticsearchPlugin\Entity\DisableFacetAwareTrait`
`BitBag\SyliusElasticsearchPlugin\Entity\ProductAttributeInterface`
`BitBag\SyliusElasticsearchPlugin\Entity\ProductOptionInterface`

in an overridden ProductVariant,ProductOption and ProductAttribute entity classes. The configuration, depending on the mapping used, may vary.

### Attribute mapping
- [Attribute mapping configuration](installation/attribute-mapping.md)
### XML mapping
- [XML mapping configuration](installation/xml-mapping.md)

### Add configuration for extended product option and product attribute:
```yaml
sylius_product:
resources:
product_option:
classes:
model: App\Entity\Product\ProductOption

sylius_attribute:
resources:
product:
attribute:
classes:
model: App\Entity\Product\ProductAttribute
```

### Update installed assets with the following command:
```bash
bin/console assets:install
Expand All @@ -107,6 +130,12 @@ bin/console assets:install
bin/console cache:clear
```

### Finish the installation by updating the database schema and installing assets
```bash
bin/console doctrine:migrations:diff
bin/console doctrine:migrations:migrate
```

### Finally, with an elasticsearch server running, execute following command:
```bash
bin/console fos:elastica:populate
Expand Down
19 changes: 19 additions & 0 deletions src/Entity/DisableFacetAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on hello@bitbag.io.
*/

declare(strict_types=1);

namespace BitBag\SyliusElasticsearchPlugin\Entity;

interface DisableFacetAwareInterface
{
public function isFacetDisabled(): bool;

public function setFacetDisabled(bool $facetDisabled): void;
}
30 changes: 30 additions & 0 deletions src/Entity/DisableFacetAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on hello@bitbag.io.
*/

declare(strict_types=1);

namespace BitBag\SyliusElasticsearchPlugin\Entity;

use Doctrine\ORM\Mapping as ORM;

trait DisableFacetAwareTrait
{
#[ORM\Column(type: 'boolean', options: ['default' => false])]
protected bool $facetDisabled = false;

public function isFacetDisabled(): bool
{
return $this->facetDisabled;
}

public function setFacetDisabled(bool $facetDisabled): void
{
$this->facetDisabled = $facetDisabled;
}
}
18 changes: 18 additions & 0 deletions src/Entity/ProductAttributeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on hello@bitbag.io.
*/

declare(strict_types=1);

namespace BitBag\SyliusElasticsearchPlugin\Entity;

use Sylius\Component\Product\Model\ProductAttributeInterface as BaseProductAttributeInterface;

interface ProductAttributeInterface extends DisableFacetAwareInterface, BaseProductAttributeInterface
{
}
18 changes: 18 additions & 0 deletions src/Entity/ProductOptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on hello@bitbag.io.
*/

declare(strict_types=1);

namespace BitBag\SyliusElasticsearchPlugin\Entity;

use Sylius\Component\Product\Model\ProductOptionInterface as BaseProductOptionInterface;

interface ProductOptionInterface extends DisableFacetAwareInterface, BaseProductOptionInterface
{
}
4 changes: 2 additions & 2 deletions src/Facet/AutoDiscoverRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function autoRegister(): void

private function discoverAttributes(): void
{
$attributes = $this->productAttributeRepository->findAllWithTranslations($this->localeContext->getLocaleCode());
$attributes = $this->productAttributeRepository->findEnabledWithTranslations($this->localeContext->getLocaleCode());

/** @var AttributeInterface $attribute */
foreach ($attributes as $attribute) {
Expand All @@ -73,7 +73,7 @@ private function discoverAttributes(): void

private function discoverOptions(): void
{
$options = $this->productOptionRepository->findAllWithTranslations($this->localeContext->getLocaleCode());
$options = $this->productOptionRepository->findEnabledWithTranslations($this->localeContext->getLocaleCode());

/** @var ProductOptionInterface $option */
foreach ($options as $option) {
Expand Down
34 changes: 34 additions & 0 deletions src/Form/Extension/ProductAttributeTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace BitBag\SyliusElasticsearchPlugin\Form\Extension;

use Sylius\Bundle\AdminBundle\Form\Type\ProductAttributeType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;

class ProductAttributeTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('facetDisabled', CheckboxType::class, [
'label' => 'bitbag_sylius_elasticsearch_plugin.admin.form.facet_disabled',
]);
}

public static function getExtendedTypes(): iterable
{
return [ProductAttributeType::class];
}
}
34 changes: 34 additions & 0 deletions src/Form/Extension/ProductOptionTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace BitBag\SyliusElasticsearchPlugin\Form\Extension;

use Sylius\Bundle\AdminBundle\Form\Type\ProductOptionType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;

class ProductOptionTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('facetDisabled', CheckboxType::class, [
'label' => 'bitbag_sylius_elasticsearch_plugin.admin.form.facet_disabled',
]);
}

public static function getExtendedTypes(): iterable
{
return [ProductOptionType::class];
}
}
2 changes: 1 addition & 1 deletion src/Form/Type/SearchFacetsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
foreach ($options['facets'] as $facetId => $facetData) {
$facet = $this->facetRegistry->getFacetById((string) $facetId);
$choices = [];
foreach ($facetData['buckets'] as $bucket) {
foreach ($facetData['buckets'] ?? [] as $bucket) {
$choices[$facet->getBucketLabel($bucket)] = $bucket['key'];
}
if ([] !== $choices) {
Expand Down
3 changes: 2 additions & 1 deletion src/Repository/ProductAttributeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ public function getAttributeTypeByName(string $attributeName): string
return $result['type'];
}

public function findAllWithTranslations(?string $locale): array
public function findEnabledWithTranslations(?string $locale): array
{
/** @var EntityRepository $productAttributeRepository */
$productAttributeRepository = $this->productAttributeRepository;

$queryBuilder = $productAttributeRepository->createQueryBuilder('o');
$queryBuilder->andWhere('o.facetDisabled = 0');

if (null !== $locale) {
$queryBuilder
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/ProductAttributeRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ interface ProductAttributeRepositoryInterface
{
public function getAttributeTypeByName(string $attributeName): string;

public function findAllWithTranslations(?string $locale): array;
public function findEnabledWithTranslations(?string $locale): array;
}
11 changes: 6 additions & 5 deletions src/Repository/ProductOptionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ public function __construct(
) {
}

public function findAllWithTranslations(?string $locale): array
public function findEnabledWithTranslations(?string $locale): array
{
/** @var EntityRepository $queryBuilder */
$queryBuilder = $this->productOptionRepository;
/** @var EntityRepository $productAttributeRepository */
$productAttributeRepository = $this->productOptionRepository;

$queryBuilder = $productAttributeRepository->createQueryBuilder('o');
$queryBuilder->andWhere('o.facetDisabled = 0');

if (null !== $locale) {
$queryBuilder
->createQueryBuilder('o')
->addSelect('translation')
/** @phpstan-ignore-next-line phpstan can't read relationship correctly */
->leftJoin('o.translations', 'translation', 'ot')
Expand All @@ -39,7 +41,6 @@ public function findAllWithTranslations(?string $locale): array
}

return $queryBuilder
->createQueryBuilder('o')
->getQuery()
->getResult()
;
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/ProductOptionRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@

interface ProductOptionRepositoryInterface
{
public function findAllWithTranslations(?string $locale): array;
public function findEnabledWithTranslations(?string $locale): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="col-12 col-md-6">
{{ form_row(hookable_metadata.context.form.facetDisabled, sylius_test_form_attribute('facetDisabled')) }}
</div>
11 changes: 11 additions & 0 deletions tests/Application/config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ doctrine:
charset: UTF8

url: '%env(resolve:DATABASE_URL)%'

orm:
entity_managers:
default:
auto_mapping: true
mappings:
TestApp:
is_bundle: false
type: attribute
dir: '%kernel.project_dir%/src/Entity'
prefix: Tests\BitBag\SyliusElasticsearchPlugin
12 changes: 12 additions & 0 deletions tests/Application/config/packages/sylius_product.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sylius_product:
resources:
product_option:
classes:
model: Tests\BitBag\SyliusElasticsearchPlugin\Entity\ProductOption

sylius_attribute:
resources:
product:
attribute:
classes:
model: Tests\BitBag\SyliusElasticsearchPlugin\Entity\ProductAttribute
24 changes: 24 additions & 0 deletions tests/Application/src/Entity/ProductAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on hello@bitbag.io.
*/

declare(strict_types=1);

namespace Tests\BitBag\SyliusElasticsearchPlugin\Entity;

use BitBag\SyliusElasticsearchPlugin\Entity\DisableFacetAwareTrait;
use BitBag\SyliusElasticsearchPlugin\Entity\ProductAttributeInterface;
use Doctrine\ORM\Mapping as ORM;
use Sylius\Component\Product\Model\ProductAttribute as BaseProductAttribute;

#[ORM\Entity]
#[ORM\Table(name: 'sylius_product_attribute')]
class ProductAttribute extends BaseProductAttribute implements ProductAttributeInterface
{
use DisableFacetAwareTrait;
}
Loading