Skip to content

how to enable/disable custom attribute showing on product page

I created an attribute switch, but how do I get it to work? Also I created an attribute – which I want enable/disable

  <?php

   namespace ivanProductAtrrSetupPatchData;

   use MagentoCatalogModelProduct;
   use MagentoCatalogSetupCategorySetup;
   use MagentoEavModelEntityAttributeScopedAttributeInterface;
   use MagentoFrameworkSetupPatchDataPatchInterface;
   use MagentoFrameworkSetupModuleDataSetupInterface;
   use MagentoCatalogSetupCategorySetupFactory;

  class AddProductAtrrEnableAttribute implements DataPatchInterface
 {
/**
 * @var ModuleDataSetupInterface
 */
private $moduleDataSetup;

/**
 * @var CategorySetupFactory
 */
private $categorySetupFactory;

/**
 * @param ModuleDataSetupInterface $moduleDataSetup
 * @param CategorySetupFactory $categorySetupFactory
 */
public function __construct(
    ModuleDataSetupInterface $moduleDataSetup,
    CategorySetupFactory $categorySetupFactory
) {
    $this->moduleDataSetup = $moduleDataSetup;
    $this->categorySetupFactory = $categorySetupFactory;
}

public function apply()
{
    /** @var CategorySetup $categorySetup */
    $categorySetup = $this->categorySetupFactory->create(['setup' => $this- 
    >moduleDataSetup]);

    $attributeCode = 'product_atrr_enable';
    $attributeLabel = 'Product Atrr Enable';

    $categorySetup->addAttribute(
        Product::ENTITY,
        $attributeCode,
        [
            'type' => 'int',
            'frontend' => '',
            'label' => $attributeLabel,
            'input' => 'boolean',
            'backend' => ProductAttributeBackendBoolean::class,
            'source' => ProductAttributeSourceBoolean::class,
            'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'default' => '',
            'visible_on_front' => false,
            'unique' => false,
            'is_used_in_grid' => true,
            'sort_order' => 49
        ]
    );

    $attributeSetId = $categorySetup->getDefaultAttributeSetId(Product::ENTITY);

    $categorySetup->addAttributeToGroup(
        Product::ENTITY,
        $attributeSetId,
        'Default',
        $attributeCode,
        99
    );
}

public static function getDependencies()
{
    return [];
}

public function getAliases()
{
    return [];
}

}

catalog_product_view.xml

      <?xml version="1.0"?>
     <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:
noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" 
        >
     <body>
    <referenceBlock name="product.info.main">
        <block class="IvanProductAtrrBlockAtrr" name="ivan.atrr" 
  template="Ivan_ProductAtrr::atrr.phtml" />
    </referenceBlock>
    <move element="ivan.atrr" destination="product.info.main" 
   after="product.info.price"/>
   </body>
     </page>