1

I have created product attribute from admin and set input type "Date" and assigned "default date value" but default value is not showing when adding product and frontend also. Please check below screenshot :

enter image description here

enter image description here

Anyone have an idea , how can solve this?

4
  • You want default value for that date attribute in frontend, Right ? Commented Oct 19, 2019 at 6:53
  • @KishanSavaliya Yes. correct. Can you please help me for that.
    – Ravi Varma
    Commented Oct 19, 2019 at 7:39
  • Please check my answer :) Commented Oct 19, 2019 at 7:44
  • @RaviVarma: How you fix this issue? Please share your solution Commented Jul 14, 2021 at 13:32

1 Answer 1

1

You can use following code to get any attribute's default value.

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productId = 2; // Your Product ID
    $storeId = 1; // Your Store ID
    $product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
    $product->setStoreId($storeId);
    $defaultValue = $product->getResource()->getAttribute('your_attribute_code')->getFrontend()->getAttribute()->getDefaultValue();
    echo $defaultValue;
?>
  • You can use your attribute code instead of your_attribute_code.
  • Please make sure you've selected "Used in Product Listing : Yes" for that attribute.

Hope this will helps you!

5
  • Thanks for your answer. This issue is that date attribute default value is not coming in "add product page" you can see my screenshot. I want default value of date in add product/edit product page. so if default value come in add/edit product page then it will automatically display in front-end also.
    – Ravi Varma
    Commented Oct 19, 2019 at 7:56
  • Yes, I've read your requirements. And Yes, Somehow that is not working with default Magento. So you can use above thing in frontend to get default value of any attribute if not any value assigned for particular product. Commented Oct 19, 2019 at 8:03
  • I've just checked, I set default value in default date attribute field and tried to save without selecting any date there and that is not working. So may be Magento added some restriction for date type attribute here. Commented Oct 21, 2019 at 6:59
  • Thank you for your effort. I am checking release not of latest magento: prnt.sc/pmyujk
    – Ravi Varma
    Commented Oct 23, 2019 at 9:09
  • Great! I also didn't notice that in release notes. Thanks for sharing info. :) You can accept my answer or give me an upvote for me. :) Commented Oct 23, 2019 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.