I've added a custom attribute in the Magento 2 back end called preorder and preordernote, then in my custom theme I have created an override to display Preorder instead of Add to Cart in the product view, as well as the preorder note.

The override is in the following structure: Magento_Catalog > templates > product > view > addtocart.phtml.

I will also be modifying the catalog view, and cart page to include the appropriate pre order messages when a product has the preorder attribute set to 'yes'.

While this is working, I am confused as to if I have used the correct method or not. Is there a recommended way to do this type of customization?


To call attributes that you have created try below code:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
    $demo = $product->getResource()->getAttribute('your_attribute_code')->getFrontend()->getValue($product);

Now you can call this variable $demo anywhere you like. like below:

<?php echo $demo; ?>
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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