If the configurable product has size, I want to add a size guide for it. But how can I check that product have the size or not?

configurable product


Try this

  //check product is configurable 

  if ($product -> getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE){
      //if product is configurable

      $product -> hasData('size');
      $product  > hasSize();


Hope this help you, Thanks

  • Thank you. I have used this code: $sizeguideBlock = $this->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('size_guide')->toHtml(); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); var_dump($product->hasData('size')) but it return false. Do you have any idea? – David Duong Aug 20 '19 at 6:42

Option 1:

$size = $_product->getResource()->getAttribute('size')->getFrontend()->getValue($_product);

Option 2

$size = $_product->getSize();

Then you just need to check it exists and output your block

if($size) {
  • Thank you. I try to check it on a configurable product, but it's not working. This return false and $size = $_product->getSize(); return null. – David Duong Aug 22 '19 at 4:01
 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

 class yourclass{
  const CONFIGURABLE = "configurable";

   protected $_productCollection;

   public function __construct(

    CollectionFactory $collectionFactory,
    ) {
      $this->_productCollection = $collectionFactory;

    public function yourfunction
      $products = $this->_productCollection->create();
      foreach($products as $product){
       if ($product->getTypeId() == self::CONFIGURABLE ) {
         // if options are their
         // do your rest of work
       //if no options are their
  • Thank you. How about the configurable product don't have size option? – David Duong Aug 20 '19 at 6:44
  • @DavidDuong kindly check the this code – mohith Aug 20 '19 at 7:10
  • How about if that product has the only color and doesn't have size. I don't want to show size guide in this case. – David Duong Aug 21 '19 at 7:36

Thank you all, you guys. I have checked it by this code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');

$data = $product->getTypeInstance()->getConfigurableOptions($product);

$show_size = false;
foreach ($data as $key => $value) {
     if($value['0']['attribute_code'] == 'size')
         $show_size = true;

Hope this helps someone!

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.