0

I need to create a custom attribute on the order address to stock my ERP id of the address for a syncing purpose.

I've tried to create a Eav in Maru3l/SyncOrchestra/Setup/InstallData.php. I don't see the input appeared in the admin panel but the file in the log folder is created.

<?php

namespace Maru3l\SyncOrchestra\Setup;

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

class InstallData implements InstallDataInterface
{
    /**
     * Attribute Code of the Orchestra ID attribute
     */
    const ORECHESTRA_ID_ATTRIBUTE_CODE = 'orchestra_id';

    /**
     * @var EavSetupFactory
     */
    protected $eavSetupFactory;

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

    /**
     * Install Orchestra id attribute
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            'customer_address',
            self::ORECHESTRA_ID_ATTRIBUTE_CODE,
            [
                'label'         =>  'Orchestra ID',
                'input'         =>  'text',
                'visible'       =>  true,
                'position'      =>  100,
                'unique'        =>  true,
                'required'      =>  false,
                'system'        =>  false
            ]
        );

        $dumpFile = fopen('/var/www/vhosts/store-api.silverwax.ca/httpdocs/var/log/dataDump', 'w+');
        fwrite($dumpFile, 'Install eav \n');

        $setup->endSetup();
    }
}

I've also created the Maru3l/SyncOrchestra/etc/extension_attributes.xml:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Mageside. All rights reserved.
 * See MS-LICENSE.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Sales\Api\Data\OrderAddressInterface">
        <attribute code="orchestra_id" type="string" />
    </extension_attributes>
</config>

My other concern is how can I get or set the value programmatically.

maru3l
  • 135
  • 9

1 Answers1

1

I recently covered loading address by custom attribute in question. I created a extension to test the theory. Setup is included within it. But here it is

<?php
namespace Xigen\Address\Setup;
use Magento\Customer\Setup\CustomerSetupFactory;
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;
    }
    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer_address');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        $customerSetup->addAttribute('customer_address', 'backoffice_id', [
            'type' => 'int',
            'label' => 'Backoffice Id',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 50,
            'position' => 50,
            'system' => 0,
        ]);
        $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'backoffice_id')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => [
                    'adminhtml_customer_address',
                    'customer_address_edit',
                    'customer_register_address',
                ],
            ]);
        $attribute->save();
    }
}

Here you go

https://github.com/DominicWatts/Magento-2-Custom-Address

Here is my original question

Loading a customer address by a custom attribute

You can save in admin. Or load address using getById and set it.

/**
 * @var \Magento\Customer\Api\AddressRepositoryInterface
 */
protected $addressRepository;

/**
 * Sync constructor.
 * @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
 */
public function __construct(
    \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
) {
    $this->addressRepository = $addressRepository;
}

public function changeAddress($addressId)
{
    /** @var \Magento\Customer\Api\Data\AddressInterface $address */
    $address = $this->addressRepository->getById($addressId);
    // whatever
    $this->addressRepository->save($address);
}
Dominic Pixie
  • 7,520
  • 4
  • 17
  • 56