Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
date_attribute.php
Go to the documentation of this file.
1 <?php
7 /* Create attribute */
8 
11 
14  \Magento\Catalog\Setup\CategorySetup::class,
15  ['resourceName' => 'catalog_setup']
16 );
17 
20  \Magento\Catalog\Model\ResourceModel\Eav\Attribute::class
21 );
22 $dateAttribute->setData(
23  [
24  'attribute_code' => 'date_attribute',
25  'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
26  'is_global' => 1,
27  'is_filterable' => 1,
28  'backend_type' => 'datetime',
29  'frontend_input' => 'date',
30  'frontend_label' => 'Test Date',
31  ]
32 );
33 $dateAttribute->save();
34 /* Assign attribute to attribute set */
35 $installer->addAttributeToGroup('catalog_product', 'Default', 'General', $dateAttribute->getId());
36 
38 $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
40  ->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
41  ->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
42  ->setWebsiteIds([1])
43  ->setName('Simple Product with date attribute')
44  ->setSku('simple_product_with_date_attribute')
45  ->setPrice(1)
46  ->setCategoryIds([2])
47  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
48  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
49  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 5, 'is_in_stock' => 1])
50  ->save();
51 
52 $objectManager->get(\Magento\Catalog\Model\Product\Action::class)
53  ->updateAttributes(
54  [$product->getId()],
55  [
56  $dateAttribute->getAttributeCode() => '01/01/2000' // m/d/Y
57  ],
58  $product->getStoreId()
59  );
$objectManager
$product
$dateAttribute
$installer