0

I'm trying to add an attribute called orchestra_id to Order and OrderAddress. I've made a plugin who have been already activated. So I've create an Setup\UpgradeData model. This is what it look like:

<?php 
namespace Maru3l\SyncOrchestra\Setup;

use Magento\Sales\Model\Order;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
{
    private $eavSetupFactory;

    private $eavConfig;

    private $attributeResource;

    public function __construct(
        \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory,
        \Magento\Eav\Model\Config $eavConfig,
        \Magento\Customer\Model\ResourceModel\Attribute $attributeResource
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
        $this->attributeResource = $attributeResource;
    }

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

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

        $this->installAddressOrchestraIdAttribute($eavSetup);

        $this->installOrderOrchestraIdAttribute($eavSetup);

        $setup->endSetup();
    }

    private function installAddressOrchestraIdAttribute($eavSetup) {
        $eavSetup->addAttribute('customer_address', 'orchestra_id', [
            'label'         =>  'Orchestra ID',
            'input'         =>  'text',
            'visible'       =>  true,
            'position'      =>  100,
            'unique'        =>  true,
            'required'      =>  false,
            'system'        =>  false
        ]);

        $attribute = $this->eavConfig->getAttribute('customer_address', 'orchestra_id');
        $attribute->setData('used_in_forms', ['adminhtml_customer_address', 'customer_address_edit']);
        $this->attributeResource->save($attribute);
    }

    private function installOrderOrchestraIdAttribute($eavSetup) {
        $eavSetup->addAttribute(Order::ENTITY, 'orchestra_id', [
            'label'         =>  'Orchestra ID',
            'input'         =>  'text',
            'visible'       =>  true,
            'position'      =>  100,
            'unique'        =>  true,
            'required'      =>  false,
            'system'        =>  false
        ]);

        $attribute = $this->eavConfig->getAttribute(Order::ENTITY, 'orchestra_id');
        $attribute->setData('used_in_forms', ['adminhtml_order']);
        $this->attributeResource->save($attribute);
    }
}

I've literally copy/past the exemple in https://devdocs.magento.com/guides/v2.2/extension-dev-guide/attributes.html but it is not working.

I'm always getting this issue during the php bin/magento setup:upgrade and I don't see the collomn appared in the database :

