1

I have a site that currently uses a structure where we have a parent entry and children under it. On the parent page we have entries that have been created with matrix blocks. The structure is as follows below in the screenshot:

enter image description here

On each parent page we want the ability to filter by the child entries under it. So far we have got it working well with a little fiddling but the only issue is that its showing entries for other nested children not related to the parent entry.

So for instance the result we want is:

Bedroom furniture(parent page) - Beds - Bedroom storage - etc - etc

But what we are seeing is:

Bedroom furniture(parent page) - Bed - Armchairs

The filters on the page should only be the child entries that belong under that parent.

Now here is the code we have got in the algolia plugin when indexing the commerce product:

[
            'indexName' => 'allProducts',
            'elementType' => 'Commerce_Product',
            'transformer' => function(Commerce_ProductModel $product) {

                $typesCriteria = craft()->elements->getCriteria(ElementType::Entry, [
                    'section' => 'products',
                    'relatedTo' => [
                        'targetElement' => $product,
                        'field' => 'productBlock.product',
                    ]
                ]);

                $types = array_map(function($el) {
                  return [
                    'title' => $el->title,
                    'id' => $el->id,
                    'level' => $el->level,
                  ];
                }, $typesCriteria->find());
                $typesByLevel = [];  

                foreach ($types as $type) {
                  $typesByLevel['types.level' . $type['level']][] = $type;                   
                }                

                $variantAmount = count($variants);

                return array_merge([
                    'title' => $title,
                    'id' => $product->id,
                    'frontImage' => $fm,
                    'type' => $product->type->name,
                    'types' => $types,
                    'designer' => $designer,
                    'brand' => $brand,
                    'price' => $totalPrice,
                    'productPrice' => $product->defaultVariant->price,
                    'material' => $product->variantMaterials[0]->title,
                    'url' => $url,
                    'variants' => $variantAmount,
                    'isInWishlist' => craft()->like->isLike($product->id),
                    'quickViewJson' => $mergedProductData,
                ], $typesByLevel);
            }
        ],

This code indexes the product and creates the levels in Algolia and we have this result in algolia below:

enter image description here enter image description here

This gets the level but we need the level that is associated with the parent page only not from all other areas of the site.

On the parent page we then get the parent structure Id and do disjunctive faceting with js on that page the get the filters that belong to that page like so:

if( $('#product-search-config').length ){
 // type gets the entry.id
        const type = $('#product-search-config').data('type');

            searchProducts.addWidget({
              init: function(data) {
                console.log(data)
              },
              getConfiguration: function() {
                return {
                  disjunctiveFacets: ['types'],
                  disjunctiveFacetsRefinements: {
                    'types.level1.id': [type],
                  }
                };
              }
            });

    };

This needs to only find the filter relating to that parent as you see in the menu above.

Can this be done?

Thanks

  • Your question is really hard to understanding. Let's sum it up: you have a structured section called products. What do you mean with "filtering children under those entries" you can get all children with the children function. There is usually no need to filter them somehow. With your current structure it should be impossible to get armchair when you are fetching the children for bedroom. And you can't create entries with matrix blocks. You can relate entries with a field inside a matrix but I don't have any idea what you mean with "entries that have been created with matrix blocks" – Robin Schambach Feb 28 '18 at 3:50
  • Basically the children under each parent structure Entry need to be the filters when you land on the parent structure Entry page. These filters need to be added to Algolia with indexing and the children must only show on the parent structure entry that belong to that parent only. The issue is that in Algolia that relationship is not working. – M dunbavan Feb 28 '18 at 12:54
  • Then again, one child will only belong to one parent. So your entry.children will return an array of entries that are totally unique. there is no entry that will return the same child like another one – Robin Schambach Feb 28 '18 at 12:58
  • Hmm yes I know that but we aren’t using the native craft templates we are using a plugin to index that in Algolia – M dunbavan Feb 28 '18 at 13:00
  • What does this has to do with your question? There is no difference between calling functions in twig or php – Robin Schambach Feb 28 '18 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.