0

My client has multi website in Magento2. (ex. Site A and Site B)

Now, I have to create customer attribute for both website but value of both sites attributes are different.

For Site A customer attribute value like (A,B,C)

and For Site B Customer attribute value like (P,Q,R).

Does anyone know how to achive this?

1 Answer 1

1

I got solution of how we can use same attribute with different store and websites.

For the we can create our custom source model of an attributes.

<?php

namespace Custom\Module\Model\Eav\Entity\Attribute\Source;
use Magento\Store\Model\StoreManagerInterface;

class FindUs extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    protected $storeManager;
    private $state;


    public function __construct(
        StoreManagerInterface $storeManager,
        \Magento\Framework\App\State $state
    ) {
        $this->storeManager = $storeManager;
        $this->state = $state;
    }

    /**
     * Retrieve all options array
     *
     * @return array
     */
    public function getAllOptions()
    { 
        $storeId = $this->storeManager->getStore()->getStoreId();
        $areaCode = $this->state->getAreaCode();
        $this->_options = array(
                    array(
                    "label" => __("Please select"),
                    "value" =>  0
                )
            );

        if($storeId != 7){
            $this->_options[] =  array("label" => __("Referral"),"value" =>  1);
            $this->_options[] =  array("label" => __("Online Search"),"value" =>  2);
            $this->_options[] =  array("label" => __("Magazine Ads"),"value" =>  3);
            $this->_options[] =  array("label" => __("Walk In"),"value" =>  4);
            $this->_options[] =  array("label" => __("Radio"),"value" =>  5);
            $this->_options[] =  array("label" => __("Newspaper"),"value" =>  6);    
        }
        if($storeId == 7 || $areaCode == 'adminhtml'){
            $this->_options[] =  array("label" => __("ASI"),"value" =>  7); 
            $this->_options[] =  array("label" => __("Newsletter"),"value" =>  8); 
            $this->_options[] =  array("label" => __("Referred by friend/other company"),"value" =>  9);
            $this->_options[] =  array("label" => __("Organic search"),"value" =>  10); 
        }
        return $this->_options;
    }

    /**
     * Retrieve option array
     *
     * @return array
     */
    public function getOptionArray()
    {
        $_options = array();
        foreach ($this->getAllOptions() as $option) {
            $_options[$option["value"]] = $option["label"];
        }
        return $_options;
    }

}

With the help of store manager we can display atribute value store wise.

But in admin all the attribute of value are display for all store.

also, you can create store configuration variable and load accordigly.

If you want to display attribute store wise in admin than you have to create custom javascript.

Thanks,

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.