I recently needed to create a datetime product attribute programatically in Magento 2. When looking for it on the Internet, all the information I found was outdated. It’s really easy to do so.
Use this for the attribute creation and it will be displayed as datetime in the backoffice right away, without any ui_component customization:
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'offer_start_time',
[
'group' => 'Advanced Pricing',
'label' => 'Offer Start Time',
'type' => 'datetime',
'input' => 'datetime',
'input_renderer' => 'Velanapps\Test\Block\Adminhtml\Form\Element\Datetime',
'class' => 'validate-date',
'backend' => 'Magento\Catalog\Model\Attribute\Backend\Startdate',
'frontend' => 'Magento\Eav\Model\Entity\Attribute\Frontend\Datetime',
'required' => false,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'visible' => true,
'user_defined' => true,
'default' => '',
'searchable' => true,
'filterable' => true,
'filterable_in_search' => true,
'visible_in_advanced_search' => true,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false
]
);
0 Comments