2

I'm trying to display some text IF Product Type = General. This edit is being made within a phtml file.

How'd I'd normally do it for a Product Attribute called Example

<?php $_product = $block->getProduct(); ?>

<?php if ($_product->getExample()): ?>
    <div>
        <p>Example</p>
    </div>
<?php endif; ?>

I'd also like to know how that could be achieved by checking for an Attribute Set.

3
  • Do you need to check attribute set condition? or any attribute condition? Sep 20, 2018 at 14:32
  • For example, I'd want to check if the Attribute Set of the Product was called "Test".
    – Craig
    Sep 20, 2018 at 14:34
  • Check my answer. Sep 20, 2018 at 14:39

3 Answers 3

1

The alternate way to do this, You need to set those attribute value only for those products, for which you want to show that. And you need to code like this.

<?php $_product = $block->getProduct(); ?>

<?php if ($_product->getExample() !== '' && $_product->getExample() !== null): ?>
    <div>
        <p>Example</p>
    </div>
<?php endif; ?>
2
  • $_product->getExample() != '' and $_product->getExample() != null is the same, so no need to ask for both. it differs only if you use !== instead of != Sep 20, 2018 at 14:01
  • Yes, I update answer. Sep 20, 2018 at 14:03
1

You can get attribute set name by following code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$attributeSet = $objectManager->create('Magento\Eav\Api\AttributeSetRepositoryInterface');
$attributeSetRepository = $attributeSet->get($_product->getAttributeSetId());
$attribute_set_name = $attributeSetRepository->getAttributeSetName();
if($attribute_set_name == 'Test'){
    //Do your stuff
}
-1

You can try like below (its for simple products) :

$_product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE

You can check for required product types in file

vendor\Magento\Catalog\Model\Product\Type.php
1
  • I'll try that in a bit. How do you think I'd do this for Attribute Set?
    – Craig
    Sep 20, 2018 at 13:02

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.