Tag Archives: Attribute

Magento 2 Product Attribute Customer Groups

Creating module PHPCodez_Customattribute to add a new attribute to list all the customer groups including ‘NOT LOGGED IN’.

1) Declare the module

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="PHPCodez_Customattribute" setup_version="0.0.1" />
</config>

2) Register the module. Continue reading Magento 2 Product Attribute Customer Groups

Magento 2 Create Yes No Attribute Programmatically

Here i am creating upgradeData script that will add custom product attribute with yes and no options.

<?php
 namespace PHPCodez\Subscription\Setup;

use Magento\Eav\Setup\EavSetup;
 use Magento\Eav\Setup\EavSetupFactory;
 use Magento\Framework\Setup\UpgradeDataInterface;
 use Magento\Framework\Setup\ModuleContextInterface;
 use Magento\Framework\Setup\ModuleDataSetupInterface;

class upgradeData implements UpgradeDataInterface {
 private $eavSetupFactory;
 protected $logger;

public function __construct(EavSetupFactory $eavSetupFactory,\Psr\Log\LoggerInterface $logger) {
 $this->eavSetupFactory = $eavSetupFactory;
 $this->logger = $logger;
 }

public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
 if (version_compare($context->getVersion(), '0.0.8', '<=')) {
 
  $this->logger->info("System - upgrading module - removing product attribute - subscripion_product");
 $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 $eavSetup->addAttribute(
 \Magento\Catalog\Model\Product::ENTITY,
 'subscripion_product',[
 'type' => 'text',
 'backend' => '',
 'frontend' => '',
 'label' => 'Enable Subscription',
 'input' => 'select',
 'class' => '',
 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
 'visible' => true,
 'required' => true,
 'user_defined' => false,
 'default' => '',
 'searchable' => false,
 'filterable' => false,
 'comparable' => false,
 'visible_on_front' => false,
 'used_in_product_listing' => true,
 'unique' => false,
 'apply_to' => ''
 ]
 );
 
 
 }
 }
 }

Get configurable products attributes Magento

<?php
$obj = Mage::getModel(‘catalog/product’);
$_product = $obj->load(19);
$configurableAttributeCollection=$_product->getTypeInstance()->getConfigurableAttributes();
foreach($configurableAttributeCollection as $attribute){
echo “Attr-Code:”.$attribute->getProductAttribute()->getAttributeCode() .” – “;
echo “Attr-Label:”.$attribute->getProductAttribute()->getFrontend()->getLabel().” – “;
echo “Attr-Id:”.$attribute->getProductAttribute()->getId().” <br /> “;
}
?>