Search code examples
phploopsshopwareshopware6

Get values defined in Shopware 6 backend into MultiFilter


I'm going to get some values defined in Shopware 6 backend into MultiFilter. Can someone give a tip on implementation?

Can I loop through prefixes inside the MultiFilter somehow?

Surely it doesn't work, but just to give the idea of what I'm trying to setup at Subscriber.php:

    // Here I will receive text strings 
$prefixes = ['Aaa', 'Bbb', 'Ccc', 'Ddd'];
$ii = 0;

// To use in PrefixFilter
if (in_array('3f777000a2734deead391133cee3a6a9', $currentPropertyOptions)) {
    $criteria->addFilter(
        new MultiFilter(
            Multifilter::CONNECTION_OR,
            [

                // This is the idea
                while($ii < count($prefixes))
                {
                    echo "new PrefixFilter('product.properties.name', '$prefixes[$i]'),";
                    $ii++;
                }

                // It should work like this
                new PrefixFilter('product.properties.name', 'Aaa'),
                new PrefixFilter('product.properties.name', 'Bbb'),
                new PrefixFilter('product.properties.name', 'Ccc'),
                new PrefixFilter('product.properties.name', 'Ddd'),

                // Other filters
                new EqualsFilter('product.properties.group.name', 'G1'),
                new EqualsFilter('product.properties.group.name', 'G2'),
                ..
            ]
        )
    );
}

Solution

  • $multiFilter = new MultiFilter(MultiFilter::CONNECTION_OR);
    
    foreach ($prefixes as $prefix) {
        $multiFilter->addQuery(new PrefixFilter('product.properties.name', $prefix));
    }
    
    $multiFilter->addQuery(new EqualsFilter('product.properties.group.name', 'G1'));
    $multiFilter->addQuery(new EqualsFilter('product.properties.group.name', 'G2'));
    
    $criteria->addFilter($multiFilter);