PHP Fatal error:  Uncaught Error: Call to a member function getId() on null in /var/www/vhosts/store-api.silverwax.ca/httpdocs/vendor/magento/module-eav/Model/ResourceModel/Attribute.php:117
Stack trace:
#0 /var/www/vhosts/store-api.silverwax.ca/httpdocs/vendor/magento/framework/Model/ResourceModel/Db/AbstractDb.php(832): Magento\Eav\Model\ResourceModel\Attribute->_afterSave(Object(Magento\Eav\Model\Entity\Attribute))
#1 /var/www/vhosts/store-api.silverwax.ca/httpdocs/vendor/magento/framework/Model/ResourceModel/Db/AbstractDb.php(419): Magento\Framework\Model\ResourceModel\Db\AbstractDb->processAfterSaves(Object(Magento\Eav\Model\Entity\Attribute))
#2 /var/www/vhosts/store-api.silverwax.ca/httpdocs/generated/code/Magento/Customer/Model/ResourceModel/Attribute/Interceptor.php(258): Magento\Framework\Model\ResourceModel\Db\AbstractDb->save(Object(Magento\Eav\Model\Entity\Attribute))
#3 /var/www/vhosts/store-api.silverwax.ca/httpdocs/app/code/Maru3l/SyncOrchestra/Setup/UpgradeData.php(68): Magento\Customer\Model\ResourceModel\Att in /var/www/vhosts/store-api.silverwax.ca/httpdocs/vendor/magento/module-eav/Model/ResourceModel/Attribute.php on line 117
{"messages":{"error":[{"code":500,"message":"Fatal Error: 'Uncaught Error: Call to a member function getId() on null in \/var\/www\/vhosts\/store-api.silverwax.ca\/httpdocs\/vendor\/magento\/module-eav\/Model\/ResourceModel\/Attribute.php:117\nStack trace:\n#0 \/var\/www\/vhosts\/store-api.silverwax.ca\/httpdocs\/vendor\/magento\/framework\/Model\/ResourceModel\/Db\/AbstractDb.php(832): Magento\\Eav\\Model\\ResourceModel\\Attribute->_afterSave(Object(Magento\\Eav\\Model\\Entity\\Attribute))\n#1 \/var\/www\/vhosts\/store-api.silverwax.ca\/httpdocs\/vendor\/magento\/framework\/Model\/ResourceModel\/Db\/AbstractDb.php(419): Magento\\Framework\\Model\\ResourceModel\\Db\\AbstractDb->processAfterSaves(Object(Magento\\Eav\\Model\\Entity\\Attribute))\n#2 \/var\/www\/vhosts\/store-api.silverwax.ca\/httpdocs\/generated\/code\/Magento\/Customer\/Model\/ResourceModel\/Attribute\/Interceptor.php(258): Magento\\Framework\\Model\\ResourceModel\\Db\\AbstractDb->save(Object(Magento\\Eav\\Model\\Entity\\Attribute))\n#3 \/var\/www\/vhosts\/store-api.silverwax.ca\/httpdocs\/app\/code\/Maru3l\/SyncOrchestra\/Setup\/UpgradeData.php(68): Magento\\Customer\\Model\\ResourceModel\\Att' in '\/var\/www\/vhosts\/store-api.silverwax.ca\/httpdocs\/vendor\/magento\/module-eav\/Model\/ResourceModel\/Attribute.php' on line 117","trace":"Trace is not available."}]}}

I've also create the etc\extension_attributes.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="Maru3l_SyncOrchestra" setup_version="0.0.18">
        <sequence>
            <module name="Magento_Cusomer"/>
            <module name="Magento_InventoryApi"/>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>
maru3l
  • 135
  • 9

1 Answers1

0

I think you are trying to create a custom address attribute. Is that right?

Is this the same problem you were having before?

Can't create custom attribute for order_address

The setup file in my extension creates the custom address attribute

https://github.com/DominicWatts/Magento-2-Custom-Address/blob/master/app/code/Xigen/Address/Setup/InstallData.php

Or do you mean add column to quote_address and sales_order_address

I think something like this should work


    class InstallSchema implements InstallSchemaInterface
    {
        public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $installer = $setup;
            $installer->startSetup();
            $installer->getConnection()->addColumn(
                $installer->getTable('quote_address'),
                'backoffice_id',
                [
                    'type' => 'varchar',
                    'length' => 255,
                ]
            );

            $installer->getConnection()->addColumn(
                $installer->getTable('sales_order_address'),
                'backoffice_id',
                [
                    'type' => 'varchar',
                    'length' => 255,
                ]
            );
            $installer->endSetup();
        }
    }

Did you manage to get column in sales order?

namespace MyCompany\MyModule\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        $connection = $installer->getConnection();

        if ($connection->tableColumnExists('sales_order', 'backoffice_id') === false) {
            $connection
                ->addColumn(
                    $setup->getTable('sales_order'),
                    'backoffice_id',
                    [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        'length' => 255,
                        'comment' => 'Backoffice Id'
                    ]
                );
        }
        $installer->endSetup();
    }
}
Dominic Pixie
  • 7,520
  • 4
  • 17
  • 56
  • Yes, it's part of the question I've asked earlier. My need is to store the ID of the Address and the Order. It's two different ID from the ERP. I don't know how I did it with all my attempts, but somehow, I got the attribute in the table sales_order_address and I can use the $address->setOrchestraId() and $address->getOrchestraId() like the way i need it. But I can't figure out how to reproduce it in the table sales_order. That's the nature of the question. – maru3l May 27 '19 at 13:48
  • Edited my answer – Dominic Pixie May 27 '19 at 20:54