0

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 Alex\RequestSample\Observer;

use Magento\Customer\Model\Session;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Registry;

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?

1
  • $this->requestFormDisallowed() this condition is called? Commented Jan 23 at 7:46

1 Answer 1

1

The code seems to be fine. However, there may be a problem with the customer attribute value, possibly resulting in a consistent false value being returned.

It is advisable to verify/output the value of $this->requestFormDisallowed() in all cases (if-else cases).

Updated Solution:

Please load the customer by customer id (customer id can be fetched from the customer session). After loading the customer, get the allow_request_sample attribute value from the customer object.

protected $_customerRepositoryInterface;
protected $customerSession;

public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
    \ Magento\Customer\Model\SessionFactory $customerSession
) {
    $this->_customerRepositoryInterface = $customerRepositoryInterface;
    $this->customerSession = $customerSession;
}

Now load the customer:

$customerSession = $this->customerSession->create();
$customerId = $customerSession->getCustomer()->getId(); // fetch the customer id from the customer session
$customer = $this->_customerRepositoryInterface->getById($customerId);

$customer->getAllowRequestSample();

OR

$customer->getData('allow_request_sample');

OR

$customer->getCustomAttribute('allow_request_sample')->getValue();

In this way, You will get the correct value instead of the null value.

8
  • it always returns true leading to removing the block. also, it can see the customerId that is logged in, in my case it's a completely new one with id 5, but in the debug session it returns a null. maybe that's the issue?
    – Dramorian
    Commented Jan 23 at 12:02
  • You have used ! to the below line: !$this->customerSession->getCustomer()->getAllowRequestSample() is there specific reason using this? Also are you getting the getAllowRequestSample() value that is saved into the database? Commented Jan 24 at 10:28
  • unfortunately, it returns null, thus the whole expression !$this->customerSession->getCustomer()->getAllowRequestSample() is true
    – Dramorian
    Commented Jan 24 at 17:24
  • 1
    It is returning 1 always because the attribute default value is 1. You have to load the customer by customer id (customer id can fetched from the customer session) and then get the allow_request_sample attribute value from the loaded customer object. Commented Jan 25 at 5:09
  • 1
    I have updated the answer & added the solution. Please try it once, this should work. Commented Jan 25 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.