0

Can anyone help with the correct syntax for what I'm trying to achieve?

I need an IF statement with the correct syntax please.

I have a .phtml template and want to display html code containing a custom attribute value. This is working fine, but I only want to display the html and attribute values if the attribute contains a value.

The reason for this is that I have a style on the li within the ul code that displays an icon. Currently if there are no attribute values, the div, ul and li code still displays and so does the icon.

My current code needs an If Then Statement so that if the attribute value is empty, the page displays an empty div. So something like: If then display an empty div Else display my code and the attribute vales. It kind of works now, but because I have a style that displays an icon on the tag, I don't want to display the div, ul, li code.

See the tick in the image.

Icon

Style:

style

1
  • Share your phtml file's code.
    – Mital Shah
    Commented Jun 11, 2021 at 12:13

5 Answers 5

0

try this

<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();
if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
?>
<?php if ($_attributeValue):?>
<?php $_attributeValue = explode(",",$_attributeValue); ?>
<div>   
<div class="product-attribute-keyfeatures">
    <ul class="product-attribute__list">
        <?php foreach ($_attributeValue as $_attributeValuesep): ?>
        <li class="product-attribute__item"> 
            <span><?php echo $_attributeAddAttribute;?><?php echo $_attributeValuesep; ?></span>
        </li>
        <? endforeach; ?>
    </ul>       
</div>
</div>
<?php endif;?>
0
0

I hope you're getting custom attribute value in phtml then on top of your .phtml file just check <?php if (isset(your_custom_attribute_variable)):?> //your html code in true case <?php endif;?>. Else will do nothing if value is not set.

0

You are checking if $_attributeValue is set. It will always be set, even if it is an empty array. Try checking the size of the array instead:

<?php if (count($_attributeValue)):?>
<div>   
<div class="product-attribute-keyfeatures">
    <ul class="product-attribute__list">
        <?php foreach ($_attributeValue as $_attributeValuesep): ?>
        <li class="product-attribute__item"> 
            <span><?php echo $_attributeAddAttribute;?><?php echo $_attributeValuesep; ?></span>
        </li>
        <? endforeach; ?>
    </ul>       
</div>
</div>
<?php endif;?>
0

The answer from krybbio worked perfectly for this question, Thank You This template code also has an outer If Statement that filters on the Attribute Set used for the product. There are many attributes containing 'key features' and all have different attribute value options. All of these attributes use the same Label (Key Features) but with different Default labels. The current code is written for Magento 1 but this needs to work in Magento 2. Here is the Magento 1 code which hopefully makes sense.

    <?php /* @var Mage_Catalog_Block_Product_View $this */ ?>

<?php
/** @var Mage_Catalog_Model_Product $_product */
/** @var Mage_Eav_Model_Entity_Attribute_Set $attributeSet */
/** @var array $attributes */
$_product       = $this->getProduct();
$attributeSetId = $_product->getAttributeSetId();
$attributeSet   = Mage::getModel('eav/entity_attribute_set')->load($attributeSetId);
$attributes     = Mage::getModel('catalog/product_attribute_api')->items($attributeSetId);
$limit          = (is_null($this->getData('limit')) ? 999 : $this->getData('limit'));
?>

<?php foreach ($attributes as $attribute) : ?>
    <?php /** @var array $attribute */ ?>
    <?php $attribute_code = $attribute['code']; ?>
    
    <?php if ($_product->getResource()->getAttribute($attribute_code)->getStoreLabel() == 'Key Features') : ?>
        <?php
        $key_features = $_product->getAttributeText($attribute_code);
        
        if (!empty($key_features) && !is_array($key_features)) {
            $key_features = array($key_features);
        }
        ?>
        
        <?php if (!empty($key_features) && count($key_features) > 0) : ?>
            <?php $key_features = array_slice($key_features, 0, $limit); ?>

            <div class="product-attribute product-attribute--key-features">
                <ul class="product-attribute__list">
                    <?php foreach ($key_features as $key_feature) : ?>
                        <li class="product-attribute__item">
                            <span class="product-attribute__text">
                                <?php echo $key_feature; ?>
                            </span>
                        </li>
                    <?php endforeach; // $key_features as $key_feature ?>
                </ul>
            </div>
            
            <?php break; ?>
        <?php endif; // is_array($key_features) s?>
    <?php endif; // $_product->getResource()->getAttribute($attribute_code)->getStoreLabel() == 'Key Features' ?>
<?php endforeach; // $attributes as $attribute ?>

How can I change the code so it works in Magento 2 please?

Thank you Andy

2
  • try this: magento.stackexchange.com/a/229570/15051.
    – krybbio
    Commented Jun 17, 2021 at 12:44
  • Hi Krybbio - I'm still struggling with the code :-( I just don't know how to put it all together. How do I implement the code mentioned above so that the template uses the correct attribute set which will then use the correct attribute options? Andy
    – AndyJH
    Commented Jul 14, 2021 at 9:42
0

Thanks again krybbio - I appreciate the time you are taking to help me. I'm not sure how I should add the code you provided but my page code now looks like this (I'm sure this needs changing). If you can advise, I'd be very grateful.

<?php

$attributeSetId = 95;//your_attributeSetId
    $attributeSet = $this->objectManager->create('Magento\Eav\Api\AttributeSetRepositoryInterface');
    $attributeSetRepository = $attributeSet->get($attributeSetId);
    $attribute_set_name = $attributeSetRepository->getAttributeSetName(); 
    $attributeGroupCollection = $this->objectManager->get ( 'Magento\Eav\Model\Entity\Attribute\Group' )->getCollection ();
    $attributeGroupCollection->addFieldToFilter ( 'attribute_group_name', $attribute_set_name ); 
    $attributeGroupCollection->addFieldToFilter ( 'attribute_set_id', $attributeSetId );
    $attributeGroupId = '';
    foreach ( $attributeGroupCollection as $attributeGroup ) {
        $attributeGroupId = $attributeGroup->getId ();
        break;
    }
    $attributeCollection = $this->objectManager->get ( 'Magento\Eav\Model\Entity\Attribute' )->getCollection ();
    $attributeCollection->setAttributeSetFilter ( $attributeSetId );
    $attributeCollection->setAttributeGroupFilter ( $attributeGroupId );

    echo "<pre>";
    print_r($attributeCollection->getData());
    

$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();
if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
?>
<?php if ($_attributeValue):?>
<?php $_attributeValue = explode(",",$_attributeValue); ?>
<div>   
<div class="product-attribute-keyfeatures">
    <ul class="product-attribute__list">
        <?php foreach ($_attributeValue as $_attributeValuesep): ?>
        <li class="product-attribute__item"> 
            <span><?php echo $_attributeAddAttribute;?><?php echo $_attributeValuesep; ?></span>
        </li>
        <? endforeach; ?>
    </ul>       
</div>
</div>
<?php endif;?>

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.