1

I have created a custom attribute as follows:

<?php

namespace RB\Certification\Setup;

use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Setup\CustomerSetupFactory;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param AttributeSetFactory $attributeSetFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), 'VERSION') < 0) {
            /** @var CustomerSetup $customerSetup */
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

            $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
            $attributeSetId = $customerEntity->getDefaultAttributeSetId();

            /** @var $attributeSet AttributeSet */
            $attributeSet = $this->attributeSetFactory->create();
            $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

            $customerSetup->removeAttribute(Customer::ENTITY, "verified_buyer");

            $customerSetup->addAttribute(Customer::ENTITY, 'verfied_buyer', [
                'type' => 'int',
                'label' => 'Verified Buyer',
                'input' => 'boolean',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 90,
                'position' => 90,
                'system' => 0,
            ]);

            $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'verfied_buyer')
                ->addData([
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer'],
                ]);

            $attribute->save();
        }
    }
}

And I am trying to fetch in frontend block as follows :

 <?php

    /**
     * Copyright © 2016 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */

    namespace RB\Review\Block\Product;

    use Magento\Catalog\Api\ProductRepositoryInterface;

    /**
     *
     * @author     Rocket Bazaar Team
     */
    class ListView extends \Magento\Review\Block\Product\View\ListView {

        protected $customerRepository;
        /**
         * @param array $data
         */
        public function __construct(
            \Magento\Catalog\Block\Product\Context $context,
            \Magento\Framework\Url\EncoderInterface $urlEncoder,
            \Magento\Framework\Json\EncoderInterface $jsonEncoder,
            \Magento\Framework\Stdlib\StringUtils $string,
            \Magento\Catalog\Helper\Product $productHelper,
            \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
            \Magento\Framework\Locale\FormatInterface $localeFormat, 
            \Magento\Customer\Model\Session $customerSession,
            ProductRepositoryInterface $productRepository,
            \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
            \Magento\Review\Model\ResourceModel\Review\CollectionFactory $collectionFactory, 
            array $data = [],
            \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
        ) {
            $this->customerRepository = $customerRepository;
              parent::__construct(
                $context,
                $urlEncoder,
                $jsonEncoder,
                $string,
                $productHelper,
                $productTypeConfig,
                $localeFormat,
                $customerSession,
                $productRepository,
                $priceCurrency,
                $collectionFactory,
                $data
            );
        }

        public function getCustomerDetails($customerId='') {
            if($customerId){
                $customer = $this->customerRepository->getById($customerId);
                $cattrValue = $customer->getCustomAttribute('verified_buyer');
                return $cattrValue;
            }
            return 0;

        }

    }

Let me know where I am wrong. Thanks !

2

Looks like a typo.

You've setup the attribute name as verfied_buyer in the database, and are then calling verified_buyer later on. Both should be the same, verified_buyer.

The line in UpgradeData class:

$customerSetup->addAttribute(Customer::ENTITY, 'verfied_buyer', [

Should be:

$customerSetup->addAttribute(Customer::ENTITY, 'verified_buyer', [

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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