2

I have created two attribute for customer as "Restaurant" and "Stores". Currently I am getting data from their respective model collection through InstallData.php in customer add/edit form. Now I want stores to be appear on the basis restaurant by ajax call. How can I do this.

enter image description here

InstallData.php

<?php 


namespace Vendor\Module\Setup;

 use Magento\Eav\Setup\EavSetup;
 use Magento\Eav\Setup\EavSetupFactory;
 use Magento\Framework\Setup\InstallDataInterface;
 use Magento\Framework\Setup\ModuleContextInterface;
 use Magento\Framework\Setup\ModuleDataSetupInterface;
 use Magento\Eav\Model\Config;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'restaurant',
            [
                'type' => 'text',
                'label' => 'Restaurant',
                'input' => 'select',
                'source' => 'Vendor\Module\Model\Config\Source\Options',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'required' => true,
                'default' => '0',
                'sort_order' => 99,
                'system' => false,
                'position' => 99
            ]
        );
        $restaurantAttribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'restaurant');
        $usedInForms = array(
                        'customer_account_create',
                        'customer_account_edit',
                        'checkout_register',
                    );
        if (!empty($data['adminhtml_only'])) {
            $usedInForms = array('adminhtml_customer');
        } else {
            $usedInForms[] = 'adminhtml_customer';
        }
               $restaurantAttribute->setData('used_in_forms', $usedInForms); 

            $restaurantAttribute->save();

        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'stores',
            [
                'type' => 'text',
                'label' => 'Stores',
                'input' => 'multiselect',
                'source' => 'Vendor\Module\Model\Config\Source\Stores',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'required' => true,
                'default' => '0',
                'sort_order' => 100,
                'system' => false,
                'position' => 100
            ]
        );
        $storeAttribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'stores');
        $usedInForms = array(
                        'customer_account_create',
                        'customer_account_edit',
                        'checkout_register',
                    );
        if (!empty($data['adminhtml_only'])) {
            $usedInForms = array('adminhtml_customer');
        } else {
            $usedInForms[] = 'adminhtml_customer';
        }
               $storeAttribute->setData('used_in_forms', $usedInForms); 

        $storeAttribute->save();



    }
}

Options.php (same Stores.php)

<?php

namespace Vendor\Module\Model\Config\Source;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory;
use Magento\Framework\DB\Ddl\Table;



class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    /**
     * @var OptionFactory
     */
    protected $optionFactory;

    /**
     * @param OptionFactory $optionFactory
     */
    /*public function __construct(OptionFactory $optionFactory)
    {
        $this->optionFactory = $optionFactory;  
        //you can use this if you want to prepare options dynamically  
    }*/

    /**
     * Get all options
     *
     * @return array
     */
    public function getAllOptions()
    {
        //$restaurant = $this->getRestaurantDetail();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $tableName = $resource->getTableName('restaurant_template');
        $sql = "Select * FROM " . $tableName;
        $result = $connection->fetchAll($sql);
        $this->_options = array(); 

        foreach( $result as $key => $value){
            //$this->_options[] = array('label' => $value['name'], 'value ' => $value['name']);

            $this->_options[] =

                array(
                    "label" => $value['name'],
                    "value" =>  $value['id']

            );

        }

        return $this->_options;
    }



    /**
     * Get a text for option value
     *
     * @param string|integer $value
     * @return string
     */
    public function getOptionText($value)
    {
        $options = $this->getAllOptions();
        foreach ($options as $option) {
            if ($option["value"] == $value) {
                return $option["label"];
            }
        }
        return false;
    }

    /**
     * Retrieve Column(s) for Flat
     *
     * @return array
     */
    public function getFlatColums()
    {
        $columns = array();
        $columns[$this->getAttribute()->getAttributeCode()] = array(
            "type"      => "text",
            "unsigned"  => false,
            "is_null"   => true,
            "default"   => null,
            "extra"     => null
        );

        return $columns;
    }

    /**
     * Retrieve Indexes(s) for Flat
     *
     * @return array
     */
    public function getFlatIndexes()
    {
        $indexes = array();

        $index = "IDX_" . strtoupper($this->getAttribute()->getAttributeCode());
        $indexes[$index] = array(
            "type"      => "index",
            "fields"    => array($this->getAttribute()->getAttributeCode())
        );

        return $indexes;
    }

    /**
     * Retrieve Select For Flat Attribute update
     *
     * @param int $store
     * @return Varien_Db_Select|null
     */
    public function getFlatUpdateSelect($store)
    {
        return Mage::getResourceModel("eav/entity_attribute")
            ->getFlatUpdateSelect($this->getAttribute(), $store);
    }
}

Your Answer

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

Browse other questions tagged or ask your own question.