cancel
Showing results for 
Search instead for 
Did you mean: 

Need to create an integer product attribute

Need to create an integer product attribute

Hi all,

 

I need to add a custom attribute which represents an age. My final goal is to query the products with the API, using search criteria, to get products whose attribute "age" is greater than 30 for example.

 

The problem is that when creating a product attribute in the back-office (Store > Attributes > Product > Add New Attribute), in the select Catalog Input Type for Store Owner there is no "integer type", except Yes/No, which is not really suitable...

 

If I store ages using a Text Field, comparisons such as "is greater than 30" don't work, since ages are actually stored as strings.

 

Is there a way to achieve what I need?

 

Thank you in advance

4 REPLIES 4

Re: Need to create an integer product attribute

Hi @david_iux 

 

Magento out-of-the-box doesn't offer as well as support Age-associated any core functionality, which you can extend or configure it when you create a new attribute(s). 

 

This restriction Magento has applied to their both editions - Open Source & Commerce.

 

So, the only option remains over here is to purchase a good comprehensive extension from the Magento Marketplace and install it to your Magento instance.

 

Hope it helps!

Found the above suggestion helpful, hit the "Kudos" & Accept as Solution!

Thank you,
Rohan D.
Free & Premium Magento 2 Extensions

Re: Need to create an integer product attribute

Hi @david_iux 


As magento do not allow to create Integer type attribute, but you can create it programmatically and also add the validation "is greater than 30" while creating attribute. You can use below code for create product attribute with type = Integer:

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, 'age',
  [
    'type' => 'int',
    'label' => 'Age',
    'input' => 'text',
    'required' => false,
    'visible' => true,
    'system' => 0,
    'is_user_defined' => 1,
    'searchable' => false,
    'filterable' => false,
    'comparable' => false,
    'visible_on_front' => false,
    'used_in_product_listing' => true,
    'unique' => false,
    'default' => '',
    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
    'apply_to' => '',
    'frontend_class' => 'validate-length maximum-length-30',
  ]
);




Problem solved? Please give 'Kudos' and accept 'Answer as Solution'.

Re: Need to create an integer product attribute

Hi

I found an easy way around this was to store my variable as a price, that way sorting and filtering worked as expected.

 

for example sorting descending as text

938 would come before1518 

 

But if you set the attribute as a price you get numeric sort and 

1518 does come before 938

Re: Need to create an integer product attribute

Hello @david_iux
You can also try this method, Product attributes are additional characteristics of a product. For example product attributes can be size and color. You first create the attribute, such as size. Then, you create values for this attribute.
Regards!