0

I create one custom date attribute for my products in backend:

enter image description here

I need to get the attribute value in the products list page and to check if the product have any data added for the product.

I try something like this but is not work well:

$expectedInDate = $_product->getResource()->getAttribute('expecdate')->getFrontend()->getValue($_product);
if(isset($expectedInDate)) : 
...........
endif; 

What is the correct way to get data attribute value and to check if has data setup per current product?

The entire code:

<?php $timezoneInterface = $objectManager->create('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');  ?>
<?php $expectedInDate = $_product->getResource()->getAttribute('expecdate')->getFrontend()->getValue($_product); ?>
<?php if ($StockState->getStockQty($_product->getId(), $_product->getStore()->getWebsiteId()) > 0 ): ?>
    <div class="stock available"><span><?= /* @escapeNotVerified */ __('In stock') ?></span></div>
<?php else: ?>
    <?php if($expectedInDate) : ?>
        <?php $date =   $timezoneInterface->date($expectedInDate)->format('m/d/Y');?>
        <div class="stock available-in"><span><?= /* @escapeNotVerified */ __('Available in:') ?><?php echo $date; ?></span></div>
    <?php else: ?>
        <div class="stock unavailable"><span><?= /* @escapeNotVerified */ __('Out of stock') ?></span></div>
    <?php endif; ?>
<?php endif; ?>
3
  • Did you try with $_product->getExpecdate(); ? What about the flag "Used in Product Listing", is it set to yes?
    – HelgeB
    Commented Feb 19, 2019 at 8:23
  • Hi, thank you for your help yes the Used in Product Listing is it set to yes
    – Robert
    Commented Feb 19, 2019 at 8:27
  • @Robert you can check my answer Commented Feb 19, 2019 at 8:28

2 Answers 2

4
$value = $product->getData('expecdate'));
if ($value) {
    //print value here
}

Now depending on the attribute type you can display it in different ways.
if it's a text or textarea or int or decimal you can just echo it.
If it's dropdown attribute or multiselect you can display it like this

echo $_product->getResource()->getAttribute('expecdate')->getFrontend()->getValue($_product);

if it's a date attribute you may need to format it:

if you are inside a template you can simply do

echo $block->formatData($value);  

or

echo $block->formatDate($value, \IntlDateFormatter::LONG); //or MEDIUM, SHORT, FULL

if you don't want the time also printed and not just the date, pass the third parameter to the formatDate method true.

3
  • Hi, thank you so much Marius, I edit my post and I add my entire code there, all I want to do is to check if the product have qty > 0 to display in stock, else if the date attribute has value then to display this value, if the value is not set to display out of stock
    – Robert
    Commented Feb 19, 2019 at 8:37
  • I made the code like you said and everything seems to be perfect, thank you, is there any way to have the date format like this: dd.mm.yyyy
    – Robert
    Commented Feb 19, 2019 at 8:58
  • I don't think you should impose a format. just let the current locale display it's own format
    – Marius
    Commented Feb 19, 2019 at 9:24
0
$att_value=$product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($product);
if($att_value!="") { 
      echo $att_value;
}

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.