elasticsuite icon indicating copy to clipboard operation
elasticsuite copied to clipboard

Added filterable product attribute custom_filter_qty with setup/installData.php not available on category page

Open kickandrush-ecommerce opened this issue 2 years ago • 7 comments

Is your question related to a problem? Please describe.

I don't understand why an attribute added as admin works as intented (added to attribute set and after given some products in same category different values) but when adding attribute with setup script it will not appear on category page.

I do see the with the after and before in plugin in elasticsuite but that does not apply on install scripts then?

For your info i'm a web developer but kind of new to magento 2

Additional context

Here's the setup:

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, \Kickandrush\SidebarStockFilter\Helper\Data::ATTRIBUTE, [ 'group' => 'General', 'type' => 'decimal', 'backend' => 'Magento\Catalog\Model\Product\Attribute\Backend\Price', 'frontend' => '', 'label' => 'Quantity', 'note' => 'Custom qty for category filter if enabled', 'input' => 'price', 'class' => '', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 'visible' => true, 'required' => false, 'user_defined' => true, 'searchable' => false, 'is_filterable' => true, 'filterable' => true, 'comparable' => false, 'visible_on_front' => false, 'used_in_product_listing' => false, 'unique' => false ] );

kickandrush-ecommerce avatar Sep 02 '22 14:09 kickandrush-ecommerce

Hello @mcs-jonasvanderhaegen,

First of all, we need a little bit more details to understand what exactly you want to do.

Also, please clarify what you mean by saying:

when adding attribute with setup script it will not appear on category page

It's not visible on the products listing or in layered navigation filters, etc?

I'm not sure that your setup script a correct, on first look used_in_product_listing should have true value. But again I don't know what you want to do. Also if you add an attribute via setup script you should also add a code to assign your attribute to some attribute set.

BR, Vadym

vahonc avatar Sep 05 '22 15:09 vahonc

Hi Vadym

The purpose of added attribute "custom_filter_qty" is to filter products on category page.

As an admin when i create the attribute with input type price, filterable on layered navigation with results it shows message "Elasticsuite mapping has been updated real-time. However, your modifications might not be visible until the Catalogsearch Fulltext index is completely rebuilt." and after adding value to couple of products it does appear on frontend category page without a problem. used_in_product_listing does not affect this layered navigation filters,

When I delete the attribute, I add the product attribute programatically with install script, run bin/magento setup:upgrade, it adds the attribute and already puts it in every attribute set in general group. It looks exactly the same in the database like when I create it as admin. but i don' t know maybe i'm missing something.

When I re-edit the products, add a value to them, then it does nothing even after cleaning cache and reindexing everything afterwards. Even if I edit the attribute afterwards. from filterable with results to none to filterable with results again it will not appear on sidebar ever. After cleaning cache and reindexing everything. I don't know why. Magento's documentation on adding attributes programmatically lacks some info pff, filterable => true should be enough.

Is this more clear for you?

kickandrush-ecommerce avatar Sep 06 '22 08:09 kickandrush-ecommerce

Hello @mcs-jonasvanderhaegen,

Thanks for precising what you meant by "appear on the category page" : you meant as a layered navigation filter.

Question : Are you sure the content of eav_attribute and catalog_eav_attribute are really identical whether you create the attribute through a setup or through admin ? One reason would be that the admin / core code would "fix" your combination of backend type + backend model + input type into something supported natively by Magento.

