Category Archives: Magento

Disable Frontend Registration Magento 2

Basically we have to override the function \Magento\Customer\Model\Registration::isAllowed() to achieve this.

Follow the below steps to create an extension PHPCodez_Disableregistration for this task.

1) Declare a 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_Disableregistration" setup_version="0.0.1" />
</config>

2) Register a module.

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'PHPCodez_Disableregistration',
__DIR__
);

3) Create di.xml file and add the below content.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> 
<preference for="Magento\Customer\Model\Registration" type="PHPCodez\Disableregistration\Model\Overrides\Registration" /> 
</config>

4) Override Registration.php

<?php
namespace PHPCodez\Disableregistration\Model\Overrides;
class Registration extends \Magento\Customer\Model\Registration {

private $scopeConfig;
public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig) {
$this->scopeConfig = $scopeConfig;
}

public function isAllowed() {
return false;
}
}

5) Issue the following commands.

$ php bin/magento indexer:reindex
$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile
$ php bin/magento setup:static-content:deploy -f
$ php bin/magento cache:flush
Share

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' => ''
 ]
 );
 
 
 }
 }
 }
Share