0

I'm too new to Magento: i'm getting an error when setup:upgrade, here is my code: app/code/Adamsmage/Checkoutattribute/Setup/InstallData.php

<?php
namespace Adamsmage\Checkoutattribute\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;

class InstallData implements InstallDataInterface
{

    private $customerSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute('customer_address', 'example', [
            'label' => 'example',
            'input' => 'text',
            'type' => 'varchar',
            'source' => '',
            'required' => false,
            'position' => 333,
            'visible' => true,
            'system' => false,
            'is_used_in_grid' => false,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => false,
            'is_searchable_in_grid' => false,
            'backend' => ''
        ]);


        $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'example')
            ->addData(['used_in_forms' => [
                'customer_address_edit',
                'customer_register_address',
                'adminhtml_customer_address',
                'customer_address'
            ]]);
        $attribute->save();

        $installer->getConnection()->addColumn(
            $installer->getTable('quote_address'),
            'example',
            [
                'type' => 'varchar',
                'length' => 255
            ]
        );

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

ERROR: enter image description here

4
  • Just replace $installer variable to $setup – Evgeny Levinsky Jul 18 '19 at 15:18
  • already tried but getting this error: Impossible to create a column without comment. – Partab Saifuddin Zakir Jul 18 '19 at 15:20
  • add the comment to the column information [ 'type' => 'varchar', 'length' => 255, 'comment' =>'Example' ] – WISAM HAKIM Jul 18 '19 at 15:29
  • now getting this error: Invalid column definition data – Partab Saifuddin Zakir Jul 18 '19 at 15:45
0

You need to define $installer

https://github.com/DominicWatts/AutoShipment/blob/master/Setup/InstallData.php#L35-L36

See how I use $salesSetup to add column to sales_order tables

use Magento\Sales\Setup\SalesSetupFactory;
/**
 * InstallData class
 */
class InstallData implements InstallDataInterface
{
    private $salesSetupFactory;
    /**
     * Constructor
     *
     * @param \Magento\Sales\Setup\SalesSetupFactory $salesSetupFactory
     */
    public function __construct(SalesSetupFactory $salesSetupFactory)
    {
        $this->salesSetupFactory = $salesSetupFactory;
    }
        $salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);
        $salesSetup->addAttribute(
            'order',
            'prevent_automatic_shipment',
            [
                'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
                'length' => 5,
                'default' => 2,
                'unsigned' => true,
                'nullable' => false,
                'visible' => false,
                'required' => false,
                'grid' => false
            ]
        );

So you would need to define a variable for you quote_address and sales_order_address addColumn's

0

Remember that

Implementing declarative schema is not a requirement for Magento 2.3. However, upgrade scripts will be phased out in favor of declarative schema.

I will use data patch in your case. We need to create two separate scripts: data is for DML, schema is for DDL.

app/code/Vendor/CustomerDemo/Setup/Patch/Data/CustomerAddressAttribute.php

<?php

namespace Vendor\CustomerDemo\Setup\Patch\Data;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class CustomerAddressAttribute implements DataPatchInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * CustomerAgeAttribute constructor.
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param CustomerSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $eavSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $eavSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }


    public static function getDependencies()
    {
        return [];
    }

    public function getAliases()
    {
        return [];
    }

    /**
     * @return DataPatchInterface|void
     * @throws LocalizedException
     */
    public function apply()
    {
        /** @var CustomerSetup $eavSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();
        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(
            'customer_address',
            'example',
            [
                'type' => 'varchar',
                'label' => 'Example',
                'input' => 'text',
                'required' => false,
                'sort_order' => 130,
                'visible' => true,
                'position'  => 130,
                'user_defined' => true,
                'system'  => false
            ]
        );

        $exampleAttribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'example');

        $exampleAttribute->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' =>
                [
                    'customer_address_edit',
                    'customer_register_address',
                    'adminhtml_customer_address',
                    'customer_address'
                ],
        ]);

        $exampleAttribute->save();
    }
}

app/code/Vendor/CustomerDemo/Setup/Patch/Schema/ExampleAttribute.php

<?php

namespace Vendor\CustomerDemo\Setup\Patch\Schema;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class ExampleAttribute implements SchemaPatchInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * CustomerAgeAttribute constructor.
     * @param ModuleDataSetupInterface $moduleDataSetup
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
    }


    public static function getDependencies()
    {
        return [];
    }

    public function getAliases()
    {
        return [];
    }


    public function apply()
    {
        $this->moduleDataSetup->startSetup();

        $this->moduleDataSetup->getConnection()->addColumn(
            $this->moduleDataSetup->getTable('quote_address'),
            'example',
            [
                'type' => Table::TYPE_TEXT,
                'length' => 255,
                'nullable' => true,
                'comment'  => 'Example',
            ]
        );

        $this->moduleDataSetup->getConnection()->addColumn(
            $this->moduleDataSetup->getTable('sales_order_address'),
            'example',
            [
                'type' => Table::TYPE_TEXT,
                'length' => 255,
                'nullable' => true,
                'comment'  => 'Example',
            ]
        );

        $this->moduleDataSetup->endSetup();
    }
}

Note for this schema script, 'type' => Table::TYPE_TEXT, varchar is deprecated. If you specify type TYPE_TEXT but set length to lets say 255. Magento will create a MySQL column of VARCHAR type. Add varchar column to order address.

0

create a new file only with the modifications in quote_address and sales_order_address example: https://github.com/experius/Magento-2-Module-Experius-ExtraCheckoutAddressFieldsTest/blob/master/Setup/InstallSchema.php

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.