One other reason that could explain this could be the default value for :

  • facets_max_size: max items to show (I don't remember if null or 0 means "display every value" or "do not display any value")
  • facets_coverage: if set to 0 (instead of 90 - for 90%), it may make it so the facet is never displayed But this is strange that editing the setup created attribute in admin would not fix those issues.

Concerning your about the before and after plugins in vendor/smile/elasticsuite/src/module-elasticsuite-catalog/Plugin/Catalog/Eav/AttributePlugin.php :

  • if you already manually decide to force a full reindex of catalogsearch_fulltext
  • AND if you also clear caches

then those plugins do not add any extra intelligence.

Unless maybe the cache which is cleaned through that line

        if ($cleanCache || $updateMapping) {
            $this->indicesConfig->reset();
        }

is not cleaned through a "cache:clear" but requires a cache backend purge instead.

Regards, Richard

rbayet avatar Sep 08 '22 13:09 rbayet

To answer the question yes, at least in eav_attribute table the backend_table, frontend_model, frontend_class, source_model are NULL wether i create it with admin or via script, except for attribute_code it was the same. Yes it's quite confusing, is there literally no other github issue or developer talking about this?

Oh boy my project manager wants to add some filters more but haven't figured this one out yet.

Can anyone of the Smile dev team test this on a local environment or anywhere to confirm?

kickandrush-ecommerce avatar Sep 12 '22 13:09 kickandrush-ecommerce

Hello @mcs-jonasvanderhaegen,

If all DB records are exactly the same, I don't have many more ideas than somehow a cache flush, either the EAV related cache or the one I mentionned in vendor/smile/elasticsuite/src/module-elasticsuite-catalog/Plugin/Catalog/Eav/AttributePlugin.php.

Maybe you missed my hint in the previous message : have your tried doing a cache:flush to make sure the cache of vendor/smile/elasticsuite/src/module-elasticsuite-catalog/Plugin/Catalog/Eav/AttributePlugin.php ? Obviously, please try this before in in another environment than your production environment.

Regards,

rbayet avatar Sep 12 '22 13:09 rbayet

Hi rbayet, ok yes of course no problem i'm doing it on local environment with docker container https://github.com/markshust/docker-magento. Yes i flushed it multiple times but no success. I'm also out of ideas.

I also tested creating attribute eco_score and adding to attribute set Default with only rest api requests and also there i'm having the same problem. It will not appear on the frontend as filter. I selected different values on different products that are in the same category page. So basically as a developer when adding attributes via rest api or install script it should be known your attribute will not appear as frontend category filter if that's the intention.

What I'm not sure yet if this is because of the module or it's just magento instance itself being annoying. I'm going to try that with just a fresh magento installation.

EDIT: i tested it after this comment, install script and creating attribute with rest api work without a problem on fresh instance.

create attribute https://magento.redoc.ly/2.4.5-admin/tag/productsattribute-setsattributes POST {{baseURL}}rest/V1/products/attributes

{
  "attribute": {
    "apply_to": [
        "simple",
        "configurable",
        "bundle",
        "group"
    ],
    "is_wysiwyg_enabled": false,
    "is_html_allowed_on_front": false,
    "used_for_sort_by": false,
    "is_filterable": true,
    "is_filterable_in_search": true,
    "is_used_in_grid": false,
    "is_visible_in_grid": false,
    "is_filterable_in_grid": false,
    "position": 0,
    "is_searchable": false,
    "is_visible_in_advanced_search": true,
    "is_comparable": false,
    "is_used_for_promo_rules": false,
    "is_visible_on_front": true,
    "used_in_product_listing": true,
    "is_visible": true,
    "scope": "global",
    "attribute_code": "eco_score",
    "frontend_input": "select",
    "is_required": false,
    "options": [
      {
        "label": "A",
        "value": "a",
        "sort_order": 0,
        "is_default": true
      },
      {
        "label": "B",
        "value": "b",
        "sort_order": 1,
        "is_default": false
      },
      {
        "label": "C",
        "value": "c",
        "sort_order": 2,
        "is_default": false
      }
    ],
    "is_user_defined": true,
    "default_frontend_label": "Eco score",
    "note": "",
    "backend_type": "int",
    "backend_model": "",
    "source_model": "",
    "is_unique": false,
    "frontend_class": ""
  }
}

assign attribute to attributeset https://magento.redoc.ly/2.4.5-admin/tag/productsattributes#operation/PostV1ProductsAttributes POST {{baseURL}}rest/V1/products/attribute-sets/attributes

{
    "attributeSetId": 4,
    "attributeGroupId": 7,
    "attributeCode": "eco_score",
    "sortOrder": 999
}

kickandrush-ecommerce avatar Sep 13 '22 15:09 kickandrush-ecommerce

@mcs-jonasvanderhaegen,

EDIT: i tested it after this comment, install script and creating attribute with rest api work without a problem on fresh instance.

Did you do your tests with the installed ElasticSuite module or without it?

BR, Vadym

vahonc avatar Sep 14 '22 13:09 vahonc

This issue was waiting update from the author for too long. Without any update, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. Please don't hesitate to comment on the bug if you have any more information for us; we will reopen it right away! Thanks for your contribution.

github-actions[bot] avatar Oct 02 '22 10:10 github-actions[bot]