Skip to content

Customer’s custom attribute, layout observer

This is the code snippet responsible for adding new attribute

 public function createAllowRequestSampleCustomerAttribute($setup)
        {
            $code = 'allow_request_sample';
            /** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            $eavSetup->addAttribute(
                Customer::ENTITY,
                'allow_request_sample',
                [
                    'type' => 'int',
                    'label' => 'Allow request sample',
                    'input' => 'select',
                    'source' => Boolean::class,
                    'required' => false,
                    'visible' => false,
                    'user_defined' => true,
                    'position' => 999,
                    'system' => 0,
                    'default' => 1,
                    'used_in_forms' => ['adminhtml_customer', 'customer_account_edit'],
                ]
            );
    
            $attribute = $this->customerAttribute->loadByCode(Customer::ENTITY, $code);
    
            $attribute->addData([
                'attribute_set_id' => 1,
                'attribute_group_id' => 1,
                'used_in_forms' => ['adminhtml_customer', 'customer_account_edit'],
            ])->save();
        }

Observer that is supposed to hide the custom tab in product view when the attribute is disabled or it’s a guest account (not logged-in).

<?php

namespace AlexRequestSampleObserver;

use MagentoCustomerModelSession;
use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkRegistry;

class LayoutGenerateBlocksAfter implements ObserverInterface
{
    /**
     * @var Registry
     */
    protected $registry;

    /**
     * @var Session
     */
    protected $customerSession;

    /**
     * @param Registry $registry
     * @param Session $customerSession
     */
    public function __construct(
        Registry $registry,
        Session  $customerSession
    ) {
        $this->registry = $registry;
        $this->customerSession = $customerSession;
    }

    /**
     * @param Observer $observer
     * @return LayoutGenerateBlocksAfter
     */
    public function execute(Observer $observer)
    {
        $product = $this->registry->registry('current_product');

        if (!$product) {
            return $this;
        }

        if ($this->requestFormDisallowed()) {
            $layout = $observer->getLayout();
            $sampleRequestBlock = $layout->getBlock('request.sample.tab');
            if ($sampleRequestBlock) {
                $sampleRequestBlock->setTemplate('');
            }
        }

        return $this;
    }

    /**
     * @return bool
     */
    private function requestFormDisallowed()
    {
        return !$this->customerSession->getCustomer()->getAllowRequestSample();
    }
}

For some reason, it’s always hidden regardless of the settings. What am I doing wrong?