Skip to content

Magento2: How can create custom datetime product attributes and show timer in frontend?

I want to create custom datetime product attribute with “start from” and “start to” and want to show timer with calculation of both date.
I am using custom module and creating attribute by below code in “InstallData.php”:

    <?php
namespace MymoduleCountdownSetup;

use MagentoEavSetupEavSetup;
use MagentoEavSetupEavSetupFactory;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{

/**
* EAV setup factory
*
* @var EavSetupFactory
*/

private $eavSetupFactory;



/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/

public function __construct(EavSetupFactory $eavSetupFactory)

{

    $this->eavSetupFactory = $eavSetupFactory;

}


public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{

    /** @var EavSetup $eavSetup */

    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);


$eavSetup->startSetup();
    /**
    * Add attributes to the eav/attribute
    */

    $eavSetup->addAttribute(
        MagentoCatalogModelProduct::ENTITY,
        'countdown_enabled',
        [
        'group' => 'General',
        'type' => 'int',
        'backend' => '',
        'frontend' => '',
        'label' => 'Show Countdown',
        'input' => 'boolean',
        'class' => '',
        'source' => '',
        'global' => MagentoCatalogModelResourceModelEavAttribute::SCOPE_GLOBAL,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => '',
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => false,
        'used_in_product_listing' => true,
        'unique' => false,
        'apply_to' => 'simple,configurable,virtual,bundle,downloadable'
        ]
        ); 


         $eavSetup->addAttribute(
            MagentoCatalogModelProduct::ENTITY,
            'course_start_from',
            [
                'type' => 'datetime',
                'backend' => '',
                'frontend' => 'MagentoEavModelEntityAttributeFrontendDatetime',
                'label' => 'Delivery time from',
                'input' => 'date',
                'class' => '',
                'source' => '',
                'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE,
                'visible' => true,
                'required' => true,
                'user_defined' => false,
                'default' => '',
                'sort_order' => 9,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        ); 


          $eavSetup->addAttribute(
            MagentoCatalogModelProduct::ENTITY,
            'course_start_end',
            [
                'type' => 'datetime',
                'backend' => '',
                'frontend' => 'MagentoEavModelEntityAttributeFrontendDatetime',
                'label' => 'Delivery time to',
                'input' => 'date',
                'class' => '',
                'source' => '',
                'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_STORE,
                'visible' => true,
                'required' => true,
                'user_defined' => false,
                'default' => '',
                'sort_order' => 9,
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        ); 

$eavSetup->endSetup();

    }

}

but datetime attribute in not creating in admin.
Anyone have an idea for that?