2017-05-11 18 views
7

Chcę dodać niestandardową kolumnę (telefon) w tabeli klienta w magento2 i chcę dodać wartość w tym polu podczas rejestracji klienta.Jak dodać niestandardową kolumnę do tabeli klientów w Magento2?

W tym celu najpierw utworzyć kolumnę (telefon) w dB w customer_entity table.While stworzyć klientowi gdy zgłoszę $ na klienta> setTelephone ('1234567890') w Magento/klient/Controller/Konta/CreatePost. php w wykonaniu funkcji. Występuje błąd Nie zdefiniuj funkcji setTelephone w Magento/Customer/Model/Data/Customer.php. Ale już teraz tworzę tę funkcję w tym Modelu.

Magento/klienta/Controller/Konto/CreatePost.php

public function execute() 
{ 
    /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ 
    $resultRedirect = $this->resultRedirectFactory->create(); 
    if ($this->session->isLoggedIn() || !$this->registration->isAllowed()) { 
     $resultRedirect->setPath('*/*/'); 
     return $resultRedirect; 
    } 

    if (!$this->getRequest()->isPost()) { 
     $url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]); 
     $resultRedirect->setUrl($this->_redirect->error($url)); 
     return $resultRedirect; 
    } 

    $this->session->regenerateId(); 

    try { 
     $address = $this->extractAddress(); 
     $addresses = $address === null ? [] : [$address]; 

     $customer = $this->customerExtractor->extract('customer_account_create', $this->_request); 
     $customer->setAddresses($addresses); 

     //Here is I set the telephone and it is giving an error 
     $customer->setTelephone('1234567890'); 

Magento/klienta/model/Data/Customer.php

public function setTelephone($telephone) 
{ 
    return $this->setData(self::TELEPHONE, $telephone); 
} 

public function getTelephone() 
{ 
    return $this->_get(self::TELEPHONE); 
} 

Magento/klienta/Api /Data/CustomerInterface.php

<?php 
namespace Magento\Customer\Api\Data; 

interface CustomerInterface extends \Magento\Framework\Api\CustomAttributesDataInterface 
{ 
    /* Add this code*/ 
    const TELEPHONE = 'telephone'; 

    public function getTelephone(); 

    public function setTelephone($telephone); 

} 

Próbowałem zrobić z niestandardowym modułem. Wystąpił błąd.

1 exception(s): Exception #0 (Magento\Framework\Exception\LocalizedException): Please upgrade your database: Run "bin/magento setup:upgrade" from the Magento root directory. The following modules are outdated: Onjection_Customer data: current version - none, required version - 1.0.0...

Code Module

  1. app/code/Onjection/klienta/registration.php

    <?php 
    
        \Magento\Framework\Component\ComponentRegistrar::register(
         \Magento\Framework\Component\ComponentRegistrar::MODULE, 
         'Onjection_Customer', 
         __DIR__ 
        ); 
    

2.app/code/Onjection/Customer /etc/module.xml

<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> 
    <module name="Onjection_Customer" setup_version="1.0.0"> 
     <sequence>   
      <module name="Magento_Customer"/> 
     </sequence> 
    </module> 
</config> 

3.app/code/Onjection/Customer/Setup/InstallData.php

<?php 

namespace Onjection\Customer\Setup; 

use Magento\Framework\Setup\InstallDataInterface; 
use Magento\Framework\Setup\ModuleDataSetupInterface; 
use Magento\Framework\Setup\ModuleContextInterface; 

/** 
* @codeCoverageIgnore 
*/ 
class InstallData implements InstallDataInterface 
{ 
    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; 
    } 

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 
    { 
     $setup->startSetup(); 

     /** @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->addAttribute(Customer::ENTITY, 'mobile', [ 
      'type' => 'varchar', 
      'label' => 'Mobile', 
      'input' => 'text', 
      'required' => false, 
      'visible' => true, 
      'user_defined' => true, 
      'sort_order' => 1000, 
      'position' => 1000, 
      'system' => 0, 
     ]); 

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

     $attribute->save(); 
     $setup->endSetup(); 
    } 
} 

Polecenia używane do instalacji:

  1. konfiguracji php bin/magento: upgrade

  2. Ustawienia bin/magento: statyczne: wdrażanie

+0

To nie jest właściwy sposób pracy z Magento. Zobacz [tutaj] (https://magento.stackexchange.com/questions/88245/magento2-create-a-customer-custom- attribute). Nie powinieneś także modyfikować podstawowych plików. – subroutines

+0

@ subsuty Jak dodać ten niestandardowy atrybut na stronie rejestracji klienta? Czy jest jakaś metoda, aby to zrobić bez tworzenia modułu? – Tomas

+0

'telefon' jest już istniejącym atrybutem adresu klienta, możesz zajrzeć do dostosowywania' magento \ module-customer \ view \ frontend \ templates \ form \ register.phtml', aby pokazać je na stronie rejestracji. – subroutines

Odpowiedz

2

Można stworzyć klienta atrybut niestandardowy Dodawanie klienta atrybut

1) Utwórz moduł plik

<?xml version="1.0" encoding="UTF-8"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> 
    <module name="Kalpesh_Mobile" setup_version="1.0.0"> 
     <sequence>   
      <!--<module name="Kalpesh_Mobile"/>--> 
      <module name="Magento_Customer"/> 
     </sequence> 
    </module> 
</config> 



class InstallData implements InstallDataInterface 
    { 
     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; 
     } 

     public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 
     { 
        $setup->startSetup(); 


      /** @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->addAttribute(Customer::ENTITY, 'mobile', [ 
       'type' => 'varchar', 
       'label' => 'Mobile', 
       'input' => 'text', 
       'required' => false, 
       'visible' => true, 
       'user_defined' => true, 
       'sort_order' => 1000, 
       'position' => 1000, 
       'system' => 0, 
      ]); 

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

      $attribute->save(); 

        $setup->endSetup(); 



     } 
    } 
+0

Podając błąd. Poddaj mi moje pytanie, sprawdź. – Tomas

2

Musisz utworzyć własną moduł

w Magento prostym przykładzie modułu można sprawdzić https://github.com/magento/magento2-samples/blob/master/sample-module-form-uicomponent/view/adminhtml/ui_component/sampleform_form.xml dostarczyły one funkcjonalne za dodanie nowego pola

<field name="color"> 
    <argument name="data" xsi:type="array"> 
     <item name="config" xsi:type="array"> 
      <!--component constructor--> 
      <item name="component" xsi:type="string">Magento_SampleForm/js/form/element/color-select</item> 
      <!--main template for form field that renders elementTmpl as a child template--> 
      <item name="template" xsi:type="string">ui/form/field</item> 
      <!--customized form element template that will show colors--> 
      <item name="elementTmpl" xsi:type="string">Magento_SampleForm/form/element/color-select</item> 
      <item name="label" xsi:type="string">Autumn colors</item> 
      <item name="visible" xsi:type="boolean">true</item> 
      <item name="dataType" xsi:type="string">text</item> 
      <item name="formElement" xsi:type="string">input</item> 
      <item name="source" xsi:type="string">sampleform</item> 
     </item> 
    </argument> 
</field> 

w m2 nie ma potrzeby, aby edytować mysql wiersze bezpośrednio lub zmień kod podstawowy, wszystko co możesz i powinieneś przepisać.Czytać dokumenty dotyczące ogólnych zasad pracy z Magento 2

i jak wspomniano w komentarzach, jeśli chcesz pole telefonicznej to już wdrożyć

Powiązane problemy