Magento 2: Müşteriye özel özellik nasıl oluşturulur?


Yanıtlar:


28

Makalede Magento 2: Nasıl müşteri niteliğini yapılır? adım adım açıklayın.

Ana bölüm aşağıdaki DataInstall::installyöntemdir:

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        /** @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, '{attributeCode}', [
            'type' => 'varchar',
            'label' => '{attributeLabel}',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);
        //add attribute to attribute set
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'magento_username')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]);

        $attribute->save();


    }

CustomerSetupFactoryDoğrudan enjekte etmek yerine enjekte etmenin faydası nedir CustomerSetup? Açıkladığınız için teşekkürler.
— Vinai

@Vinai, Looks, customerSetup sınıfının yapıcıda ModuleDataSetupInterface komutunu beklediğini görünüyor ancak bu sınıf, yükleme yönteminin bağımsız değişkenidir.
— KAndy

Yana ModuleDataSetupInterfacekurulum sınıfına özgü hiçbir devlet vardır, ObjectManager sonra örneği bağımlılıkları oluşturma sorumlu olalım daha iyi olmaz mı? Bu şekilde CustomerSetupmüşteri uygulamaya daha az bağlanır. Görebildiğim kadarıyla.
— Vinai

Modülü kaldırmak özelliği kaldırmaz, o zaman nasıl kaldırılmalıdır?
— DevonDahon

Birden fazla alanı veya özelliği nasıl ekleyebiliriz?
— Jai

1

Modülünüzde, yeni bir Müşteri varlığı oluşturacak olan bu dosyayı aşağıya uygulayın .

Testi \ CustomAttribute \ Setup \ InstallData.php

<?php
namespace test\CustomAttribute\Setup;

use Magento\Customer\Setup\CustomerSetupFactory;
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\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{

    /**
     * @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;
    }


    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        /** @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, 'custom_attribute', [
            'type' => 'varchar',
            'label' => 'Custom Attribute',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'position' =>999,
            'system' => 0,
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_attribute')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],//you can use other forms also ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
        ]);

        $attribute->save();
    }
}

çalışmıyor ....
— Sarfaraj Sipai

Bu benim için Magneto 2.3'te çalıştı ibnab.com/en/blog/magento-2/…
— Raivis Dejus

@Rafael Corrêa Gomes bu yöntemi kullanarak birden fazla özellik oluşturmak mümkün mü? Nasıl?
— Pragman

@ZUBU sadece yeni bir $ customerSetup-> addAttribute eklemeniz gerekiyor, ilk önce -> addAttribute'u da referansları görmek için çekirdeğe arayabilirsiniz.
— Rafael Corrêa Gomes
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.