3

I'm new to magento 2. Correct me If I'm wrong in the title I've made for this thread.

I have a problem in my condition about a specific attribute for a product. I want to show a content when product attribute promo_banner is set to yes.

Here's my code:

<?php if ( ($parent_category->getParentId() == 40 ) && ($_product->getAttributeText('promo_banner'))) :?>
        <div class="container">
            <div class="row">
                <div class="col-md-6 container-of-text-banner">
                    <label class="promo-banner-here">
                        Save when ordering case quantities!! Extra 20% Savings
                    </label>
                </div>
            </div>
        </div>
<?php endif;?>

Thanks, MazeStricks

3
  • Try with if condition ( ($parent_category->getParentId() == 40 ) && (!empty($_product->getAttributeText('promo_banner')) && $_product->getAttributeText('promo_banner' != '')) Commented Dec 14, 2016 at 10:41
  • Thanks for the comment Rakesh you're so good. I'll try this one Commented Dec 15, 2016 at 1:38
  • Thank you so much Rakesh for the effort answering my question. Thanks for the time. Commented Dec 15, 2016 at 2:00

2 Answers 2

1

In eav_attribute table, the product attribute Yesno values are stored as 'int' (backend_type) so you should try with:

if ( ($parent_category->getParentId() == 40 ) && ($_product->getData('promo_banner') == 1))
2
  • Hi WaPoNe, I'll try this also thanks for your answer Commented Dec 15, 2016 at 1:39
  • Hi Guys and also WaPone, I tried your approach this works best on me. Thank you so much for your answer! Commented Dec 15, 2016 at 1:58
1

I would suggest to not hard code category ID's. That is a bad practice as in some scenarios they can change.

<?php if ((bool)$_product->getAttributeText('promo_banner'))) :?>
        <div class="container">
            <div class="row">
                <div class="col-md-6 container-of-text-banner">
                    <label class="promo-banner-here">
                        Save when ordering case quantities!! Extra 20% Savings
                    </label>
                </div>
            </div>
        </div>
<?php endif;?>
3
  • Hi Andre thanks for a very informative approach. I will avoid hard coding the ID's in the future projects. I'll try this approach also. Commented Dec 15, 2016 at 1:40
  • Hi Andre I tried also this approach of yours but it did not work in my end. but anyway thank you for your effort answering my question. :) Commented Dec 15, 2016 at 2:00
  • 1
    I upvoted your answer because it will help others to remind also them that it's not proper to hard code category ID's. Thanks for this Commented Dec 15, 2016 at 2:04

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.