0

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 Mymodule\Countdown\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

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(
        \Magento\Catalog\Model\Product::ENTITY,
        'countdown_enabled',
        [
        'group' => 'General',
        'type' => 'int',
        'backend' => '',
        'frontend' => '',
        'label' => 'Show Countdown',
        'input' => 'boolean',
        'class' => '',
        'source' => '',
        'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::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(
            \Magento\Catalog\Model\Product::ENTITY,
            'course_start_from',
            [
                'type' => 'datetime',
                'backend' => '',
                'frontend' => 'Magento\Eav\Model\Entity\Attribute\Frontend\Datetime',
                'label' => 'Delivery time from',
                'input' => 'date',
                'class' => '',
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::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(
            \Magento\Catalog\Model\Product::ENTITY,
            'course_start_end',
            [
                'type' => 'datetime',
                'backend' => '',
                'frontend' => 'Magento\Eav\Model\Entity\Attribute\Frontend\Datetime',
                'label' => 'Delivery time to',
                'input' => 'date',
                'class' => '',
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::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?

Ravi Varma
  • 548
  • 6
  • 25

2 Answers2

0

I think you should be using modifiers for time and data attribute. or you can also do it by adding date and time ui component. I have added some links to help you

Magento 2 Create Date with Time attribute for product

Magento 2 - How to add the DateTime UI Component

Hope it helps!

surbhi agr
  • 886
  • 5
  • 22
0

I use this way and it's clear for me. Put code to Setup/InstallData.php

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();
    /** @var EavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$attrCode = Attribute::TIMER_1_ATTRIBUTE_CODE;

try {
        $eavSetup-&gt;addAttribute(
            Product::ENTITY,
            $attrCode,
            [
                'type' =&gt; 'datetime',
                'backend' =&gt; '',
                'frontend' =&gt; '',
                'label' =&gt; __($attrCode),
                'input' =&gt; 'date',
                'class' =&gt; '',
                'group' =&gt; self::ATTRIBUTES_GROUP,
                'global' =&gt; ScopedAttributeInterface::SCOPE_STORE,
                'visible' =&gt; true,
                'required' =&gt; false,
                'user_defined' =&gt; true,
                'default' =&gt; '',
                'searchable' =&gt; true,
                'filterable' =&gt; true,
                'comparable' =&gt; true,
                'visible_on_front' =&gt; false,
                'used_in_product_listing' =&gt; false,
                'unique' =&gt; false,
                'source' =&gt; '',
                'sort_order' =&gt; '100',
                'is_used_in_grid' =&gt; true,
                'is_visible_in_grid' =&gt; true,
                'is_filterable_in_grid' =&gt; true,
                'apply_to' =&gt; 'simple,configurable,virtual,bundle,downloadable',
            ]
        );
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {

}

$setup-&gt;endSetup();

}

Also create etc/catalog_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
<group name="quote_item">
    <attribute name="attr_name_code"/>
</group>
</config>
ilia plat
  • 1
  • 1